Simplify pager component and fix pager example
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 7562f29 · Browse files · 2 files·+75 added−19 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+75 added−19 removed
- other:+75 added−19 removed
Provenance
- Commit
25f8b8c- Tree
8421ab2- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
7562f29
2 files changed+75 added−19 removed
modified:examples/pager/main.go+70 added-3 removed
8 unmodified lines9 "github.com/charmbracelet/boba/pager"10 )11 12 func main() {13+14+ // Load some text to render15 content, err := ioutil.ReadFile("artichoke.md")16 if err != nil {17 fmt.Println("could not load file:", err)18 os.Exit(1)19 }20 21+ // Use the full size of the terminal in its "Alternate Screen Buffer"22 boba.AltScreen()23 defer boba.ExitAltScreen()24+25 if err := boba.NewProgram(22- pager.Init(string(content)),23- pager.Update,24- pager.View,26+ initialize(string(content)),27+ update,28+ view,29 ).Start(); err != nil {30 fmt.Println("could not run program:", err)31 os.Exit(1)32 }33 }34+35+type terminalSizeMsg struct {36+ width int37+ height int38+ err error39+}40+41+func (t terminalSizeMsg) Size() (int, int) { return t.width, t.height }42+func (t terminalSizeMsg) Error() error { return t.err }43+44+type model struct {45+ err error46+ content string47+ ready bool48+ pager pager.Model49+}50+51+func initialize(content string) func() (boba.Model, boba.Cmd) {52+ return func() (boba.Model, boba.Cmd) {53+ return model{54+ content: content,55+ }, getTerminalSize()56+ }57+}58+59+func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {60+ m, _ := mdl.(model)61+62+ switch msg := msg.(type) {63+ case boba.KeyMsg:64+ if msg.Type == boba.KeyCtrlC {65+ return m, boba.Quit66+ }67+ m.pager, _ = pager.Update(msg, m.pager)68+ case terminalSizeMsg:69+ if msg.Error() != nil {70+ m.err = msg.Error()71+ break72+ }73+ w, h := msg.Size()74+ m.pager = pager.NewModel(w, h)75+ m.pager.SetContent(m.content)76+ m.ready = true77+ }78+79+ return m, nil80+}81+82+func view(mdl boba.Model) string {83+ m, _ := mdl.(model)84+ if m.err != nil {85+ return "\nError:" + m.err.Error()86+ } else if m.ready {87+ return "\n" + pager.View(m.pager)88+ }89+ return "\nInitalizing..."90+}91+92+func getTerminalSize() boba.Cmd {93+ return boba.GetTerminalSize(func(w, h int, err error) boba.TerminalSizeMsg {94+ return terminalSizeMsg{width: w, height: h, err: err}95+ })96+}modified:pager/pager.go+5 added-16 removed
1 package pager2 3 import (4- "errors"4 "strings"5 6 "github.com/charmbracelet/boba"7 )8 unmodified lines16 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 }8 unmodified lines33 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,1 unmodified line43 }44 45 // UPDATE46 46-func Update(msg boba.Msg, model boba.Model) (boba.Model, boba.Cmd) {47- m, ok := model.(Model)48- if !ok {49- return Model{50- Err: errors.New("could not perform assertion on model in update in pager; are you sure you passed the correct model?"),51- }, nil52- }53-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() {44 unmodified lines96 97 }98 }99 106- return model, nil100+ return m, nil101 }102 103 // VIEW104 105 // View renders the viewport into a string112-func View(model boba.Model) string {113- m, ok := model.(Model)114- if !ok {115- return "could not perform assertion on model in view in pager; are you sure you passed the correct model?"116- }117-106+func View(m Model) string {107 if m.Err != nil {108 return m.Err.Error()109 }110 32 unmodified lines