Rework high-performance pgup/pgdown

This commit is contained in:
Christian Rocha 2020-06-19 12:20:35 -04:00
parent da3150ded7
commit 1629afe087
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018

View File

@ -73,29 +73,44 @@ func (m *Model) SetContent(s string) {
m.lines = strings.Split(s, "\n") m.lines = strings.Split(s, "\n")
} }
// Return 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 := min(len(m.lines), m.YOffset+m.Height)
lines = m.lines[top:bottom]
}
return lines
}
// ViewDown moves the view down by the number of lines in the viewport. // ViewDown moves the view down by the number of lines in the viewport.
// Basically, "page down". // Basically, "page down".
func (m *Model) ViewDown() { func (m *Model) ViewDown() []string {
if m.AtBottom() { if m.AtBottom() {
return return nil
} }
m.YOffset = min( m.YOffset = min(
m.YOffset+m.Height, // target m.YOffset+m.Height, // target
len(m.lines)-m.Height, // fallback len(m.lines)-m.Height, // fallback
) )
return m.visibleLines()
} }
// ViewUp moves the view up by one height of the viewport. Basically, "page up". // ViewUp moves the view up by one height of the viewport. Basically, "page up".
func (m *Model) ViewUp() { func (m *Model) ViewUp() []string {
if m.AtTop() { if m.AtTop() {
return return nil
} }
m.YOffset = max( m.YOffset = max(
m.YOffset-m.Height, // target m.YOffset-m.Height, // target
0, // fallback 0, // fallback
) )
return m.visibleLines()
} }
// HalfViewDown moves the view down by half the height of the viewport. // HalfViewDown moves the view down by half the height of the viewport.
@ -173,31 +188,25 @@ func Sync(m Model) tea.Cmd {
) )
} }
func ViewDown(m Model) tea.Cmd { func ViewDown(m Model, lines []string) tea.Cmd {
if m.AtBottom() { if len(lines) == 0 {
return nil return nil
} }
top := max(m.YOffset+m.Height, 0)
bottom := min(top+m.Height, len(m.lines)-1)
return tea.ScrollDown( return tea.ScrollDown(
m.lines[top:bottom], lines,
m.YPosition, m.YPosition,
m.YPosition+m.Height, m.YPosition+m.Height,
) )
} }
func ViewUp(m Model) tea.Cmd { func ViewUp(m Model, lines []string) tea.Cmd {
if m.AtTop() { if len(lines) == 0 {
return nil return nil
} }
top := max(m.YOffset-m.Height, 0)
bottom := min(m.YOffset, len(m.lines)-1)
return tea.ScrollUp( return tea.ScrollUp(
m.lines[top:bottom], lines,
m.YPosition, m.YPosition,
m.YPosition+m.Height, m.YPosition+m.Height,
) )
@ -275,19 +284,19 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
case " ": // spacebar case " ": // spacebar
fallthrough fallthrough
case "f": case "f":
lines := m.ViewDown()
if m.HighPerformanceRendering { if m.HighPerformanceRendering {
cmd = ViewDown(m) cmd = ViewDown(m, lines)
} }
m.ViewDown()
// Up one page // Up one page
case "pgup": case "pgup":
fallthrough fallthrough
case "b": case "b":
lines := m.ViewUp()
if m.HighPerformanceRendering { if m.HighPerformanceRendering {
cmd = ViewUp(m) cmd = ViewUp(m, lines)
} }
m.ViewUp()
// Down half page // Down half page
case "d": case "d":