Flexible filename truncation (#1029)

* allow flexible truncation of filenames

As of now, lf truncates always at the end. So for long filenames one can
not see the end (especially not the file ending, like .jpeg or .pdf).

[MidnightCommander](https://github.com/MidnightCommander/mc/) truncates
too long filenames in the middle, so it's possible to see the beginning
and the end of the too long filename.

This commit allows to specify a flexible point for the truncation in
percent of the available space.

* The default value of 100 for the new `truncatepct` option means no
change in lf behaviour.

* More adhering to naming convention.

* Improve error message for invalid `truncatepct` values.

* Remove redundant test cases.

* FIXUP Add dummy empty line to make `go fmt` happy.

* Avoid explicit "cast" to rune array.

* fix(doc.go): remove line with single dot

This was there to avoid that `go fmt` touches the next item with `waitmsg`.
However, removing the spaces before the example list items has the same
effect.
This commit is contained in:
raslop authored and GitHub committed 2023-07-03 12:48:51 +03:00
1 parent e885abe1a3
commit b1a851953b
9 files changed
+120 -1

No files matched your search

+1
View File
@@ -218,6 +218,7 @@ var (
"infotimefmtnew", "infotimefmtnew",
"infotimefmtold", "infotimefmtold",
"truncatechar", "truncatechar",
"truncatepct",
} }
) )
+18
View File
@@ -172,6 +172,7 @@ The following options can be used to customize the behavior of lf:
tempmarks string (default '') tempmarks string (default '')
timefmt string (default 'Mon Jan _2 15:04:05 2006') timefmt string (default 'Mon Jan _2 15:04:05 2006')
truncatechar string (default '~') truncatechar string (default '~')
truncatepct int (default 100)
waitmsg string (default 'Press any key to continue') waitmsg string (default 'Press any key to continue')
wrapscan bool (default true) wrapscan bool (default true)
wrapscroll bool (default false) wrapscroll bool (default false)
@@ -941,6 +942,23 @@ Format string of the file modification time shown in the bottom line.
Truncate character shown at the end when the file name does not fit to the pane. Truncate character shown at the end when the file name does not fit to the pane.
truncatepct int (default 100)
When a filename is too long to be shown completely, the available space is
partitioned in two pieces. truncatepct defines a fraction (in percent
between 0 and 100) for the size of the first piece, which will show the
beginning of the filename. The second piece will show the end of the filename
and will use the rest of the available space. Both pieces are separated by the
truncation character (truncatechar).
A value of 100 will only show the beginning of the filename,
while a value of 0 will only show the end of the filename, e.g.:
- `set truncatepct 100` -> "very-long-filename-tr~" (default)
- `set truncatepct 50` -> "very-long-f~-truncated"
- `set truncatepct 0` -> "~ng-filename-truncated"
waitmsg string (default 'Press any key to continue') waitmsg string (default 'Press any key to continue')
String shown after commands of shell-wait type. String shown after commands of shell-wait type.
+17
View File
@@ -175,6 +175,7 @@ The following options can be used to customize the behavior of lf:
tempmarks string (default '') tempmarks string (default '')
timefmt string (default 'Mon Jan _2 15:04:05 2006') timefmt string (default 'Mon Jan _2 15:04:05 2006')
truncatechar string (default '~') truncatechar string (default '~')
truncatepct int (default 100)
waitmsg string (default 'Press any key to continue') waitmsg string (default 'Press any key to continue')
wrapscan bool (default true) wrapscan bool (default true)
wrapscroll bool (default false) wrapscroll bool (default false)
@@ -1013,6 +1014,22 @@ Format string of the file modification time shown in the bottom line.
Truncate character shown at the end when the file name does not fit to the pane. Truncate character shown at the end when the file name does not fit to the pane.
truncatepct int (default 100)
When a filename is too long to be shown completely, the available space is
partitioned in two pieces. truncatepct defines a fraction (in percent between
0 and 100) for the size of the first piece, which will show the beginning of
the filename. The second piece will show the end of the filename and will use
the rest of the available space. Both pieces are separated by the truncation
character (truncatechar). A value of 100 will only show the beginning of the
filename, while a value of 0 will only show the end of the filename, e.g.:
- 'set truncatepct 100' -> "very-long-filename-tr~" (default)
- 'set truncatepct 50' -> "very-long-f~-truncated"
- 'set truncatepct 0' -> "~ng-filename-truncated"
waitmsg string (default 'Press any key to continue') waitmsg string (default 'Press any key to continue')
String shown after commands of shell-wait type. String shown after commands of shell-wait type.
+11
View File
@@ -831,6 +831,17 @@ func (e *setExpr) eval(app *app, args []string) {
} }
gOpts.truncatechar = e.val gOpts.truncatechar = e.val
case "truncatepct":
n, err := strconv.Atoi(e.val)
if err != nil {
app.ui.echoerrf("truncatepct: %s", err)
return
}
if n < 0 || n > 100 {
app.ui.echoerrf("truncatepct: must be between 0 and 100 (both inclusive), got %d", n)
return
}
gOpts.truncatepct = n
case "waitmsg": case "waitmsg":
gOpts.waitmsg = e.val gOpts.waitmsg = e.val
case "wrapscan": case "wrapscan":
+13
View File
@@ -191,6 +191,7 @@ The following options can be used to customize the behavior of lf:
tempmarks string (default '') tempmarks string (default '')
timefmt string (default 'Mon Jan _2 15:04:05 2006') timefmt string (default 'Mon Jan _2 15:04:05 2006')
truncatechar string (default '~') truncatechar string (default '~')
truncatepct int (default 100)
waitmsg string (default 'Press any key to continue') waitmsg string (default 'Press any key to continue')
wrapscan bool (default true) wrapscan bool (default true)
wrapscroll bool (default false) wrapscroll bool (default false)
@@ -1119,6 +1120,18 @@ Format string of the file modification time shown in the bottom line.
.PP .PP
Truncate character shown at the end when the file name does not fit to the pane. Truncate character shown at the end when the file name does not fit to the pane.
.PP .PP
.EX
truncatepct int (default 100)
.EE
.PP
When a filename is too long to be shown completely, the available space is partitioned in two pieces. truncatepct defines a fraction (in percent between 0 and 100) for the size of the first piece, which will show the beginning of the filename. The second piece will show the end of the filename and will use the rest of the available space. Both pieces are separated by the truncation character (truncatechar). A value of 100 will only show the beginning of the filename, while a value of 0 will only show the end of the filename, e.g.:
.PP
- `set truncatepct 100` -> "very-long-filename-tr~" (default)
.PP
- `set truncatepct 50` -> "very-long-f~-truncated"
.PP
- `set truncatepct 0` -> "~ng-filename-truncated"
.PP
.EX .EX
waitmsg string (default 'Press any key to continue') waitmsg string (default 'Press any key to continue')
.EE .EE
+13
View File
@@ -53,6 +53,19 @@ func runeSliceWidthRange(rs []rune, beg, end int) []rune {
return rs[b:] return rs[b:]
} }
// Returns the last runes of `rs` that take up at most `maxWidth` space.
func runeSliceWidthLastRange(rs []rune, maxWidth int) []rune {
lastWidth := 0
for i := len(rs) - 1; i >= 0; i-- {
w := runewidth.RuneWidth(rs[i])
if lastWidth+w > maxWidth {
return rs[i+1:]
}
lastWidth += w
}
return rs
}
// This function is used to escape whitespaces and special characters with // This function is used to escape whitespaces and special characters with
// backlashes in a given string. // backlashes in a given string.
func escape(s string) string { func escape(s string) string {
+41
View File
@@ -83,6 +83,47 @@ func TestRuneSliceWidthRange(t *testing.T) {
} }
} }
func TestRuneSliceWidthLastRange(t *testing.T) {
tests := []struct {
rs []rune
maxWidth int
exp []rune
}{
{[]rune{}, 0, []rune{}},
{[]rune{}, 1, []rune{}},
{[]rune{'a', 'ı', 'b', ' '}, 0, []rune{}},
{[]rune{'a', 'ı', 'b', ' '}, 1, []rune{' '}},
{[]rune{'a', 'ı', 'b', ' '}, 2, []rune{'b', ' '}},
{[]rune{'a', 'ı', 'b', ' '}, 3, []rune{'ı', 'b', ' '}},
{[]rune{'a', 'ı', 'b', ' '}, 4, []rune{'a', 'ı', 'b', ' '}},
{[]rune{'a', 'ı', 'b', ' '}, 5, []rune{'a', 'ı', 'b', ' '}},
{[]rune{'世', '界', '世', '界'}, 0, []rune{}},
{[]rune{'世', '界', '世', '界'}, 1, []rune{}},
{[]rune{'世', '界', '世', '界'}, 2, []rune{'界'}},
{[]rune{'世', '界', '世', '界'}, 3, []rune{'界'}},
{[]rune{'世', '界', '世', '界'}, 4, []rune{'世', '界'}},
{[]rune{'世', '界', '世', '界'}, 5, []rune{'世', '界'}},
{[]rune{'世', '界', '世', '界'}, 6, []rune{'界', '世', '界'}},
{[]rune{'世', '界', '世', '界'}, 7, []rune{'界', '世', '界'}},
{[]rune{'世', '界', '世', '界'}, 8, []rune{'世', '界', '世', '界'}},
{[]rune{'世', '界', '世', '界'}, 9, []rune{'世', '界', '世', '界'}},
{[]rune{'世', 'a', '界', 'ı'}, 0, []rune{}},
{[]rune{'世', 'a', '界', 'ı'}, 1, []rune{'ı'}},
{[]rune{'世', 'a', '界', 'ı'}, 2, []rune{'ı'}},
{[]rune{'世', 'a', '界', 'ı'}, 3, []rune{'界', 'ı'}},
{[]rune{'世', 'a', '界', 'ı'}, 4, []rune{'a', '界', 'ı'}},
{[]rune{'世', 'a', '界', 'ı'}, 5, []rune{'a', '界', 'ı'}},
{[]rune{'世', 'a', '界', 'ı'}, 6, []rune{'世', 'a', '界', 'ı'}},
{[]rune{'世', 'a', '界', 'ı'}, 7, []rune{'世', 'a', '界', 'ı'}},
}
for _, test := range tests {
if got := runeSliceWidthLastRange(test.rs, test.maxWidth); !reflect.DeepEqual(got, test.exp) {
t.Errorf("at input '%v' expected '%v' but got '%v'", test.rs, test.exp, got)
}
}
}
func TestEscape(t *testing.T) { func TestEscape(t *testing.T) {
tests := []struct { tests := []struct {
s string s string
+2
View File
@@ -72,6 +72,7 @@ var gOpts struct {
infotimefmtnew string infotimefmtnew string
infotimefmtold string infotimefmtold string
truncatechar string truncatechar string
truncatepct int
ratios []int ratios []int
hiddenfiles []string hiddenfiles []string
history bool history bool
@@ -133,6 +134,7 @@ func init() {
gOpts.infotimefmtnew = "Jan _2 15:04" gOpts.infotimefmtnew = "Jan _2 15:04"
gOpts.infotimefmtold = "Jan _2 2006" gOpts.infotimefmtold = "Jan _2 2006"
gOpts.truncatechar = "~" gOpts.truncatechar = "~"
gOpts.truncatepct = 100
gOpts.ratios = []int{1, 2, 3} gOpts.ratios = []int{1, 2, 3}
gOpts.hiddenfiles = []string{".*"} gOpts.hiddenfiles = []string{".*"}
gOpts.history = true gOpts.history = true
+4 -1
View File
@@ -468,8 +468,11 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, context *dirContext, dir
filename := []rune(f.Name()) filename := []rune(f.Name())
if runeSliceWidth(filename) > maxFilenameWidth { if runeSliceWidth(filename) > maxFilenameWidth {
filename = runeSliceWidthRange(filename, 0, maxFilenameWidth-1) truncatePos := (maxFilenameWidth - 1) * gOpts.truncatepct / 100
lastPart := runeSliceWidthLastRange(filename, maxFilenameWidth-truncatePos-1)
filename = runeSliceWidthRange(filename, 0, truncatePos)
filename = append(filename, []rune(gOpts.truncatechar)...) filename = append(filename, []rune(gOpts.truncatechar)...)
filename = append(filename, lastPart...)
} }
for i := runeSliceWidth(filename); i < maxFilenameWidth; i++ { for i := runeSliceWidth(filename); i < maxFilenameWidth; i++ {
filename = append(filename, ' ') filename = append(filename, ' ')