diff --git a/viewport/viewport.go b/viewport/viewport.go index 5862ac05e..3dedd1d25 100644 --- a/viewport/viewport.go +++ b/viewport/viewport.go @@ -147,6 +147,9 @@ func (m Model) visibleLines() (lines []string) { if len(m.lines) > 0 { top := max(0, m.YOffset) bottom := clamp(m.YOffset+h, top, len(m.lines)) + if top >= bottom { + return nil + } lines = m.lines[top:bottom] } diff --git a/viewport/viewport_test.go b/viewport/viewport_test.go index 6f8cb371b..02b89ad1b 100644 --- a/viewport/viewport_test.go +++ b/viewport/viewport_test.go @@ -201,6 +201,38 @@ func TestVisibleLines(t *testing.T) { } }) + t.Run("negative height should not panic", func(t *testing.T) { + t.Parallel() + + m := New(10, 10) + m.SetContent(strings.Join(defaultList, "\n")) + m.YOffset = 5 + m.Height = -4 + + // This should not panic + list := m.visibleLines() + + if list != nil { + t.Errorf("list should be nil with negative height, got %d items", len(list)) + } + }) + + t.Run("zero height should not panic", func(t *testing.T) { + t.Parallel() + + m := New(10, 10) + m.SetContent(strings.Join(defaultList, "\n")) + m.YOffset = 5 + m.Height = 0 + + // This should not panic + list := m.visibleLines() + + if list != nil { + t.Errorf("list should be nil with zero height, got %d items", len(list)) + } + }) + t.Run("empty list: with indent", func(t *testing.T) { t.Parallel()