2020-05-19 13:18:32 -04:00
|
|
|
package viewport
|
2020-05-12 17:05:16 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2020-05-12 17:05:16 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// MODEL
|
|
|
|
|
|
|
|
type Model struct {
|
2020-05-14 10:45:53 -04:00
|
|
|
Err error
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
Y int
|
2020-05-12 17:05:16 -04:00
|
|
|
|
|
|
|
lines []string
|
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:46 -04:00
|
|
|
// Scrollpercent returns the amount scrolled as a float between 0 and 1.
|
2020-05-14 15:04:18 -04:00
|
|
|
func (m Model) ScrollPercent() float64 {
|
|
|
|
if m.Height >= len(m.lines) {
|
|
|
|
return 1.0
|
|
|
|
}
|
|
|
|
y := float64(m.Y)
|
|
|
|
h := float64(m.Height)
|
|
|
|
t := float64(len(m.lines))
|
2020-05-14 15:10:07 -04:00
|
|
|
return y / (t - h)
|
2020-05-14 15:04:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:46 -04:00
|
|
|
// SetContent set the pager's text content.
|
2020-05-15 14:14:17 -04:00
|
|
|
func (m *Model) SetContent(s string) {
|
2020-05-12 17:05:16 -04:00
|
|
|
s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings
|
|
|
|
m.lines = strings.Split(s, "\n")
|
|
|
|
}
|
|
|
|
|
2020-05-15 16:08:58 -04:00
|
|
|
// NewModel creates a new pager model. Pass the dimensions of the pager.
|
2020-05-14 10:45:53 -04:00
|
|
|
func NewModel(width, height int) Model {
|
2020-05-12 17:05:16 -04:00
|
|
|
return Model{
|
2020-05-14 10:45:53 -04:00
|
|
|
Width: width,
|
|
|
|
Height: height,
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:18:32 -04:00
|
|
|
// ViewDown moves the view down by the number of lines in the viewport.
|
2020-05-25 08:02:46 -04:00
|
|
|
// Basically, "page down".
|
2020-05-19 13:18:32 -04:00
|
|
|
func (m *Model) ViewDown() {
|
|
|
|
m.Y = min(len(m.lines)-m.Height, m.Y+m.Height)
|
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:46 -04:00
|
|
|
// ViewUp moves the view up by one height of the viewport. Basically, "page up".
|
2020-05-19 13:18:32 -04:00
|
|
|
func (m *Model) ViewUp() {
|
|
|
|
m.Y = max(0, m.Y-m.Height)
|
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:46 -04:00
|
|
|
// HalfViewUp moves the view up by half the height of the viewport.
|
2020-05-19 13:18:32 -04:00
|
|
|
func (m *Model) HalfViewUp() {
|
|
|
|
m.Y = max(0, m.Y-m.Height/2)
|
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:46 -04:00
|
|
|
// HalfViewDown moves the view down by half the height of the viewport.
|
2020-05-19 13:18:32 -04:00
|
|
|
func (m *Model) HalfViewDown() {
|
|
|
|
m.Y = min(len(m.lines)-m.Height, m.Y+m.Height/2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LineDown moves the view up by the given number of lines.
|
|
|
|
func (m *Model) LineDown(n int) {
|
|
|
|
m.Y = min(len(m.lines)-m.Height, m.Y+n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LineDown moves the view down by the given number of lines.
|
|
|
|
func (m *Model) LineUp(n int) {
|
|
|
|
m.Y = max(0, m.Y-n)
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:05:16 -04:00
|
|
|
// UPDATE
|
|
|
|
|
2020-05-19 13:18:32 -04:00
|
|
|
// Update runs the update loop with default keybindings. To define your own
|
|
|
|
// keybindings use the methods on Model.
|
2020-05-25 19:26:40 -04:00
|
|
|
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
2020-05-12 17:05:16 -04:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
case tea.KeyMsg:
|
2020-05-12 17:05:16 -04:00
|
|
|
switch msg.String() {
|
|
|
|
// Down one page
|
2020-05-13 22:06:35 -04:00
|
|
|
case "pgdown":
|
|
|
|
fallthrough
|
2020-05-19 14:41:55 -04:00
|
|
|
case " ": // spacebar
|
2020-05-12 17:05:16 -04:00
|
|
|
fallthrough
|
|
|
|
case "f":
|
2020-05-19 13:18:32 -04:00
|
|
|
m.ViewDown()
|
2020-05-12 17:05:16 -04:00
|
|
|
return m, nil
|
|
|
|
|
2020-05-19 13:18:32 -04:00
|
|
|
// Up one page
|
|
|
|
case "pgup":
|
|
|
|
fallthrough
|
|
|
|
case "b":
|
|
|
|
m.ViewUp()
|
2020-05-12 17:05:16 -04:00
|
|
|
return m, nil
|
|
|
|
|
|
|
|
// Down half page
|
|
|
|
case "d":
|
2020-05-19 13:18:32 -04:00
|
|
|
m.HalfViewDown()
|
2020-05-12 17:05:16 -04:00
|
|
|
return m, nil
|
|
|
|
|
2020-05-19 13:18:32 -04:00
|
|
|
// Up half page
|
|
|
|
case "u":
|
|
|
|
m.HalfViewUp()
|
2020-05-12 17:05:16 -04:00
|
|
|
return m, nil
|
|
|
|
|
|
|
|
// Down one line
|
|
|
|
case "down":
|
|
|
|
fallthrough
|
|
|
|
case "j":
|
2020-05-19 13:18:32 -04:00
|
|
|
m.LineDown(1)
|
2020-05-12 17:05:16 -04:00
|
|
|
return m, nil
|
|
|
|
|
2020-05-19 13:18:32 -04:00
|
|
|
// Up one line
|
|
|
|
case "up":
|
|
|
|
fallthrough
|
|
|
|
case "k":
|
|
|
|
m.LineUp(1)
|
2020-05-14 10:45:53 -04:00
|
|
|
return m, nil
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 16:08:58 -04:00
|
|
|
return m, nil
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// VIEW
|
|
|
|
|
2020-05-25 08:02:46 -04:00
|
|
|
// View renders the viewport into a string.
|
2020-05-15 16:08:58 -04:00
|
|
|
func View(m Model) string {
|
2020-05-12 17:05:16 -04:00
|
|
|
if m.Err != nil {
|
|
|
|
return m.Err.Error()
|
|
|
|
}
|
|
|
|
|
2020-05-14 17:56:54 -04:00
|
|
|
var lines []string
|
2020-05-12 17:05:16 -04:00
|
|
|
|
2020-05-14 17:56:54 -04:00
|
|
|
if len(m.lines) > 0 {
|
|
|
|
top := max(0, m.Y)
|
|
|
|
bottom := min(len(m.lines), m.Y+m.Height)
|
|
|
|
lines = m.lines[top:bottom]
|
|
|
|
}
|
2020-05-14 15:04:39 -04:00
|
|
|
|
2020-05-14 15:10:07 -04:00
|
|
|
// Fill empty space with newlines
|
2020-05-14 15:04:39 -04:00
|
|
|
extraLines := ""
|
|
|
|
if len(lines) < m.Height {
|
|
|
|
extraLines = strings.Repeat("\n", m.Height-len(lines))
|
|
|
|
}
|
|
|
|
|
2020-05-14 17:56:54 -04:00
|
|
|
return strings.Join(lines, "\n") + extraLines
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|