Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code for printing directories #1150

Merged
merged 1 commit into from
Mar 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 24 additions & 31 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,12 +444,9 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, context *dirContext, dir

s = append(s, ' ')

var iwidth int

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

for _, r := range f.Name() {
Expand All @@ -458,23 +455,30 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, context *dirContext, dir

w := runeSliceWidth(s)

if w > win.w-3 {
s = runeSliceWidthRange(s, 0, win.w-4)
// 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 < win.w-3-w; i++ {
for i := 0; i < maxlength-w; i++ {
s = append(s, ' ')
}
}

info := fileInfo(f, dir)

if len(info) > 0 && win.w-lnwidth-iwidth-2 > 2*len(info) {
if win.w-2 > w+len(info) {
s = runeSliceWidthRange(s, 0, win.w-3-len(info)-lnwidth)
} else {
s = runeSliceWidthRange(s, 0, win.w-4-len(info)-lnwidth)
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)
Expand Down Expand Up @@ -518,17 +522,17 @@ func getWidths(wtot int) []int {
wlen := len(gOpts.ratios)
widths := make([]int, wlen)

if gOpts.drawbox {
wtot -= (wlen + 1)
}

wsum := 0
for i := 0; i < wlen-1; i++ {
widths[i] = gOpts.ratios[i] * wtot / rsum
wsum += widths[i]
}
widths[wlen-1] = wtot - wsum

if gOpts.drawbox {
widths[wlen-1]--
}

return widths
}

Expand All @@ -543,7 +547,8 @@ func getWins(screen tcell.Screen) []*win {
wlen := len(widths)
for i := 0; i < wlen; i++ {
if gOpts.drawbox {
wins = append(wins, newWin(widths[i], htot-4, wacc+1, 2))
wacc++
wins = append(wins, newWin(widths[i], htot-4, wacc, 2))
} else {
wins = append(wins, newWin(widths[i], htot-2, wacc, 1))
}
Expand Down Expand Up @@ -627,21 +632,9 @@ func (ui *ui) pollEvents() {
}

func (ui *ui) renew() {
wtot, htot := ui.screen.Size()

widths := getWidths(wtot)

wacc := 0
wlen := len(widths)
for i := 0; i < wlen; i++ {
if gOpts.drawbox {
ui.wins[i].renew(widths[i], htot-4, wacc+1, 2)
} else {
ui.wins[i].renew(widths[i], htot-2, wacc, 1)
}
wacc += widths[i]
}
ui.wins = getWins(ui.screen)

wtot, htot := ui.screen.Size()
ui.promptWin.renew(wtot, 1, 0, 0)
ui.msgWin.renew(wtot, 1, 0, htot-1)
ui.menuWin.renew(wtot, 1, 0, htot-2)
Expand Down Expand Up @@ -892,7 +885,7 @@ func (ui *ui) drawBox() {

wacc := 0
for wind := 0; wind < len(ui.wins)-1; wind++ {
wacc += ui.wins[wind].w
wacc += ui.wins[wind].w + 1
ui.screen.SetContent(wacc, 1, '┬', nil, st)
for i := 2; i < h-2; i++ {
ui.screen.SetContent(wacc, i, '│', nil, st)
Expand Down