Skip to content

Commit

Permalink
Refactor filename truncation code (#1272)
Browse files Browse the repository at this point in the history
* Refactor filename truncation code

* Another refactor
  • Loading branch information
joelim-work authored Jun 4, 2023
1 parent 014d18d commit 7c33da2
Showing 1 changed file with 23 additions and 29 deletions.
52 changes: 23 additions & 29 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,49 +441,43 @@ 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

// leave space for displaying the tag
s = append(s, ' ')

if gOpts.icons {
s = append(s, []rune(dirStyle.icons.get(f))...)
s = append(s, ' ')
}

for _, r := range f.Name() {
s = append(s, r)
}

w := runeSliceWidth(s)
maxFilenameWidth := maxWidth - 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
info := fileInfo(f, dir)
showInfo := len(info) > 0 && 2*len(info) < maxWidth
if showInfo {
maxFilenameWidth -= len(info)
}

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, ' ')
}
filename := []rune(f.Name())
if runeSliceWidth(filename) > maxFilenameWidth {
filename = runeSliceWidthRange(filename, 0, maxFilenameWidth-1)
filename = append(filename, []rune(gOpts.truncatechar)...)
}
for i := runeSliceWidth(filename); i < maxFilenameWidth; i++ {
filename = append(filename, ' ')
}
s = append(s, filename...)

info := fileInfo(f, dir)

if len(info) > 0 && 2*len(info) < maxlength {
if w+len(info) > maxlength {
s = runeSliceWidthRange(s, 0, maxlength-len(info)-1)
s = append(s, []rune(gOpts.truncatechar)...)
} else {
s = runeSliceWidthRange(s, 0, maxlength-len(info))
}
for _, r := range info {
s = append(s, r)
}
if showInfo {
s = append(s, []rune(info)...)
}

ce := ""
Expand Down

0 comments on commit 7c33da2

Please sign in to comment.