2020-05-26 02:57:58 +03:00
|
|
|
package viewport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MODEL
|
|
|
|
|
|
|
|
type Model struct {
|
|
|
|
Width int
|
|
|
|
Height int
|
2020-06-16 00:50:13 +03:00
|
|
|
|
|
|
|
// YOffset is the vertical scroll position.
|
|
|
|
YOffset int
|
|
|
|
|
2020-06-18 03:55:49 +03:00
|
|
|
// YPosition is the position of the viewport in relation to the terminal
|
|
|
|
// window. It's used in high performance rendering.
|
|
|
|
YPosition int
|
2020-06-16 00:50:13 +03:00
|
|
|
|
2020-06-18 03:55:49 +03:00
|
|
|
// HighPerformanceRendering bypasses the normal Bubble Tea renderer to
|
|
|
|
// provide higher performance rendering. Most of the time the normal Bubble
|
|
|
|
// Tea rendering methods will suffice, but if you're passing content with
|
|
|
|
// a lot of ANSI escape codes you may see improved rendering in certain
|
|
|
|
// terminals with this enabled.
|
|
|
|
//
|
|
|
|
// This should only be used in program occupying the entire terminal width,
|
|
|
|
// usually via the alternate screen buffer.
|
|
|
|
HighPerformanceRendering bool
|
2020-05-26 02:57:58 +03:00
|
|
|
|
|
|
|
lines []string
|
2020-06-16 00:50:13 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 03:55:49 +03:00
|
|
|
func NewModel(width, height int) Model {
|
2020-06-16 00:50:13 +03:00
|
|
|
return Model{
|
2020-06-18 03:55:49 +03:00
|
|
|
Width: width,
|
|
|
|
Height: height,
|
2020-06-16 00:50:13 +03:00
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 03:55:49 +03:00
|
|
|
func (m Model) SetSize(yPos int, width, height int) {
|
|
|
|
m.YPosition = yPos
|
|
|
|
m.Width = width
|
|
|
|
m.Height = height
|
|
|
|
}
|
|
|
|
|
2020-06-16 04:19:15 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
// Scrollpercent returns the amount scrolled as a float between 0 and 1.
|
|
|
|
func (m Model) ScrollPercent() float64 {
|
|
|
|
if m.Height >= len(m.lines) {
|
|
|
|
return 1.0
|
|
|
|
}
|
2020-06-16 00:50:13 +03:00
|
|
|
y := float64(m.YOffset)
|
2020-05-26 02:57:58 +03:00
|
|
|
h := float64(m.Height)
|
|
|
|
t := float64(len(m.lines))
|
|
|
|
return y / (t - h)
|
|
|
|
}
|
|
|
|
|
2020-06-18 20:41:23 +03:00
|
|
|
// SetContent set the pager's text content. For high performance rendering the
|
|
|
|
// Sync command should also be called.
|
2020-05-26 02:57:58 +03:00
|
|
|
func (m *Model) SetContent(s string) {
|
|
|
|
s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings
|
|
|
|
m.lines = strings.Split(s, "\n")
|
|
|
|
}
|
|
|
|
|
2020-06-19 19:20:35 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
// ViewDown moves the view down by the number of lines in the viewport.
|
|
|
|
// Basically, "page down".
|
2020-06-19 19:20:35 +03:00
|
|
|
func (m *Model) ViewDown() []string {
|
2020-06-16 04:19:15 +03:00
|
|
|
if m.AtBottom() {
|
2020-06-19 19:20:35 +03:00
|
|
|
return nil
|
2020-06-16 04:19:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
m.YOffset = min(
|
|
|
|
m.YOffset+m.Height, // target
|
|
|
|
len(m.lines)-m.Height, // fallback
|
|
|
|
)
|
2020-06-19 19:20:35 +03:00
|
|
|
|
|
|
|
return m.visibleLines()
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ViewUp moves the view up by one height of the viewport. Basically, "page up".
|
2020-06-19 19:20:35 +03:00
|
|
|
func (m *Model) ViewUp() []string {
|
2020-06-16 04:19:15 +03:00
|
|
|
if m.AtTop() {
|
2020-06-19 19:20:35 +03:00
|
|
|
return nil
|
2020-06-16 04:19:15 +03:00
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
|
2020-06-16 04:19:15 +03:00
|
|
|
m.YOffset = max(
|
|
|
|
m.YOffset-m.Height, // target
|
|
|
|
0, // fallback
|
|
|
|
)
|
2020-06-19 19:20:35 +03:00
|
|
|
|
|
|
|
return m.visibleLines()
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// HalfViewDown moves the view down by half the height of the viewport.
|
|
|
|
func (m *Model) HalfViewDown() {
|
2020-06-16 04:19:15 +03:00
|
|
|
if m.AtBottom() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
m.YOffset = max(
|
|
|
|
m.YOffset-m.Height/2, // target
|
|
|
|
0, // fallback
|
|
|
|
)
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// LineDown moves the view up by the given number of lines.
|
2020-06-19 18:50:54 +03:00
|
|
|
func (m *Model) LineDown(n int) (lines []string) {
|
2020-06-16 04:19:15 +03:00
|
|
|
if m.AtBottom() || n == 0 {
|
2020-06-16 00:50:13 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-16 04:19:15 +03:00
|
|
|
m.YOffset = min(
|
|
|
|
m.YOffset+n, // target
|
|
|
|
len(m.lines)-m.Height, // fallback
|
|
|
|
)
|
2020-06-19 18:50:54 +03:00
|
|
|
|
|
|
|
if len(m.lines) > 0 {
|
|
|
|
top := max(0, m.YOffset+m.Height-n)
|
|
|
|
bottom := min(len(m.lines)-1, m.YOffset+m.Height)
|
|
|
|
lines = m.lines[top:bottom]
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
2020-06-19 18:50:54 +03:00
|
|
|
// LineUp moves the view down by the given number of lines. Returns the new
|
|
|
|
// lines to show.
|
|
|
|
func (m *Model) LineUp(n int) (lines []string) {
|
2020-06-16 04:19:15 +03:00
|
|
|
if m.AtTop() || n == 0 {
|
2020-06-19 18:50:54 +03:00
|
|
|
return lines
|
2020-06-16 00:50:13 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 03:55:49 +03:00
|
|
|
m.YOffset = max(m.YOffset-n, 0)
|
2020-06-19 18:50:54 +03:00
|
|
|
|
|
|
|
if len(m.lines) > 0 {
|
|
|
|
top := max(0, m.YOffset)
|
|
|
|
bottom := min(len(m.lines)-1, m.YOffset+n)
|
|
|
|
lines = m.lines[top:bottom]
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines
|
2020-06-18 03:55:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// COMMANDS
|
|
|
|
|
|
|
|
func Sync(m Model) tea.Cmd {
|
2020-06-18 23:29:05 +03:00
|
|
|
top := max(m.YOffset, 0)
|
|
|
|
bottom := min(m.YOffset+m.Height, len(m.lines)-1)
|
|
|
|
|
2020-06-18 20:41:23 +03:00
|
|
|
return tea.SyncScrollArea(
|
|
|
|
m.lines[top:bottom],
|
|
|
|
m.YPosition,
|
|
|
|
m.YPosition+m.Height,
|
|
|
|
)
|
2020-06-18 03:55:49 +03:00
|
|
|
}
|
|
|
|
|
2020-06-19 19:20:35 +03:00
|
|
|
func ViewDown(m Model, lines []string) tea.Cmd {
|
|
|
|
if len(lines) == 0 {
|
2020-06-18 03:55:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return tea.ScrollDown(
|
2020-06-19 19:20:35 +03:00
|
|
|
lines,
|
2020-06-18 03:55:49 +03:00
|
|
|
m.YPosition,
|
|
|
|
m.YPosition+m.Height,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-19 19:20:35 +03:00
|
|
|
func ViewUp(m Model, lines []string) tea.Cmd {
|
|
|
|
if len(lines) == 0 {
|
2020-06-18 03:55:49 +03:00
|
|
|
return nil
|
2020-06-16 00:50:13 +03:00
|
|
|
}
|
2020-06-16 04:19:15 +03:00
|
|
|
|
2020-06-18 03:55:49 +03:00
|
|
|
return tea.ScrollUp(
|
2020-06-19 19:20:35 +03:00
|
|
|
lines,
|
2020-06-18 03:55:49 +03:00
|
|
|
m.YPosition,
|
|
|
|
m.YPosition+m.Height,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func HalfViewDown(m Model) tea.Cmd {
|
|
|
|
if m.AtBottom() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
top := max(m.YOffset+m.Height/2, 0)
|
|
|
|
bottom := min(top+m.Height, len(m.lines)-1)
|
|
|
|
|
|
|
|
return tea.ScrollDown(
|
|
|
|
m.lines[top:bottom],
|
|
|
|
m.YPosition,
|
|
|
|
m.YPosition+m.Height,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func HalfViewUp(m Model) tea.Cmd {
|
|
|
|
if m.AtTop() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
top := max(m.YOffset-m.Height/2, 0)
|
2020-06-18 23:29:05 +03:00
|
|
|
bottom := clamp(m.YOffset, top, len(m.lines)-1)
|
2020-06-18 03:55:49 +03:00
|
|
|
|
|
|
|
return tea.ScrollUp(
|
|
|
|
m.lines[top:bottom],
|
|
|
|
m.YPosition,
|
|
|
|
m.YPosition+m.Height,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-19 18:50:54 +03:00
|
|
|
func LineDown(m Model, lines []string) tea.Cmd {
|
|
|
|
if len(lines) == 0 {
|
2020-06-18 03:55:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return tea.ScrollDown(
|
2020-06-19 18:50:54 +03:00
|
|
|
lines,
|
2020-06-18 03:55:49 +03:00
|
|
|
m.YPosition,
|
|
|
|
m.YPosition+m.Height,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-19 18:50:54 +03:00
|
|
|
func LineUp(m Model, lines []string) tea.Cmd {
|
|
|
|
if len(lines) == 0 {
|
2020-06-18 03:55:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return tea.ScrollUp(
|
2020-06-19 18:50:54 +03:00
|
|
|
lines,
|
2020-06-18 03:55:49 +03:00
|
|
|
m.YPosition,
|
|
|
|
m.YPosition+m.Height,
|
|
|
|
)
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// UPDATE
|
|
|
|
|
2020-06-18 23:29:05 +03:00
|
|
|
// Update runs the update loop with default keybindings similar to popular
|
|
|
|
// pagers. To define your own keybindings use the methods on Model (i.e.
|
|
|
|
// Model.LineDown()) and define your own update function.
|
2020-05-26 02:57:58 +03:00
|
|
|
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
2020-06-18 03:55:49 +03:00
|
|
|
var cmd tea.Cmd
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.String() {
|
|
|
|
// Down one page
|
|
|
|
case "pgdown":
|
|
|
|
fallthrough
|
|
|
|
case " ": // spacebar
|
|
|
|
fallthrough
|
|
|
|
case "f":
|
2020-06-19 19:20:35 +03:00
|
|
|
lines := m.ViewDown()
|
2020-06-18 03:55:49 +03:00
|
|
|
if m.HighPerformanceRendering {
|
2020-06-19 19:20:35 +03:00
|
|
|
cmd = ViewDown(m, lines)
|
2020-06-18 03:55:49 +03:00
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
|
|
|
|
// Up one page
|
|
|
|
case "pgup":
|
|
|
|
fallthrough
|
|
|
|
case "b":
|
2020-06-19 19:20:35 +03:00
|
|
|
lines := m.ViewUp()
|
2020-06-18 03:55:49 +03:00
|
|
|
if m.HighPerformanceRendering {
|
2020-06-19 19:20:35 +03:00
|
|
|
cmd = ViewUp(m, lines)
|
2020-06-18 03:55:49 +03:00
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
|
|
|
|
// Down half page
|
|
|
|
case "d":
|
2020-06-18 03:55:49 +03:00
|
|
|
if m.HighPerformanceRendering {
|
|
|
|
cmd = HalfViewDown(m)
|
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
m.HalfViewDown()
|
|
|
|
|
|
|
|
// Up half page
|
|
|
|
case "u":
|
2020-06-18 03:55:49 +03:00
|
|
|
if m.HighPerformanceRendering {
|
|
|
|
cmd = HalfViewUp(m)
|
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
m.HalfViewUp()
|
|
|
|
|
|
|
|
// Down one line
|
|
|
|
case "down":
|
|
|
|
fallthrough
|
|
|
|
case "j":
|
2020-06-19 18:50:54 +03:00
|
|
|
lines := m.LineDown(1)
|
2020-06-18 03:55:49 +03:00
|
|
|
if m.HighPerformanceRendering {
|
2020-06-19 18:50:54 +03:00
|
|
|
cmd = LineDown(m, lines)
|
2020-06-18 03:55:49 +03:00
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
|
|
|
|
// Up one line
|
|
|
|
case "up":
|
|
|
|
fallthrough
|
|
|
|
case "k":
|
2020-06-19 18:50:54 +03:00
|
|
|
lines := m.LineUp(1)
|
2020-06-18 03:55:49 +03:00
|
|
|
if m.HighPerformanceRendering {
|
2020-06-19 18:50:54 +03:00
|
|
|
cmd = LineUp(m, lines)
|
2020-06-18 03:55:49 +03:00
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 03:55:49 +03:00
|
|
|
return m, cmd
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// VIEW
|
|
|
|
|
|
|
|
// View renders the viewport into a string.
|
|
|
|
func View(m Model) string {
|
2020-06-16 00:50:13 +03:00
|
|
|
|
2020-06-18 03:55:49 +03:00
|
|
|
if m.HighPerformanceRendering {
|
2020-06-19 19:20:35 +03:00
|
|
|
// Just send newlines since we're doing to be rendering the actual
|
2020-06-19 18:50:54 +03:00
|
|
|
// content seprately. We still need send something that equals the
|
|
|
|
// height of this view so that the Bubble Tea standard renderer can
|
|
|
|
// position anything below this view properly.
|
2020-06-19 01:31:30 +03:00
|
|
|
return strings.Repeat("\n", m.Height-1)
|
2020-06-16 00:50:13 +03:00
|
|
|
}
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
var lines []string
|
|
|
|
|
|
|
|
if len(m.lines) > 0 {
|
2020-06-16 00:50:13 +03:00
|
|
|
top := max(0, m.YOffset)
|
|
|
|
bottom := min(len(m.lines), m.YOffset+m.Height)
|
2020-05-26 02:57:58 +03:00
|
|
|
lines = m.lines[top:bottom]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill empty space with newlines
|
|
|
|
extraLines := ""
|
|
|
|
if len(lines) < m.Height {
|
|
|
|
extraLines = strings.Repeat("\n", m.Height-len(lines))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(lines, "\n") + extraLines
|
|
|
|
}
|
|
|
|
|
|
|
|
// ETC
|
|
|
|
|
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func max(a, b int) int {
|
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
2020-06-16 00:50:13 +03:00
|
|
|
|
|
|
|
func clamp(val, low, high int) int {
|
|
|
|
return max(low, min(high, val))
|
|
|
|
}
|