Skip to content
Myr

Pager is now called viewport and allows for non-default keybindings

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christian Rocha · committed by Christian Rocha · parent 822c234 · Browse files · 2 files+171 added−142 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+171 added−142 removed

  • other:+171 added−142 removed

2 files+171 added−142 removed

deleted:pager/pager.go-142 removed
1package pager23import (4 "strings"56 "github.com/charmbracelet/boba"7)89// MODEL1011type Model struct {12 Err error13 Width int14 Height int15 Y int1617 lines []string18}1920// Scrollpercent returns the amount scrolled as a float between 0 and 121func (m Model) ScrollPercent() float64 {22 if m.Height >= len(m.lines) {23 return 1.024 }25 y := float64(m.Y)26 h := float64(m.Height)27 t := float64(len(m.lines))28 return y / (t - h)29}3031// SetContent set the pager's text content32func (m *Model) SetContent(s string) {33 s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings34 m.lines = strings.Split(s, "\n")35}3637// NewModel creates a new pager model. Pass the dimensions of the pager.38func NewModel(width, height int) Model {39 return Model{40 Width: width,41 Height: height,42 }43}4445// UPDATE4647func Update(msg boba.Msg, m Model) (Model, boba.Cmd) {48 switch msg := msg.(type) {4950 case boba.KeyMsg:51 switch msg.String() {5253 // Up one page54 case "pgup":55 fallthrough56 case "b":57 m.Y = max(0, m.Y-m.Height)58 return m, nil5960 // Down one page61 case "pgdown":62 fallthrough63 case "space":64 fallthrough65 case "f":66 m.Y = min(len(m.lines)-m.Height, m.Y+m.Height)67 return m, nil6869 // Up half page70 case "u":71 m.Y = max(0, m.Y-m.Height/2)72 return m, nil7374 // Down half page75 case "d":76 m.Y = min(len(m.lines)-m.Height, m.Y+m.Height/2)77 return m, nil7879 // Up one line80 case "up":81 fallthrough82 case "k":83 m.Y = max(0, m.Y-1)84 return m, nil8586 // Down one line87 case "down":88 fallthrough89 case "j":90 m.Y = min(len(m.lines)-m.Height, m.Y+1)91 return m, nil9293 // Re-render94 case "ctrl+l":95 return m, nil9697 }98 }99100 return m, nil101}102103// VIEW104105// View renders the viewport into a string106func View(m Model) string {107 if m.Err != nil {108 return m.Err.Error()109 }110111 var lines []string112113 if len(m.lines) > 0 {114 top := max(0, m.Y)115 bottom := min(len(m.lines), m.Y+m.Height)116 lines = m.lines[top:bottom]117 }118119 // Fill empty space with newlines120 extraLines := ""121 if len(lines) < m.Height {122 extraLines = strings.Repeat("\n", m.Height-len(lines))123 }124125 return strings.Join(lines, "\n") + extraLines126}127128// ETC129130func min(a, b int) int {131 if a < b {132 return a133 }134 return b135}136137func max(a, b int) int {138 if a > b {139 return a140 }141 return b142}
added:viewport/viewport.go+171 added
1package viewport23import (4 "strings"56 "github.com/charmbracelet/boba"7)89// MODEL1011type Model struct {12 Err error13 Width int14 Height int15 Y int1617 lines []string18}1920// Scrollpercent returns the amount scrolled as a float between 0 and 121func (m Model) ScrollPercent() float64 {22 if m.Height >= len(m.lines) {23 return 1.024 }25 y := float64(m.Y)26 h := float64(m.Height)27 t := float64(len(m.lines))28 return y / (t - h)29}3031// SetContent set the pager's text content32func (m *Model) SetContent(s string) {33 s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings34 m.lines = strings.Split(s, "\n")35}3637// NewModel creates a new pager model. Pass the dimensions of the pager.38func NewModel(width, height int) Model {39 return Model{40 Width: width,41 Height: height,42 }43}4445// ViewDown moves the view down by the number of lines in the viewport.46// Basically, "page down."47func (m *Model) ViewDown() {48 m.Y = min(len(m.lines)-m.Height, m.Y+m.Height)49}5051// ViewUp moves the view up by one height of the viewport. Basically, "page up."52func (m *Model) ViewUp() {53 m.Y = max(0, m.Y-m.Height)54}5556// HalfViewUp moves the view up by half the height of the viewport57func (m *Model) HalfViewUp() {58 m.Y = max(0, m.Y-m.Height/2)59}6061// HalfViewDown moves the view down by half the height of the viewport62func (m *Model) HalfViewDown() {63 m.Y = min(len(m.lines)-m.Height, m.Y+m.Height/2)64}6566// LineDown moves the view up by the given number of lines.67func (m *Model) LineDown(n int) {68 m.Y = min(len(m.lines)-m.Height, m.Y+n)69}7071// LineDown moves the view down by the given number of lines.72func (m *Model) LineUp(n int) {73 m.Y = max(0, m.Y-n)74}7576// UPDATE7778// Update runs the update loop with default keybindings. To define your own79// keybindings use the methods on Model.80func Update(msg boba.Msg, m Model) (Model, boba.Cmd) {81 switch msg := msg.(type) {8283 case boba.KeyMsg:84 switch msg.String() {8586 // Down one page87 case "pgdown":88 fallthrough89 case "space":90 fallthrough91 case "f":92 m.ViewDown()93 return m, nil9495 // Up one page96 case "pgup":97 fallthrough98 case "b":99 m.ViewUp()100 return m, nil101102 // Down half page103 case "d":104 m.HalfViewDown()105 return m, nil106107 // Up half page108 case "u":109 m.HalfViewUp()110 return m, nil111112 // Down one line113 case "down":114 fallthrough115 case "j":116 m.LineDown(1)117 return m, nil118119 // Up one line120 case "up":121 fallthrough122 case "k":123 m.LineUp(1)124 return m, nil125126 }127 }128129 return m, nil130}131132// VIEW133134// View renders the viewport into a string135func View(m Model) string {136 if m.Err != nil {137 return m.Err.Error()138 }139140 var lines []string141142 if len(m.lines) > 0 {143 top := max(0, m.Y)144 bottom := min(len(m.lines), m.Y+m.Height)145 lines = m.lines[top:bottom]146 }147148 // Fill empty space with newlines149 extraLines := ""150 if len(lines) < m.Height {151 extraLines = strings.Repeat("\n", m.Height-len(lines))152 }153154 return strings.Join(lines, "\n") + extraLines155}156157// ETC158159func min(a, b int) int {160 if a < b {161 return a162 }163 return b164}165166func max(a, b int) int {167 if a > b {168 return a169 }170 return b171}