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
Provenance
- Commit
dafb022- Tree
dacefa4- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
822c234
2 files changed+171 added−142 removed
deleted:pager/pager.go-142 removed
1-package pager2-3-import (4- "strings"5-6- "github.com/charmbracelet/boba"7-)8-9-// MODEL10-11-type Model struct {12- Err error13- Width int14- Height int15- Y int16-17- lines []string18-}19-20-// Scrollpercent returns the amount scrolled as a float between 0 and 121-func (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-}30-31-// SetContent set the pager's text content32-func (m *Model) SetContent(s string) {33- s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings34- m.lines = strings.Split(s, "\n")35-}36-37-// NewModel creates a new pager model. Pass the dimensions of the pager.38-func NewModel(width, height int) Model {39- return Model{40- Width: width,41- Height: height,42- }43-}44-45-// UPDATE46-47-func Update(msg boba.Msg, m Model) (Model, boba.Cmd) {48- switch msg := msg.(type) {49-50- case boba.KeyMsg:51- switch msg.String() {52-53- // Up one page54- case "pgup":55- fallthrough56- case "b":57- m.Y = max(0, m.Y-m.Height)58- return m, nil59-60- // 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, nil68-69- // Up half page70- case "u":71- m.Y = max(0, m.Y-m.Height/2)72- return m, nil73-74- // Down half page75- case "d":76- m.Y = min(len(m.lines)-m.Height, m.Y+m.Height/2)77- return m, nil78-79- // Up one line80- case "up":81- fallthrough82- case "k":83- m.Y = max(0, m.Y-1)84- return m, nil85-86- // Down one line87- case "down":88- fallthrough89- case "j":90- m.Y = min(len(m.lines)-m.Height, m.Y+1)91- return m, nil92-93- // Re-render94- case "ctrl+l":95- return m, nil96-97- }98- }99-100- return m, nil101-}102-103-// VIEW104-105-// View renders the viewport into a string106-func View(m Model) string {107- if m.Err != nil {108- return m.Err.Error()109- }110-111- var lines []string112-113- 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- }118-119- // Fill empty space with newlines120- extraLines := ""121- if len(lines) < m.Height {122- extraLines = strings.Repeat("\n", m.Height-len(lines))123- }124-125- return strings.Join(lines, "\n") + extraLines126-}127-128-// ETC129-130-func min(a, b int) int {131- if a < b {132- return a133- }134- return b135-}136-137-func max(a, b int) int {138- if a > b {139- return a140- }141- return b142-}added:viewport/viewport.go+171 added
1+package viewport2+3+import (4+ "strings"5+6+ "github.com/charmbracelet/boba"7+)8+9+// MODEL10+11+type Model struct {12+ Err error13+ Width int14+ Height int15+ Y int16+17+ lines []string18+}19+20+// Scrollpercent returns the amount scrolled as a float between 0 and 121+func (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+}30+31+// SetContent set the pager's text content32+func (m *Model) SetContent(s string) {33+ s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings34+ m.lines = strings.Split(s, "\n")35+}36+37+// NewModel creates a new pager model. Pass the dimensions of the pager.38+func NewModel(width, height int) Model {39+ return Model{40+ Width: width,41+ Height: height,42+ }43+}44+45+// ViewDown moves the view down by the number of lines in the viewport.46+// Basically, "page down."47+func (m *Model) ViewDown() {48+ m.Y = min(len(m.lines)-m.Height, m.Y+m.Height)49+}50+51+// ViewUp moves the view up by one height of the viewport. Basically, "page up."52+func (m *Model) ViewUp() {53+ m.Y = max(0, m.Y-m.Height)54+}55+56+// HalfViewUp moves the view up by half the height of the viewport57+func (m *Model) HalfViewUp() {58+ m.Y = max(0, m.Y-m.Height/2)59+}60+61+// HalfViewDown moves the view down by half the height of the viewport62+func (m *Model) HalfViewDown() {63+ m.Y = min(len(m.lines)-m.Height, m.Y+m.Height/2)64+}65+66+// LineDown moves the view up by the given number of lines.67+func (m *Model) LineDown(n int) {68+ m.Y = min(len(m.lines)-m.Height, m.Y+n)69+}70+71+// LineDown moves the view down by the given number of lines.72+func (m *Model) LineUp(n int) {73+ m.Y = max(0, m.Y-n)74+}75+76+// UPDATE77+78+// Update runs the update loop with default keybindings. To define your own79+// keybindings use the methods on Model.80+func Update(msg boba.Msg, m Model) (Model, boba.Cmd) {81+ switch msg := msg.(type) {82+83+ case boba.KeyMsg:84+ switch msg.String() {85+86+ // Down one page87+ case "pgdown":88+ fallthrough89+ case "space":90+ fallthrough91+ case "f":92+ m.ViewDown()93+ return m, nil94+95+ // Up one page96+ case "pgup":97+ fallthrough98+ case "b":99+ m.ViewUp()100+ return m, nil101+102+ // Down half page103+ case "d":104+ m.HalfViewDown()105+ return m, nil106+107+ // Up half page108+ case "u":109+ m.HalfViewUp()110+ return m, nil111+112+ // Down one line113+ case "down":114+ fallthrough115+ case "j":116+ m.LineDown(1)117+ return m, nil118+119+ // Up one line120+ case "up":121+ fallthrough122+ case "k":123+ m.LineUp(1)124+ return m, nil125+126+ }127+ }128+129+ return m, nil130+}131+132+// VIEW133+134+// View renders the viewport into a string135+func View(m Model) string {136+ if m.Err != nil {137+ return m.Err.Error()138+ }139+140+ var lines []string141+142+ 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+ }147+148+ // Fill empty space with newlines149+ extraLines := ""150+ if len(lines) < m.Height {151+ extraLines = strings.Repeat("\n", m.Height-len(lines))152+ }153+154+ return strings.Join(lines, "\n") + extraLines155+}156+157+// ETC158+159+func min(a, b int) int {160+ if a < b {161+ return a162+ }163+ return b164+}165+166+func max(a, b int) int {167+ if a > b {168+ return a169+ }170+ return b171+}