From 44a8624329edd97b24cdbf4610f93e77949995d6 Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Thu, 19 Jan 2023 12:13:32 -0800 Subject: [PATCH] Simplify configuration of several formatting options This simplifies the configuration of `cursorfmt`, `cursorpreviewfmt`, `tagfmt`, and `errorfmt` options. In almost all usecases, the value of these options had to end with `%s\033[0m` to print the contents of filename/tag/error and reset the terminal style. Now, if `%s` is not part of the option's value, `%s\033[0m` is appended automatically. This simplifies configuration. I retained the same behavior as before when `%s` is part of the string for backwards compatibility. I think it should be considered deprecated, but it also causes no harm. --- doc.go | 27 ++++++++++++++++++--------- docstring.go | 35 +++++++++++++++++++++++------------ etc/lfrc.example | 2 +- lf.1 | 24 +++++++++++++++--------- opts.go | 8 ++++---- ui.go | 19 ++++++++++++++----- 6 files changed, 75 insertions(+), 40 deletions(-) diff --git a/doc.go b/doc.go index 396883e6..cbb20d1e 100644 --- a/doc.go +++ b/doc.go @@ -114,15 +114,15 @@ The following options can be used to customize the behavior of lf: anchorfind bool (default on) autoquit bool (default off) cleaner string (default '') - cursorfmt string (default "\033[7m%s\033[0m") - cursorpreviewfmt string (default "\033[4m%s\033[0m") + cursorfmt string (default "\033[7m") + cursorpreviewfmt string (default "\033[4m") dircache bool (default on) dircounts bool (default off) dirfirst bool (default on) dironly bool (default off) dirpreviews bool (default off) drawbox bool (default off) - errorfmt string (default "\033[7;31;47m%s\033[0m") + errorfmt string (default "\033[7;31;47m") filesep string (default "\n") findlen int (default 1) globsearch bool (default off) @@ -156,7 +156,7 @@ The following options can be used to customize the behavior of lf: smartdia bool (default off) sortby string (default 'natural') tabstop int (default 8) - tagfmt string (default "\033[31m%s\033[0m") + tagfmt string (default "\033[31m") tempmarks string (default '') timefmt string (default 'Mon Jan _2 15:04:05 2006') truncatechar string (default '~') @@ -608,15 +608,18 @@ This file is called if previewing is enabled, the previewer is set, and the prev Five arguments are passed to the file, (1) current file name, (2) width, (3) height, (4) horizontal position, and (5) vertical position of preview pane respectively. Preview clearing is disabled when the value of this option is left empty. - cursorfmt string (default "\033[7m%s\033[0m") - cursorpreviewfmt string (default "\033[4m%s\033[0m") + cursorfmt string (default "\033[7m") + cursorpreviewfmt string (default "\033[4m") Format strings for highlighting the cursor. `cursorpreviewfmt` applies in panes that preview directories, and `cursorfmt` applies in all other panes. The default is to make the normal cursor inverted and the preview cursor underlined. -Some other possibilities to consider for the preview cursor: "%s" for no cursor, "\033[7;2m%s\044[0m" for dimmed inverted text (visibility varies by terminal), "\033[7;90m%s\044[0m" for inverted text with grey (aka "brightblack") background. +Some other possibilities to consider for the preview cursor: an empty string for no cursor, "\033[7;2m" for dimmed inverted text (visibility varies by terminal), "\033[7;90m" for inverted text with grey (aka "brightblack") background. + +If the format string contains the characters `%s`, it is interpreted as a format string for `fmt.Sprintf`. Such a string should end with the terminal reset sequence. +For example, "\033[4m%s\033[0m" has the same effect as "\033[4m". dircache bool (default on) @@ -646,10 +649,13 @@ Show only directories. Draw boxes around panes with box drawing characters. - errorfmt string (default "\033[7;31;47m%s\033[0m") + errorfmt string (default "\033[7;31;47m") Format string of error messages shown in the bottom message line. +If the format string contains the characters `%s`, it is interpreted as a format string for `fmt.Sprintf`. Such a string should end with the terminal reset sequence. +For example, "\033[4m%s\033[0m" has the same effect as "\033[4m". + filesep string (default "\n") File separator used in environment variables 'fs' and 'fx'. @@ -824,10 +830,13 @@ Currently supported sort types are 'natural', 'name', 'size', 'time', 'ctime', ' Number of space characters to show for horizontal tabulation (U+0009) character. - tagfmt string (default "\033[31m%s\033[0m") + tagfmt string (default "\033[31m") Format string of the tags. +If the format string contains the characters `%s`, it is interpreted as a format string for `fmt.Sprintf`. Such a string should end with the terminal reset sequence. +For example, "\033[4m%s\033[0m" has the same effect as "\033[4m". + tempmarks string (default '') Marks to be considered temporary (e.g. 'abc' refers to marks 'a', 'b', and 'c'). diff --git a/docstring.go b/docstring.go index 91cf57da..17ef3f2c 100644 --- a/docstring.go +++ b/docstring.go @@ -117,15 +117,15 @@ The following options can be used to customize the behavior of lf: anchorfind bool (default on) autoquit bool (default off) cleaner string (default '') - cursorfmt string (default "\033[7m%s\033[0m") - cursorpreviewfmt string (default "\033[4m%s\033[0m") + cursorfmt string (default "\033[7m") + cursorpreviewfmt string (default "\033[4m") dircache bool (default on) dircounts bool (default off) dirfirst bool (default on) dironly bool (default off) dirpreviews bool (default off) drawbox bool (default off) - errorfmt string (default "\033[7;31;47m%s\033[0m") + errorfmt string (default "\033[7;31;47m") filesep string (default "\n") findlen int (default 1) globsearch bool (default off) @@ -159,7 +159,7 @@ The following options can be used to customize the behavior of lf: smartdia bool (default off) sortby string (default 'natural') tabstop int (default 8) - tagfmt string (default "\033[31m%s\033[0m") + tagfmt string (default "\033[31m") tempmarks string (default '') timefmt string (default 'Mon Jan _2 15:04:05 2006') truncatechar string (default '~') @@ -634,8 +634,8 @@ file, (1) current file name, (2) width, (3) height, (4) horizontal position, and (5) vertical position of preview pane respectively. Preview clearing is disabled when the value of this option is left empty. - cursorfmt string (default "\033[7m%s\033[0m") - cursorpreviewfmt string (default "\033[4m%s\033[0m") + cursorfmt string (default "\033[7m") + cursorpreviewfmt string (default "\033[4m") Format strings for highlighting the cursor. 'cursorpreviewfmt' applies in panes that preview directories, and 'cursorfmt' applies in all other panes. @@ -643,10 +643,13 @@ that preview directories, and 'cursorfmt' applies in all other panes. The default is to make the normal cursor inverted and the preview cursor underlined. -Some other possibilities to consider for the preview cursor: "%s" for no cursor, -"\033[7;2m%s\044[0m" for dimmed inverted text (visibility varies by terminal), -"\033[7;90m%s\044[0m" for inverted text with grey (aka "brightblack") -background. +Some other possibilities to consider for the preview cursor: an empty string for +no cursor, "\033[7;2m" for dimmed inverted text (visibility varies by terminal), +"\033[7;90m" for inverted text with grey (aka "brightblack") background. + +If the format string contains the characters '%s', it is interpreted as a format +string for 'fmt.Sprintf'. Such a string should end with the terminal reset +sequence. For example, "\033[4m%s\033[0m" has the same effect as "\033[4m". dircache bool (default on) @@ -680,10 +683,14 @@ Show only directories. Draw boxes around panes with box drawing characters. - errorfmt string (default "\033[7;31;47m%s\033[0m") + errorfmt string (default "\033[7;31;47m") Format string of error messages shown in the bottom message line. +If the format string contains the characters '%s', it is interpreted as a format +string for 'fmt.Sprintf'. Such a string should end with the terminal reset +sequence. For example, "\033[4m%s\033[0m" has the same effect as "\033[4m". + filesep string (default "\n") File separator used in environment variables 'fs' and 'fx'. @@ -874,10 +881,14 @@ Sort type for directories. Currently supported sort types are 'natural', 'name', Number of space characters to show for horizontal tabulation (U+0009) character. - tagfmt string (default "\033[31m%s\033[0m") + tagfmt string (default "\033[31m") Format string of the tags. +If the format string contains the characters '%s', it is interpreted as a format +string for 'fmt.Sprintf'. Such a string should end with the terminal reset +sequence. For example, "\033[4m%s\033[0m" has the same effect as "\033[4m". + tempmarks string (default '') Marks to be considered temporary (e.g. 'abc' refers to marks 'a', 'b', diff --git a/etc/lfrc.example b/etc/lfrc.example index a92d4139..18bf621c 100644 --- a/etc/lfrc.example +++ b/etc/lfrc.example @@ -21,7 +21,7 @@ set ifs "\n" set scrolloff 10 # Use the `dim` attribute instead of underline for the cursor in the preview pane -set cursorpreviewfmt "\033[7;2m%s\033[0m" +set cursorpreviewfmt "\033[7;2m" # use enter for shell commands map shell diff --git a/lf.1 b/lf.1 index 53f4d944..3b3eed6f 100644 --- a/lf.1 +++ b/lf.1 @@ -133,15 +133,15 @@ The following options can be used to customize the behavior of lf: anchorfind bool (default on) autoquit bool (default off) cleaner string (default '') - cursorfmt string (default "\e033[7m%s\e033[0m") - cursorpreviewfmt string (default "\e033[4m%s\e033[0m") + cursorfmt string (default "\e033[7m") + cursorpreviewfmt string (default "\e033[4m") dircache bool (default on) dircounts bool (default off) dirfirst bool (default on) dironly bool (default off) dirpreviews bool (default off) drawbox bool (default off) - errorfmt string (default "\e033[7;31;47m%s\e033[0m") + errorfmt string (default "\e033[7;31;47m") filesep string (default "\en") findlen int (default 1) globsearch bool (default off) @@ -175,7 +175,7 @@ The following options can be used to customize the behavior of lf: smartdia bool (default off) sortby string (default 'natural') tabstop int (default 8) - tagfmt string (default "\e033[31m%s\e033[0m") + tagfmt string (default "\e033[31m") tempmarks string (default '') timefmt string (default 'Mon Jan _2 15:04:05 2006') truncatechar string (default '~') @@ -750,15 +750,17 @@ Automatically quit server when there are no clients left connected. Set the path of a cleaner file. The file should be executable. This file is called if previewing is enabled, the previewer is set, and the previously selected file had its preview cache disabled. Five arguments are passed to the file, (1) current file name, (2) width, (3) height, (4) horizontal position, and (5) vertical position of preview pane respectively. Preview clearing is disabled when the value of this option is left empty. .PP .EX - cursorfmt string (default "\e033[7m%s\e033[0m") - cursorpreviewfmt string (default "\e033[4m%s\e033[0m") + cursorfmt string (default "\e033[7m") + cursorpreviewfmt string (default "\e033[4m") .EE .PP Format strings for highlighting the cursor. `cursorpreviewfmt` applies in panes that preview directories, and `cursorfmt` applies in all other panes. .PP The default is to make the normal cursor inverted and the preview cursor underlined. .PP -Some other possibilities to consider for the preview cursor: "%s" for no cursor, "\e033[7;2m%s\e044[0m" for dimmed inverted text (visibility varies by terminal), "\e033[7;90m%s\e044[0m" for inverted text with grey (aka "brightblack") background. +Some other possibilities to consider for the preview cursor: an empty string for no cursor, "\e033[7;2m" for dimmed inverted text (visibility varies by terminal), "\e033[7;90m" for inverted text with grey (aka "brightblack") background. +.PP +If the format string contains the characters `%s`, it is interpreted as a format string for `fmt.Sprintf`. Such a string should end with the terminal reset sequence. For example, "\e033[4m%s\e033[0m" has the same effect as "\e033[4m". .PP .EX dircache bool (default on) @@ -797,11 +799,13 @@ Show only directories. Draw boxes around panes with box drawing characters. .PP .EX - errorfmt string (default "\e033[7;31;47m%s\e033[0m") + errorfmt string (default "\e033[7;31;47m") .EE .PP Format string of error messages shown in the bottom message line. .PP +If the format string contains the characters `%s`, it is interpreted as a format string for `fmt.Sprintf`. Such a string should end with the terminal reset sequence. For example, "\e033[4m%s\e033[0m" has the same effect as "\e033[4m". +.PP .EX filesep string (default "\en") .EE @@ -1001,11 +1005,13 @@ Sort type for directories. Currently supported sort types are 'natural', 'name', Number of space characters to show for horizontal tabulation (U+0009) character. .PP .EX - tagfmt string (default "\e033[31m%s\e033[0m") + tagfmt string (default "\e033[31m") .EE .PP Format string of the tags. .PP +If the format string contains the characters `%s`, it is interpreted as a format string for `fmt.Sprintf`. Such a string should end with the terminal reset sequence. For example, "\e033[4m%s\e033[0m" has the same effect as "\e033[4m". +.PP .EX tempmarks string (default '') .EE diff --git a/opts.go b/opts.go index b661ee76..dd33594c 100644 --- a/opts.go +++ b/opts.go @@ -91,8 +91,8 @@ func init() { gOpts.dironly = false gOpts.dirpreviews = false gOpts.drawbox = false - gOpts.cursorfmt = "\033[7m%s\033[0m" - gOpts.cursorpreviewfmt = "\033[4m%s\033[0m" + gOpts.cursorfmt = "\033[7m" + gOpts.cursorpreviewfmt = "\033[4m" gOpts.globsearch = false gOpts.icons = false gOpts.ignorecase = true @@ -112,7 +112,7 @@ func init() { gOpts.period = 0 gOpts.scrolloff = 0 gOpts.tabstop = 8 - gOpts.errorfmt = "\033[7;31;47m%s\033[0m" + gOpts.errorfmt = "\033[7;31;47m" gOpts.filesep = "\n" gOpts.ifs = "" gOpts.previewer = "" @@ -132,7 +132,7 @@ func init() { gOpts.shellopts = nil gOpts.sortType = sortType{naturalSort, dirfirstSort} gOpts.tempmarks = "'" - gOpts.tagfmt = "\033[31m%s\033[0m" + gOpts.tagfmt = "\033[31m" gOpts.keys = make(map[string]expr) diff --git a/ui.go b/ui.go index 3f8a2484..440e1a9f 100644 --- a/ui.go +++ b/ui.go @@ -473,14 +473,15 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, context *dirContext, dir } } - cursorescapefmt := "%s" + ce := "" if i == dir.pos { if dirStyle.previewing { - cursorescapefmt = gOpts.cursorpreviewfmt + ce = gOpts.cursorpreviewfmt } else { - cursorescapefmt = gOpts.cursorfmt + ce = gOpts.cursorfmt } } + cursorescapefmt := optionToFmtstr(ce) s = append(s, ' ') styledFilename := fmt.Sprintf(cursorescapefmt, string(s)) @@ -491,7 +492,7 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, context *dirContext, dir if i == dir.pos { win.print(screen, lnwidth+1, i, st, fmt.Sprintf(cursorescapefmt, tag)) } else { - win.print(screen, lnwidth+1, i, tcell.StyleDefault, fmt.Sprintf(gOpts.tagfmt, tag)) + win.print(screen, lnwidth+1, i, tcell.StyleDefault, fmt.Sprintf(optionToFmtstr(gOpts.tagfmt), tag)) } } } @@ -657,8 +658,16 @@ func (ui *ui) echomsg(msg string) { log.Print(msg) } +func optionToFmtstr(optstr string) string { + if !strings.Contains(optstr, "%s") { + return optstr + "%s\033[0m" + } else { + return optstr + } +} + func (ui *ui) echoerr(msg string) { - ui.msg = fmt.Sprintf(gOpts.errorfmt, msg) + ui.msg = fmt.Sprintf(optionToFmtstr(gOpts.errorfmt), msg) log.Printf("error: %s", msg) }