Skip to content
Myr

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

2 files+75 added−19 removed

modified:examples/pager/main.go+70 added-3 removed
8 unmodified lines
9 "github.com/charmbracelet/boba/pager"10)1112func main() {1314 // 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 }2021 // Use the full size of the terminal in its "Alternate Screen Buffer"22 boba.AltScreen()23 defer boba.ExitAltScreen()2425 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}3435type terminalSizeMsg struct {36 width int37 height int38 err error39}4041func (t terminalSizeMsg) Size() (int, int) { return t.width, t.height }42func (t terminalSizeMsg) Error() error { return t.err }4344type model struct {45 err error46 content string47 ready bool48 pager pager.Model49}5051func 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}5859func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {60 m, _ := mdl.(model)6162 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 }7879 return m, nil80}8182func 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}9192func 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
1package pager23import (4 "errors"4 "strings"56 "github.com/charmbracelet/boba"7)
8 unmodified lines
1617 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 }
8 unmodified lines
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,
1 unmodified line
43}4445// UPDATE4646func 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 }5347func Update(msg boba.Msg, m Model) (Model, boba.Cmd) {48 switch msg := msg.(type) {4950 case boba.KeyMsg:51 switch msg.String() {
44 unmodified lines
9697 }98 }99106 return model, nil100 return m, nil101}102103// VIEW104105// View renders the viewport into a string112func 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 }117106func View(m Model) string {107 if m.Err != nil {108 return m.Err.Error()109 }110
32 unmodified lines