2020-05-26 02:57:58 +03:00
|
|
|
package viewport
|
|
|
|
|
|
|
|
import (
|
2020-06-19 22:03:41 +03:00
|
|
|
"math"
|
2020-05-26 02:57:58 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
)
|
|
|
|
|
2021-03-12 03:27:41 +03:00
|
|
|
const (
|
|
|
|
spacebar = " "
|
|
|
|
mouseWheelDelta = 3
|
|
|
|
)
|
2020-07-20 18:56:35 +03:00
|
|
|
|
2021-03-12 04:00:03 +03:00
|
|
|
// Model is the Bubble Tea model for this viewport element.
|
2020-05-26 02:57:58 +03:00
|
|
|
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.
|
|
|
|
//
|
2020-06-20 02:02:14 +03:00
|
|
|
// This should only be used in program occupying the entire terminal,
|
|
|
|
// which is usually via the alternate screen buffer.
|
2020-06-18 03:55:49 +03:00
|
|
|
HighPerformanceRendering bool
|
2020-05-26 02:57:58 +03:00
|
|
|
|
|
|
|
lines []string
|
2020-06-16 00:50:13 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-18 02:22:08 +03:00
|
|
|
// AtBottom returns whether or not the viewport is at or past the very bottom
|
|
|
|
// position.
|
2020-06-16 04:19:15 +03:00
|
|
|
func (m Model) AtBottom() bool {
|
2021-09-16 18:04:08 +03:00
|
|
|
return m.YOffset >= len(m.lines)-m.Height
|
2020-07-18 02:22:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// PastBottom returns whether or not the viewport is scrolled beyond the last
|
|
|
|
// line. This can happen when adjusting the viewport height.
|
|
|
|
func (m Model) PastBottom() bool {
|
2021-09-16 18:04:08 +03:00
|
|
|
return m.YOffset > len(m.lines)-m.Height
|
2020-06-16 04:19:15 +03:00
|
|
|
}
|
|
|
|
|
2021-03-12 04:00:03 +03:00
|
|
|
// ScrollPercent returns the amount scrolled as a float between 0 and 1.
|
2020-05-26 02:57:58 +03:00
|
|
|
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)
|
2020-07-18 01:13:05 +03:00
|
|
|
t := float64(len(m.lines) - 1)
|
2020-06-19 22:03:41 +03:00
|
|
|
v := y / (t - h)
|
|
|
|
return math.Max(0.0, math.Min(1.0, v))
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
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")
|
2021-04-20 02:22:26 +03:00
|
|
|
|
|
|
|
if m.YOffset > len(m.lines)-1 {
|
|
|
|
m.GotoBottom()
|
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
2020-08-22 13:41:09 +03:00
|
|
|
// Return the lines that should currently be visible in the viewport.
|
2020-06-19 19:20:35 +03:00
|
|
|
func (m Model) visibleLines() (lines []string) {
|
|
|
|
if len(m.lines) > 0 {
|
|
|
|
top := max(0, m.YOffset)
|
2020-10-25 22:17:11 +03:00
|
|
|
bottom := clamp(m.YOffset+m.Height, top, len(m.lines))
|
2020-06-19 19:20:35 +03:00
|
|
|
lines = m.lines[top:bottom]
|
|
|
|
}
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2021-09-16 18:04:08 +03:00
|
|
|
// SetYOffset sets the Y offset.
|
|
|
|
func (m *Model) SetYOffset(n int) {
|
|
|
|
m.YOffset = clamp(n, 0, len(m.lines)-m.Height)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-16 18:04:08 +03:00
|
|
|
m.SetYOffset(m.YOffset + m.Height)
|
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
|
|
|
|
2021-09-16 18:04:08 +03:00
|
|
|
m.SetYOffset(m.YOffset - m.Height)
|
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.
|
2020-06-19 20:23:14 +03:00
|
|
|
func (m *Model) HalfViewDown() (lines []string) {
|
2020-06-16 04:19:15 +03:00
|
|
|
if m.AtBottom() {
|
2020-06-19 20:23:14 +03:00
|
|
|
return nil
|
2020-06-16 04:19:15 +03:00
|
|
|
}
|
|
|
|
|
2021-09-16 18:04:08 +03:00
|
|
|
m.SetYOffset(m.YOffset + m.Height/2)
|
|
|
|
return m.visibleLines()
|
2020-06-16 04:19:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// HalfViewUp moves the view up by half the height of the viewport.
|
2020-06-19 20:23:14 +03:00
|
|
|
func (m *Model) HalfViewUp() (lines []string) {
|
2020-06-16 04:19:15 +03:00
|
|
|
if m.AtTop() {
|
2020-06-19 20:23:14 +03:00
|
|
|
return nil
|
2020-06-16 04:19:15 +03:00
|
|
|
}
|
|
|
|
|
2021-09-16 18:04:08 +03:00
|
|
|
m.SetYOffset(m.YOffset - m.Height/2)
|
|
|
|
return m.visibleLines()
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
2020-07-18 00:02:37 +03:00
|
|
|
// LineDown moves the view down 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-19 20:23:14 +03:00
|
|
|
return nil
|
2020-06-16 00:50:13 +03:00
|
|
|
}
|
|
|
|
|
2020-07-21 00:46:15 +03:00
|
|
|
// 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.
|
2021-09-16 18:04:08 +03:00
|
|
|
m.SetYOffset(m.YOffset + n)
|
|
|
|
return m.visibleLines()
|
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 20:23:14 +03:00
|
|
|
return nil
|
2020-06-16 00:50:13 +03:00
|
|
|
}
|
|
|
|
|
2020-07-21 00:46:15 +03:00
|
|
|
// 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.
|
2021-09-16 18:04:08 +03:00
|
|
|
m.SetYOffset(m.YOffset - n)
|
|
|
|
return m.visibleLines()
|
2020-07-18 02:22:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GotoTop sets the viewport to the top position.
|
|
|
|
func (m *Model) GotoTop() (lines []string) {
|
|
|
|
if m.AtTop() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-16 18:04:08 +03:00
|
|
|
m.SetYOffset(0)
|
|
|
|
return m.visibleLines()
|
2020-07-18 02:22:08 +03:00
|
|
|
}
|
|
|
|
|
2021-03-12 04:00:03 +03:00
|
|
|
// GotoBottom sets the viewport to the bottom position.
|
2020-07-18 02:22:08 +03:00
|
|
|
func (m *Model) GotoBottom() (lines []string) {
|
2021-09-16 18:04:08 +03:00
|
|
|
m.SetYOffset(len(m.lines) - 1 - m.Height)
|
|
|
|
return m.visibleLines()
|
2020-06-18 03:55:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// COMMANDS
|
|
|
|
|
2020-06-20 20:59:13 +03:00
|
|
|
// Sync tells the renderer where the viewport will be located and requests
|
|
|
|
// a render of the current state of the viewport. It should be called for the
|
|
|
|
// first render and after a window resize.
|
|
|
|
//
|
|
|
|
// For high performance rendering only.
|
2020-06-18 03:55:49 +03:00
|
|
|
func Sync(m Model) tea.Cmd {
|
2020-06-20 02:46:51 +03:00
|
|
|
if len(m.lines) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-18 00:02:37 +03:00
|
|
|
// TODO: we should probably use m.visibleLines() rather than these two
|
|
|
|
// expressions.
|
2020-06-18 23:29:05 +03:00
|
|
|
top := max(m.YOffset, 0)
|
2020-10-25 22:17:11 +03:00
|
|
|
bottom := clamp(m.YOffset+m.Height, 0, len(m.lines)-1)
|
2020-06-18 23:29:05 +03:00
|
|
|
|
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-07-18 01:22:51 +03:00
|
|
|
// ViewDown is a high performance command that moves the viewport up by a given
|
|
|
|
// numer of lines. Use Model.ViewDown to get the lines that should be rendered.
|
|
|
|
// For example:
|
2020-06-20 20:59:13 +03:00
|
|
|
//
|
|
|
|
// lines := model.ViewDown(1)
|
|
|
|
// cmd := ViewDown(m, lines)
|
|
|
|
//
|
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
|
|
|
|
}
|
2020-06-19 20:23:14 +03:00
|
|
|
return tea.ScrollDown(lines, m.YPosition, m.YPosition+m.Height)
|
2020-06-18 03:55:49 +03:00
|
|
|
}
|
|
|
|
|
2020-07-18 01:22:51 +03:00
|
|
|
// ViewUp is a high performance command the moves the viewport down by a given
|
2020-10-27 23:41:01 +03:00
|
|
|
// number of lines height. Use Model.ViewUp to get the lines that should be
|
2020-06-20 20:59:13 +03:00
|
|
|
// rendered.
|
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-19 20:23:14 +03:00
|
|
|
return tea.ScrollUp(lines, m.YPosition, m.YPosition+m.Height)
|
2020-06-18 03:55:49 +03:00
|
|
|
}
|
|
|
|
|
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-10-27 23:41:01 +03:00
|
|
|
func (m Model) Update(msg tea.Msg) (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
|
2020-07-20 18:56:35 +03:00
|
|
|
case "pgdown", spacebar, "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
|
2020-07-20 18:56:35 +03:00
|
|
|
case "pgup", "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
|
2020-10-22 05:28:44 +03:00
|
|
|
case "d", "ctrl+d":
|
2020-06-19 20:23:14 +03:00
|
|
|
lines := m.HalfViewDown()
|
2020-06-18 03:55:49 +03:00
|
|
|
if m.HighPerformanceRendering {
|
2020-07-18 01:22:51 +03:00
|
|
|
cmd = ViewDown(m, lines)
|
2020-06-18 03:55:49 +03:00
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
|
|
|
|
// Up half page
|
2020-10-22 05:28:44 +03:00
|
|
|
case "u", "ctrl+u":
|
2020-06-19 20:23:14 +03:00
|
|
|
lines := m.HalfViewUp()
|
2020-06-18 03:55:49 +03:00
|
|
|
if m.HighPerformanceRendering {
|
2020-07-18 01:22:51 +03:00
|
|
|
cmd = ViewUp(m, lines)
|
2020-06-18 03:55:49 +03:00
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
|
|
|
|
// Down one line
|
2020-07-20 18:56:35 +03:00
|
|
|
case "down", "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-07-18 01:22:51 +03:00
|
|
|
cmd = ViewDown(m, lines)
|
2020-06-18 03:55:49 +03:00
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
|
|
|
|
// Up one line
|
2020-07-20 18:56:35 +03:00
|
|
|
case "up", "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-07-18 01:22:51 +03:00
|
|
|
cmd = ViewUp(m, lines)
|
2020-06-18 03:55:49 +03:00
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
2020-06-23 19:00:17 +03:00
|
|
|
|
|
|
|
case tea.MouseMsg:
|
2020-10-06 19:29:18 +03:00
|
|
|
switch msg.Type {
|
2020-06-23 19:00:17 +03:00
|
|
|
case tea.MouseWheelUp:
|
2021-03-12 03:27:41 +03:00
|
|
|
lines := m.LineUp(mouseWheelDelta)
|
2020-06-23 19:00:17 +03:00
|
|
|
if m.HighPerformanceRendering {
|
2020-07-18 01:22:51 +03:00
|
|
|
cmd = ViewUp(m, lines)
|
2020-06-23 19:00:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
case tea.MouseWheelDown:
|
2021-03-12 03:27:41 +03:00
|
|
|
lines := m.LineDown(mouseWheelDelta)
|
2020-06-23 19:00:17 +03:00
|
|
|
if m.HighPerformanceRendering {
|
2020-07-18 01:22:51 +03:00
|
|
|
cmd = ViewDown(m, lines)
|
2020-06-23 19:00:17 +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.
|
2020-10-27 23:41:01 +03:00
|
|
|
func (m Model) View() string {
|
2020-06-18 03:55:49 +03:00
|
|
|
if m.HighPerformanceRendering {
|
2021-09-16 18:04:08 +03:00
|
|
|
// Just send newlines since we're going to be rendering the actual
|
|
|
|
// content seprately. We still need to send something that equals the
|
2020-06-19 18:50:54 +03:00
|
|
|
// 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-06-19 20:23:14 +03:00
|
|
|
lines := m.visibleLines()
|
2020-05-26 02:57:58 +03:00
|
|
|
|
|
|
|
// Fill empty space with newlines
|
|
|
|
extraLines := ""
|
|
|
|
if len(lines) < m.Height {
|
2021-09-16 18:04:08 +03:00
|
|
|
extraLines = strings.Repeat("\n", max(0, m.Height-len(lines)))
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(lines, "\n") + extraLines
|
|
|
|
}
|
|
|
|
|
|
|
|
// ETC
|
|
|
|
|
2020-10-25 22:17:11 +03:00
|
|
|
func clamp(v, low, high int) int {
|
|
|
|
return min(high, max(low, v))
|
|
|
|
}
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
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
|
|
|
|
}
|