diff --git a/viewport/viewport.go b/viewport/viewport.go index abadb51..784a92e 100644 --- a/viewport/viewport.go +++ b/viewport/viewport.go @@ -173,6 +173,12 @@ func (m *Model) LineDown(n int) (lines []string) { return nil } + // Make sure the number of lines by which we're going to scroll isn't + // greater than the number of lines we actually have left before we reach + // the bottom. + maxDelta := (len(m.lines) - 1) - (m.YOffset + m.Height) // number of lines - viewport bottom edge + n = min(n, maxDelta) + m.YOffset = min( m.YOffset+n, // target len(m.lines)-1-m.Height, // fallback @@ -194,6 +200,10 @@ func (m *Model) LineUp(n int) (lines []string) { return nil } + // Make sure the number of lines by which we're going to scroll isn't + // greater than the number of lines we are from the top. + n = min(n, m.YOffset) + m.YOffset = max(m.YOffset-n, 0) if len(m.lines) > 0 {