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

fix(viewport): preempt potential panics when calculating visible lines #608

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 15 additions & 5 deletions viewport/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,17 @@ func (m Model) maxYOffset() int {
// visibleLines returns the lines that should currently be visible in the
// viewport.
func (m Model) visibleLines() (lines []string) {
if len(m.lines) > 0 {
top := max(0, m.YOffset)
bottom := clamp(m.YOffset+m.Height, top, len(m.lines))
lines = m.lines[top:bottom]
if len(m.lines) == 0 {
return nil
}
top := max(0, m.YOffset)
bottom := min(m.YOffset+m.Height, len(m.lines))
if top >= bottom {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we also need to check if bottom is within m.lines bounds?

// Return early, otherwise we'll panic with a slice out of bounds
// error.
return nil
}
return lines
return m.lines[top:bottom]
}

// scrollArea returns the scrollable boundaries for high performance rendering.
Expand Down Expand Up @@ -233,6 +238,11 @@ func (m *Model) GotoTop() (lines []string) {

// GotoBottom sets the viewport to the bottom position.
func (m *Model) GotoBottom() (lines []string) {
if len(m.lines) == 0 {
// If there are no lines, we can't go to the bottom...because we're
// already there.
return nil
}
m.SetYOffset(m.maxYOffset())
return m.visibleLines()
}
Expand Down
Loading