Clamp scroll percentage

This commit is contained in:
Christian Rocha 2020-06-19 15:03:41 -04:00
parent 5255143e87
commit 0c1781fbb3
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018

View File

@ -1,6 +1,7 @@
package viewport package viewport
import ( import (
"math"
"strings" "strings"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
@ -32,6 +33,7 @@ type Model struct {
lines []string lines []string
} }
// TODO: do we really need this?
func NewModel(width, height int) Model { func NewModel(width, height int) Model {
return Model{ return Model{
Width: width, Width: width,
@ -63,7 +65,8 @@ func (m Model) ScrollPercent() float64 {
y := float64(m.YOffset) y := float64(m.YOffset)
h := float64(m.Height) h := float64(m.Height)
t := float64(len(m.lines)) t := float64(len(m.lines))
return y / (t - h) v := y / (t - h)
return math.Max(0.0, math.Min(1.0, v))
} }
// SetContent set the pager's text content. For high performance rendering the // SetContent set the pager's text content. For high performance rendering the