From 4351f6b505ceb9ba1a522e48bcd2b45647eb99a5 Mon Sep 17 00:00:00 2001 From: citrons Date: Sun, 1 Jun 2025 15:29:55 -0500 Subject: tui/layout.go: fix crash --- tui/layout.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/tui/layout.go b/tui/layout.go index 398c0b1..9acc65b 100644 --- a/tui/layout.go +++ b/tui/layout.go @@ -277,19 +277,23 @@ func (b *Box) computePositions(axis int) { } } if b.Scroll != nil { - first := b.children[0] - last := b.children[len(b.children) - 1] + var first, last *Box + if len(b.children) > 0 { + first = b.children[0] + last = b.children[len(b.children) - 1] + } - lastPos := last.computedPosition - lastPos += last.computedSize[axis] - if b.Dir.reverse() { - lastPos = b.computedSize[axis] - lastPos + if last != nil { + lastPos := last.computedPosition + lastPos += last.computedSize[axis] + if b.Dir.reverse() { + lastPos = b.computedSize[axis] - lastPos + } + b.Scroll.absolute = min(b.Scroll.absolute, lastPos - b.computedSize[axis]) } - b.Scroll.absolute = min(b.Scroll.absolute, lastPos - b.computedSize[axis]) b.Scroll.absolute = max(b.Scroll.absolute, 0) - var firstShown *Box - var lastShown *Box + var firstShown, lastShown *Box for _, c := range b.children { if !b.Dir.reverse() { c.computedPosition -= b.Scroll.absolute @@ -299,9 +303,9 @@ func (b *Box) computePositions(axis int) { if c.computedPosition > b.computedRect.min[axis] && c.computedPosition < b.computedRect.max[axis] { if firstShown == nil { - first = c + firstShown = c } - last = c + lastShown = c } } if firstShown == first { -- cgit v1.2.3