From e08eebb115d36ef530024a216414994b5599562d Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Wed, 24 May 2023 19:30:38 +1000 Subject: [PATCH] Add support for displaying lf options in the ruler (#1257) * Add support for displaying lf options in the ruler * Print escape character as plain text * Document user defined options * Change escape rules * Use reflect instead of environment table * Factor out common option map logic * Improve error message slightly * Display blank option values --- doc.go | 4 +++- docstring.go | 21 +++++++++++++-------- eval.go | 6 ++++-- lf.1 | 2 +- main.go | 31 ++++++++++++++++--------------- ui.go | 18 +++++++++++++++++- 6 files changed, 54 insertions(+), 28 deletions(-) diff --git a/doc.go b/doc.go index 6ea7807c..096cf5f1 100644 --- a/doc.go +++ b/doc.go @@ -845,13 +845,15 @@ Reverse the direction of sort. ruler []string (default 'acc:progress:selection:filter:ind') List of information shown in status line ruler. -Currently supported information types are 'acc', 'progress', 'selection', 'ind' and 'df'. +Currently supported information types are 'acc', 'progress', 'selection', 'filter', 'ind', 'df' and names starting with 'lf_'. `acc` shows the pressed keys (e.g. for bindings with multiple key presses or counts given to bindings). `progress` shows the progress of file operations (e.g. copying a large directory). `selection` shows the number of files that are selected, or designated for being cut/copied. `filter` shows 'F' if a filter is currently being applied. `ind` shows the current position of the cursor as well as the number of files in the current directory. `df` shows the amount of free disk space remaining. +Names starting with `lf_` show the value of environment variables exported by lf. This is useful for displaying the current settings (e.g. `lf_selmode` displays the current setting for the `selmode` option). +User defined options starting with `lf_user_` are also supported, so it is possible to display information set from external sources. selmode string (default 'all') diff --git a/docstring.go b/docstring.go index 60bd4a8e..f7166701 100644 --- a/docstring.go +++ b/docstring.go @@ -903,14 +903,19 @@ Reverse the direction of sort. ruler []string (default 'acc:progress:selection:filter:ind') List of information shown in status line ruler. Currently supported information -types are 'acc', 'progress', 'selection', 'ind' and 'df'. 'acc' shows the -pressed keys (e.g. for bindings with multiple key presses or counts given to -bindings). 'progress' shows the progress of file operations (e.g. copying a -large directory). 'selection' shows the number of files that are selected, -or designated for being cut/copied. 'filter' shows 'F' if a filter is currently -being applied. 'ind' shows the current position of the cursor as well as the -number of files in the current directory. 'df' shows the amount of free disk -space remaining. +types are 'acc', 'progress', 'selection', 'filter', 'ind', 'df' and names +starting with 'lf_'. 'acc' shows the pressed keys (e.g. for bindings with +multiple key presses or counts given to bindings). 'progress' shows the progress +of file operations (e.g. copying a large directory). 'selection' shows the +number of files that are selected, or designated for being cut/copied. 'filter' +shows 'F' if a filter is currently being applied. 'ind' shows the current +position of the cursor as well as the number of files in the current directory. +'df' shows the amount of free disk space remaining. Names starting with 'lf_' +show the value of environment variables exported by lf. This is useful for +displaying the current settings (e.g. 'lf_selmode' displays the current setting +for the 'selmode' option). User defined options starting with 'lf_user_' are +also supported, so it is possible to display information set from external +sources. selmode string (default 'all') diff --git a/eval.go b/eval.go index db8475ac..0ddf107f 100644 --- a/eval.go +++ b/eval.go @@ -494,8 +494,10 @@ func (e *setExpr) eval(app *app, args []string) { switch s { case "df", "acc", "progress", "selection", "filter", "ind": default: - app.ui.echoerr("ruler: should consist of 'df', 'acc', 'progress', 'selection', 'filter' or 'ind' separated with colon") - return + if !strings.HasPrefix(s, "lf_") { + app.ui.echoerr("ruler: should consist of 'df', 'acc', 'progress', 'selection', 'filter', 'ind' or 'lf_' separated with colon") + return + } } } gOpts.ruler = toks diff --git a/lf.1 b/lf.1 index 49226b7f..6de2b894 100644 --- a/lf.1 +++ b/lf.1 @@ -1014,7 +1014,7 @@ Reverse the direction of sort. ruler []string (default 'acc:progress:selection:filter:ind') .EE .PP -List of information shown in status line ruler. Currently supported information types are 'acc', 'progress', 'selection', 'ind' and 'df'. `acc` shows the pressed keys (e.g. for bindings with multiple key presses or counts given to bindings). `progress` shows the progress of file operations (e.g. copying a large directory). `selection` shows the number of files that are selected, or designated for being cut/copied. `filter` shows 'F' if a filter is currently being applied. `ind` shows the current position of the cursor as well as the number of files in the current directory. `df` shows the amount of free disk space remaining. +List of information shown in status line ruler. Currently supported information types are 'acc', 'progress', 'selection', 'filter', 'ind', 'df' and names starting with 'lf_'. `acc` shows the pressed keys (e.g. for bindings with multiple key presses or counts given to bindings). `progress` shows the progress of file operations (e.g. copying a large directory). `selection` shows the number of files that are selected, or designated for being cut/copied. `filter` shows 'F' if a filter is currently being applied. `ind` shows the current position of the cursor as well as the number of files in the current directory. `df` shows the amount of free disk space remaining. Names starting with `lf_` show the value of environment variables exported by lf. This is useful for displaying the current settings (e.g. `lf_selmode` displays the current setting for the `selmode` option). User defined options starting with `lf_user_` are also supported, so it is possible to display information set from external sources. .PP .EX selmode string (default 'all') diff --git a/main.go b/main.go index 408e424f..fa267e8e 100644 --- a/main.go +++ b/main.go @@ -115,7 +115,8 @@ func fieldToString(field reflect.Value) string { return value } -func exportOpts() { +func getOptsMap() map[string]string { + opts := make(map[string]string) v := reflect.ValueOf(gOpts) t := v.Type() @@ -149,27 +150,27 @@ func exportOpts() { sortby = "ext" } - os.Setenv("lf_sortby", sortby) - - reverse := strconv.FormatBool(gOpts.sortType.option&reverseSort != 0) - os.Setenv("lf_reverse", reverse) - - hidden := strconv.FormatBool(gOpts.sortType.option&hiddenSort != 0) - os.Setenv("lf_hidden", hidden) - - dirfirst := strconv.FormatBool(gOpts.sortType.option&dirfirstSort != 0) - os.Setenv("lf_dirfirst", dirfirst) + opts["lf_sortby"] = sortby + opts["lf_reverse"] = strconv.FormatBool(gOpts.sortType.option&reverseSort != 0) + opts["lf_hidden"] = strconv.FormatBool(gOpts.sortType.option&hiddenSort != 0) + opts["lf_dirfirst"] = strconv.FormatBool(gOpts.sortType.option&dirfirstSort != 0) } else if name == "lf_user" { // set each user option for key, value := range gOpts.user { - os.Setenv(name+"_"+key, value) + opts[name+"_"+key] = value } } else { - field := v.Field(i) - value := fieldToString(field) - os.Setenv(name, value) + opts[name] = fieldToString(v.Field(i)) } } + + return opts +} + +func exportOpts() { + for key, value := range getOptsMap() { + os.Setenv(key, value) + } } func startServer() { diff --git a/ui.go b/ui.go index 542465b8..4c013916 100644 --- a/ui.go +++ b/ui.go @@ -804,6 +804,18 @@ func (ui *ui) drawPromptLine(nav *nav) { ui.promptWin.print(ui.screen, 0, 0, st, prompt) } +func formatRulerOpt(name string, val string) string { + // handle escape character so it doesn't mess up the ruler + val = strings.ReplaceAll(val, "\033", "\033[7m\\033\033[0m") + + // display name of builtin options for clarity + if !strings.HasPrefix(name, "lf_user_") { + val = fmt.Sprintf("%s=%s", strings.TrimPrefix(name, "lf_"), val) + } + + return val +} + func (ui *ui) drawStatLine(nav *nav) { st := tcell.StyleDefault @@ -855,6 +867,7 @@ func (ui *ui) drawStatLine(nav *nav) { progress = append(progress, fmt.Sprintf("[%d/%d]", nav.deleteCount, nav.deleteTotal)) } + opts := getOptsMap() ruler := []string{} for _, s := range gOpts.ruler { switch s { @@ -875,8 +888,11 @@ func (ui *ui) drawStatLine(nav *nav) { } case "ind": ruler = append(ruler, fmt.Sprintf("%d/%d", ind, tot)) + default: + if val, ok := opts[s]; ok { + ruler = append(ruler, formatRulerOpt(s, val)) + } } - } ui.msgWin.printRight(ui.screen, 0, st, strings.Join(ruler, " "))