Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions viewport/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down
32 changes: 32 additions & 0 deletions viewport/viewport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down