fix: improve width calculation for ratios option (#2347)

* fix: improve width calculation for `ratios` option

* Update `CHANGELOG.md`

* round window ends instead of floor

* grammar
This commit is contained in:
Joe Lim authored and GitHub committed 2026-01-14 11:13:06 +11:00
1 parent 48689f9eab
commit b5d981ad4a
4 files changed
+74 -39

No files matched your search

+1
View File
@@ -32,6 +32,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
- Newline characters are now ignored when drawing the ruler with the `ruler` file configured (#2319).
- A potential crash when using the `scroll-up`/`scroll-down` commands is now fixed (#2320).
- Case-insensitive command-line completions no longer cause user input to be displayed in lowercase (#2336).
- Calculation of window widths for the `ratios` option is now more accurate (#2347).
## [r40](https://github.com/gokcehan/lf/releases/tag/r40)
+33
View File
@@ -513,6 +513,39 @@ func readLines(reader io.ByteReader, maxLines int) (lines []string, binary bool,
}
}
// This function calculates the widths of windows as the result of applying the
// `ratios` option to the screen width. One column is allocated for each divider
// between windows. The `drawbox` option requires an additional two columns to
// draw the left and right borders.
func getWidths(wtot int, ratios []int, drawbox bool) []int {
rlen := len(ratios)
wtot -= rlen - 1
if drawbox {
wtot -= 2
}
wtot = max(wtot, 0)
rtot := 0
for _, r := range ratios {
rtot += r
}
divround := func(x, y int) int {
return (x + y/2) / y
}
widths := make([]int, rlen)
rsum := 0
wsum := 0
for i, r := range ratios {
rsum += r
widths[i] = divround(wtot*rsum, rtot) - wsum
wsum += widths[i]
}
return widths
}
// We don't need no generic code
// We don't need no type control
// No dark templates in compiler
+27
View File
@@ -559,3 +559,30 @@ func TestReadLines(t *testing.T) {
}
}
}
func TestGetWidths(t *testing.T) {
tests := []struct {
wtot int
ratios []int
drawbox bool
exp []int
}{
{0, []int{1}, false, []int{0}},
{0, []int{1}, true, []int{0}},
{0, []int{1, 3, 2}, false, []int{0, 0, 0}},
{0, []int{1, 3, 2}, true, []int{0, 0, 0}},
{14, []int{1, 3, 2}, false, []int{2, 6, 4}},
{16, []int{1, 3, 2}, true, []int{2, 6, 4}},
{23, []int{1, 3, 2, 4}, false, []int{2, 6, 4, 8}}, // windows end at 2.0, 8.0, 12.0, 20.0 respectively
{24, []int{1, 3, 2, 4}, false, []int{2, 6, 5, 8}}, // windows end at 2.1, 8.4, 12.6, 21.0 respectively
{25, []int{1, 3, 2, 4}, false, []int{2, 7, 4, 9}}, // windows end at 2.2, 8.8, 13.2, 22.0 respectively
{26, []int{1, 3, 2, 4}, false, []int{2, 7, 5, 9}}, // windows end at 2.3, 9.2, 13.8, 23.0 respectively
}
for _, test := range tests {
widths := getWidths(test.wtot, test.ratios, test.drawbox)
if !reflect.DeepEqual(widths, test.exp) {
t.Errorf("at input (%v, %v, %v) expected %v but got %v", test.wtot, test.ratios, test.drawbox, test.exp, widths)
}
}
}
+13 -39
View File
@@ -480,10 +480,6 @@ func (win *win) printDir(ui *ui, dir *dir, context *dirContext, dirStyle *dirSty
// 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--
}
var icon []rune
var iconDef iconDef
@@ -598,45 +594,23 @@ func getCustomWidth(dir *dir, beg, end int) int {
return maxw
}
func getWidths(wtot int) []int {
rsum := 0
for _, r := range gOpts.ratios {
rsum += r
}
wlen := len(gOpts.ratios)
widths := make([]int, wlen)
if gOpts.drawbox {
wtot -= (wlen + 1)
}
wsum := 0
for i := range wlen - 1 {
widths[i] = gOpts.ratios[i] * wtot / rsum
wsum += widths[i]
}
widths[wlen-1] = wtot - wsum
return widths
}
func getWins(screen tcell.Screen) []*win {
wtot, htot := screen.Size()
widths := getWidths(wtot)
h := max(htot-2, 0)
x := 0
y := 1
if gOpts.drawbox {
h = max(htot-4, 0)
x = 1
y = 2
}
wacc := 0
wlen := len(widths)
wins := make([]*win, 0, wlen)
for i := range wlen {
if gOpts.drawbox {
wacc++
wins = append(wins, newWin(widths[i], max(htot-4, 0), wacc, 2))
} else {
wins = append(wins, newWin(widths[i], max(htot-2, 0), wacc, 1))
}
wacc += widths[i]
widths := getWidths(wtot, gOpts.ratios, gOpts.drawbox)
wins := make([]*win, 0, len(widths))
for _, w := range widths {
wins = append(wins, newWin(w, h, x, y))
x += w + 1
}
return wins