Refactor filename truncation code (#1272)

* Refactor filename truncation code

* Another refactor
This commit is contained in:
Joe Lim authored and GitHub committed 2023-06-04 16:25:50 +03:00
1 parent 014d18d3ea
commit 7c33da2bc2
1 file changed
+23 -29
+23 -29
View File
@@ -441,8 +441,16 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, context *dirContext, dir
} }
} }
// make space for select marker, and leave another space at the end
maxWidth := win.w - lnwidth - 2
// make extra space to separate windows if drawbox is not enabled
if !gOpts.drawbox {
maxWidth -= 1
}
var s []rune var s []rune
// leave space for displaying the tag
s = append(s, ' ') s = append(s, ' ')
if gOpts.icons { if gOpts.icons {
@@ -450,40 +458,26 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, context *dirContext, dir
s = append(s, ' ') s = append(s, ' ')
} }
for _, r := range f.Name() { maxFilenameWidth := maxWidth - runeSliceWidth(s)
s = append(s, r)
}
w := runeSliceWidth(s)
// make space for select marker, and leave another space at the end
maxlength := win.w - lnwidth - 2
// make extra space to separate windows if drawbox is not enabled
if !gOpts.drawbox {
maxlength -= 1
}
if w > maxlength {
s = runeSliceWidthRange(s, 0, maxlength-1)
s = append(s, []rune(gOpts.truncatechar)...)
} else {
for i := 0; i < maxlength-w; i++ {
s = append(s, ' ')
}
}
info := fileInfo(f, dir) info := fileInfo(f, dir)
showInfo := len(info) > 0 && 2*len(info) < maxWidth
if showInfo {
maxFilenameWidth -= len(info)
}
if len(info) > 0 && 2*len(info) < maxlength { filename := []rune(f.Name())
if w+len(info) > maxlength { if runeSliceWidth(filename) > maxFilenameWidth {
s = runeSliceWidthRange(s, 0, maxlength-len(info)-1) filename = runeSliceWidthRange(filename, 0, maxFilenameWidth-1)
s = append(s, []rune(gOpts.truncatechar)...) filename = append(filename, []rune(gOpts.truncatechar)...)
} else {
s = runeSliceWidthRange(s, 0, maxlength-len(info))
} }
for _, r := range info { for i := runeSliceWidth(filename); i < maxFilenameWidth; i++ {
s = append(s, r) filename = append(filename, ' ')
} }
s = append(s, filename...)
if showInfo {
s = append(s, []rune(info)...)
} }
ce := "" ce := ""