diff --git a/viewport/renderer.go b/viewport/renderer.go index 2299f5e..610d846 100644 --- a/viewport/renderer.go +++ b/viewport/renderer.go @@ -30,37 +30,37 @@ func (r *renderer) clear() { func (r *renderer) sync(lines []string) { r.clear() moveTo(r.Out, r.Y, 0) - r.write(lines) + r.writeLines(lines) } // write writes to the set writer. -func (r *renderer) write(lines []string) { +func (r *renderer) writeLines(lines []string) { if len(lines) == 0 { return } io.WriteString(r.Out, strings.Join(lines, "\r\n")) } -// Effectively scroll up. That is, insert a line at the top, scrolling +// Effectively scroll up. That is, insert a line at the top, pushing // everything else down. This is roughly how ncurses does it. -func (r *renderer) insertTop(line string) { +func (r *renderer) insertTop(lines []string) { changeScrollingRegion(r.Out, r.Y, r.Y+r.Height) moveTo(r.Out, r.Y, 0) - insertLine(r.Out, 1) - io.WriteString(r.Out, line) + insertLine(r.Out, len(lines)) + r.writeLines(lines) changeScrollingRegion(r.Out, r.TerminalWidth, r.TerminalHeight) } -// Effectively scroll down. That is, insert a line at the bottom, scrolling +// Effectively scroll down. That is, insert a line at the bottom, pushing // everything else up. This is roughly how ncurses does it. -func (r *renderer) insertBottom(line string) { +func (r *renderer) insertBottom(lines []string) { changeScrollingRegion(r.Out, r.Y, r.Y+r.Height) moveTo(r.Out, r.Y+r.Height, 0) - io.WriteString(r.Out, "\n"+line) + io.WriteString(r.Out, "\r\n"+strings.Join(lines, "\r\n")) changeScrollingRegion(r.Out, r.TerminalWidth, r.TerminalHeight) } -// Screen command +// Terminal Control const csi = "\x1b[" diff --git a/viewport/viewport.go b/viewport/viewport.go index 9f80510..d69a296 100644 --- a/viewport/viewport.go +++ b/viewport/viewport.go @@ -45,6 +45,16 @@ func NewModel(yPos, width, height, terminalWidth, terminalHeight int) Model { } } +// AtTop returns whether or not the viewport is in the very top position. +func (m Model) AtTop() bool { + return m.YOffset <= 0 +} + +// AtBottom returns whether or not the viewport is at the very botom position. +func (m Model) AtBottom() bool { + return m.YOffset >= len(m.lines)-m.Height-1 +} + // Scrollpercent returns the amount scrolled as a float between 0 and 1. func (m Model) ScrollPercent() float64 { if m.Height >= len(m.lines) { @@ -61,53 +71,117 @@ func (m *Model) SetContent(s string) { s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings m.lines = strings.Split(s, "\n") - lines := bounded(m.lines, m.YOffset, m.Height) - m.r.sync(lines) + if m.UseInternalRenderer { + top := max(m.YOffset, 0) + bottom := min(m.YOffset+m.Height, len(m.lines)) + m.r.sync(m.lines[top:bottom]) + } } // ViewDown moves the view down by the number of lines in the viewport. // Basically, "page down". func (m *Model) ViewDown() { - m.YOffset = min(len(m.lines)-m.Height, m.YOffset+m.Height) + if m.AtBottom() { + return + } + + if m.UseInternalRenderer { + top := max(m.YOffset+m.Height, 0) + bottom := min(top+m.Height, len(m.lines)-1) + m.r.insertBottom(m.lines[top:bottom]) + } + + m.YOffset = min( + m.YOffset+m.Height, // target + len(m.lines)-m.Height, // fallback + ) } // ViewUp moves the view up by one height of the viewport. Basically, "page up". func (m *Model) ViewUp() { - m.YOffset = max(0, m.YOffset-m.Height) -} + if m.AtTop() { + return + } -// HalfViewUp moves the view up by half the height of the viewport. -func (m *Model) HalfViewUp() { - m.YOffset = max(0, m.YOffset-m.Height/2) + if m.UseInternalRenderer { + top := max(m.YOffset-m.Height, 0) + bottom := min(m.YOffset, len(m.lines)) + m.r.insertTop(m.lines[top:bottom]) + } + + m.YOffset = max( + m.YOffset-m.Height, // target + 0, // fallback + ) } // HalfViewDown moves the view down by half the height of the viewport. func (m *Model) HalfViewDown() { - m.YOffset = min(len(m.lines)-m.Height, m.YOffset+m.Height/2) + if m.AtBottom() { + return + } + + if m.UseInternalRenderer { + top := max(m.YOffset+m.Height/2, 0) + bottom := min(top+m.Height, len(m.lines)-1) + m.r.insertBottom(m.lines[top:bottom]) + } + + m.YOffset = min( + m.YOffset+m.Height/2, // target + len(m.lines)-m.Height, // fallback + ) +} + +// HalfViewUp moves the view up by half the height of the viewport. +func (m *Model) HalfViewUp() { + if m.AtTop() { + return + } + + if m.UseInternalRenderer { + top := max(m.YOffset-m.Height/2, 0) + bottom := clamp(m.YOffset, top, len(m.lines)) + m.r.insertTop(m.lines[top:bottom]) + } + + m.YOffset = max( + m.YOffset-m.Height/2, // target + 0, // fallback + ) } // LineDown moves the view up by the given number of lines. func (m *Model) LineDown(n int) { - if m.YOffset >= len(m.lines)-m.Height-1 { + if m.AtBottom() || n == 0 { return } - m.YOffset = min(len(m.lines)-m.Height, m.YOffset+n) if m.UseInternalRenderer { - m.r.insertBottom(m.lines[m.YOffset+m.Height-1]) + bottom := min(m.YOffset+m.Height+n, len(m.lines)) + top := max(bottom-n, 0) + m.r.insertBottom(m.lines[top:bottom]) } + + m.YOffset = min( + m.YOffset+n, // target + len(m.lines)-m.Height, // fallback + ) } // LineUp moves the view down by the given number of lines. func (m *Model) LineUp(n int) { - if m.YOffset == 0 { + if m.AtTop() || n == 0 { return } - m.YOffset = max(0, m.YOffset-n) if m.UseInternalRenderer { - m.r.insertTop(m.lines[m.YOffset]) + top := max(m.YOffset-n, 0) + bottom := min(top+n, len(m.lines)) + m.r.insertTop(m.lines[top:bottom]) } + + m.YOffset = max(m.YOffset-n, 0) } // UPDATE @@ -214,7 +288,3 @@ func max(a, b int) int { func clamp(val, low, high int) int { return max(low, min(high, val)) } - -func bounded(s []string, start, end int) []string { - return s[clamp(start, 0, len(s)):clamp(end, 0, len(s))] -}