Prevent truncation from applying to file extension (#2159)

* Prevent truncation from applying to file extension

* Update documentation
This commit is contained in:
Joe Lim authored and GitHub committed 2025-09-19 18:32:06 +10:00
1 parent f69aec80b1
commit 5e15655539
6 files changed
+122 -42

No files matched your search

+10 -11
View File
@@ -1120,20 +1120,19 @@ The truncate character that is shown at the end when the filename does not fit i
## truncatepct (int) (default 100)
When a filename is too long to be shown completely, the available space is
partitioned into 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.:
When a filename is too long to be shown completely, the available space will be partitioned into two parts.
`truncatepct` is a percentage value between 0 and 100 that determines the size of the first part, which will be shown at the beginning of the filename.
The second part uses the rest of the available space, and will be shown at the end of the filename.
Both parts are separated by the truncation character (`truncatechar`).
Truncation is not applied to the file extension.
- `set truncatepct 100` -> `very-long-filename-tr~` (default)
For example, with the filename `very_long_filename.txt`:
- `set truncatepct 50` -> `very-long-f~-truncated`
- `set truncatepct 100` -> `very_long_filena~.txt` (default)
- `set truncatepct 0` -> `~ng-filename-truncated`
- `set truncatepct 50` -> `very_lon~filename.txt`
- `set truncatepct 0` -> `~ry_long_filename.txt`
## visualfmt (string) (default `\033[7;36m`)
+11 -10
View File
@@ -1205,19 +1205,20 @@ not fit into the pane.
truncatepct (int) (default 100)
When a filename is too long to be shown completely, the available space
is partitioned into 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.:
will be partitioned into two parts. truncatepct is a percentage value
between 0 and 100 that determines the size of the first part, which will
be shown at the beginning of the filename. The second part uses the rest
of the available space, and will be shown at the end of the filename.
Both parts are separated by the truncation character (truncatechar).
Truncation is not applied to the file extension.
- set truncatepct 100 -> very-long-filename-tr~ (default)
For example, with the filename very_long_filename.txt:
- set truncatepct 50 -> very-long-f~-truncated
- set truncatepct 100 -> very_long_filena~.txt (default)
- set truncatepct 0 -> ~ng-filename-truncated
- set truncatepct 50 -> very_lon~filename.txt
- set truncatepct 0 -> ~ry_long_filename.txt
visualfmt (string) (default \033[7;36m)
+15 -13
View File
@@ -1051,22 +1051,24 @@ The truncate character that is shown at the end when the filename does
not fit into the pane.
.SS truncatepct (int) (default 100)
When a filename is too long to be shown completely, the available space
is partitioned into 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.:
will be partitioned into two parts.
\f[CR]truncatepct\f[R] is a percentage value between 0 and 100 that
determines the size of the first part, which will be shown at the
beginning of the filename.
The second part uses the rest of the available space, and will be shown
at the end of the filename.
Both parts are separated by the truncation character
(\f[CR]truncatechar\f[R]).
Truncation is not applied to the file extension.
.PP
For example, with the filename \f[CR]very_long_filename.txt\f[R]:
.IP \(bu 2
\f[CR]set truncatepct 100\f[R] \->
\f[CR]very\-long\-filename\-tr\(ti\f[R] (default)
\f[CR]set truncatepct 100\f[R] \-> \f[CR]very_long_filena\(ti.txt\f[R]
(default)
.IP \(bu 2
\f[CR]set truncatepct 50\f[R] \->
\f[CR]very\-long\-f\(ti\-truncated\f[R]
\f[CR]set truncatepct 50\f[R] \-> \f[CR]very_lon\(tifilename.txt\f[R]
.IP \(bu 2
\f[CR]set truncatepct 0\f[R] \-> \f[CR]\(ting\-filename\-truncated\f[R]
\f[CR]set truncatepct 0\f[R] \-> \f[CR]\(tiry_long_filename.txt\f[R]
.SS visualfmt (string) (default \f[CR]\(rs033[7;36m\f[R])
Format string of the indicator for files that are visually selected.
.SS waitmsg (string) (default \f[CR]Press any key to continue\f[R])
+30
View File
@@ -347,6 +347,36 @@ func getFileExtension(file fs.FileInfo) string {
return filepath.Ext(file.Name())
}
// This function truncates a filename at a given position.
// The position is specified as percentage indicating where the truncation
// character will appear (0 means left, 50 means middle, 100 means right).
// The file extension is not affected by truncation, however it will be clipped
// if it exceeds the allowed width.
func truncateFilename(file fs.FileInfo, maxWidth int, truncatePct int, truncateChar rune) string {
filename := file.Name()
if runeSliceWidth([]rune(filename)) <= maxWidth {
return filename
}
ext := getFileExtension(file)
avail := maxWidth - runewidth.RuneWidth(truncateChar) - runeSliceWidth([]rune(ext))
if avail < 0 {
result := append([]rune{truncateChar}, []rune(ext)...)
return string(runeSliceWidthRange(result, 0, maxWidth))
}
basename := []rune(strings.TrimSuffix(filename, ext))
left := runeSliceWidthRange(basename, 0, avail*truncatePct/100)
right := runeSliceWidthLastRange(basename, avail-runeSliceWidth(left))
var result []rune
result = append(result, left...)
result = append(result, truncateChar)
result = append(result, right...)
result = append(result, []rune(ext)...)
return string(result)
}
// This function deletes entries from a map if the key is either the given path
// or a subpath of it.
// This is useful for clearing cached data when a directory is moved or deleted.
+55
View File
@@ -410,6 +410,61 @@ func TestGetFileExtension(t *testing.T) {
}
}
func TestTruncateFilename(t *testing.T) {
tests := []struct {
file fakeFileInfo
maxWidth int
truncatePct int
exp string
}{
{fakeFileInfo{"foo", false}, 0, 0, ""},
{fakeFileInfo{"foo", false}, 0, 100, ""},
{fakeFileInfo{"foo", false}, 1, 0, "~"},
{fakeFileInfo{"foo", false}, 1, 100, "~"},
{fakeFileInfo{"foo", false}, 2, 0, "~o"},
{fakeFileInfo{"foo", false}, 2, 100, "f~"},
{fakeFileInfo{"foo", false}, 3, 0, "foo"},
{fakeFileInfo{"foo", false}, 3, 100, "foo"},
{fakeFileInfo{"foo", false}, 4, 0, "foo"},
{fakeFileInfo{"foo", false}, 4, 100, "foo"},
{fakeFileInfo{"foo.txt", false}, 0, 0, ""},
{fakeFileInfo{"foo.txt", false}, 0, 100, ""},
{fakeFileInfo{"foo.txt", false}, 1, 0, "~"},
{fakeFileInfo{"foo.txt", false}, 1, 100, "~"},
{fakeFileInfo{"foo.txt", false}, 2, 0, "~."},
{fakeFileInfo{"foo.txt", false}, 2, 100, "~."},
{fakeFileInfo{"foo.txt", false}, 3, 0, "~.t"},
{fakeFileInfo{"foo.txt", false}, 3, 100, "~.t"},
{fakeFileInfo{"foo.txt", false}, 4, 0, "~.tx"},
{fakeFileInfo{"foo.txt", false}, 4, 100, "~.tx"},
{fakeFileInfo{"foo.txt", false}, 5, 0, "~.txt"},
{fakeFileInfo{"foo.txt", false}, 5, 100, "~.txt"},
{fakeFileInfo{"foo.txt", false}, 6, 0, "~o.txt"},
{fakeFileInfo{"foo.txt", false}, 6, 100, "f~.txt"},
{fakeFileInfo{"foobarbaz", false}, 7, 0, "~barbaz"},
{fakeFileInfo{"foobarbaz", false}, 7, 50, "foo~baz"},
{fakeFileInfo{"foobarbaz", false}, 7, 100, "foobar~"},
{fakeFileInfo{"foobarbaz.txt", false}, 11, 0, "~barbaz.txt"},
{fakeFileInfo{"foobarbaz.txt", false}, 11, 50, "foo~baz.txt"},
{fakeFileInfo{"foobarbaz.txt", false}, 11, 100, "foobar~.txt"},
{fakeFileInfo{"foobarbaz.d", true}, 9, 0, "~barbaz.d"},
{fakeFileInfo{"foobarbaz.d", true}, 9, 50, "foob~az.d"},
{fakeFileInfo{"foobarbaz.d", true}, 9, 100, "foobarba~"},
{fakeFileInfo{"世界世界.txt", false}, 10, 0, "~世界.txt"},
{fakeFileInfo{"世界世界.txt", false}, 10, 50, "世~界.txt"},
{fakeFileInfo{"世界世界.txt", false}, 10, 100, "世界~.txt"},
{fakeFileInfo{"世界世界.txt", false}, 11, 0, "~界世界.txt"},
{fakeFileInfo{"世界世界.txt", false}, 11, 50, "世~世界.txt"},
{fakeFileInfo{"世界世界.txt", false}, 11, 100, "世界世~.txt"},
}
for _, test := range tests {
if got := truncateFilename(test.file, test.maxWidth, test.truncatePct, '~'); got != test.exp {
t.Errorf("at input (%v, %v, %v) expected '%s' but got '%s'", test.file, test.maxWidth, test.truncatePct, test.exp, got)
}
}
}
func TestOptionToFmtstr(t *testing.T) {
tests := []struct {
s string
+1 -8
View File
@@ -499,14 +499,7 @@ func (win *win) printDir(ui *ui, dir *dir, context *dirContext, dirStyle *dirSty
maxFilenameWidth -= infolen
}
filename := []rune(f.Name())
if runeSliceWidth(filename) > maxFilenameWidth {
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, lastPart...)
}
filename := []rune(truncateFilename(f, maxFilenameWidth, gOpts.truncatepct, []rune(gOpts.truncatechar)[0]))
for j := runeSliceWidth(filename); j < maxFilenameWidth; j++ {
filename = append(filename, ' ')
}