bubbletea/examples/pager/main.go

97 lines
1.8 KiB
Go
Raw Normal View History

2020-05-11 21:03:04 -04:00
package main
import (
"fmt"
"io/ioutil"
"os"
2020-05-12 16:39:08 -04:00
"github.com/charmbracelet/boba"
"github.com/charmbracelet/boba/pager"
2020-05-11 21:03:04 -04:00
)
func main() {
// Load some text to render
2020-05-11 21:03:04 -04:00
content, err := ioutil.ReadFile("artichoke.md")
if err != nil {
fmt.Println("could not load file:", err)
os.Exit(1)
}
// Use the full size of the terminal in its "Alternate Screen Buffer"
2020-05-12 16:39:08 -04:00
boba.AltScreen()
defer boba.ExitAltScreen()
if err := boba.NewProgram(
initialize(string(content)),
update,
view,
).Start(); err != nil {
2020-05-11 21:03:04 -04:00
fmt.Println("could not run program:", err)
os.Exit(1)
}
}
type terminalSizeMsg struct {
width int
height int
err error
}
func (t terminalSizeMsg) Size() (int, int) { return t.width, t.height }
func (t terminalSizeMsg) Error() error { return t.err }
type model struct {
err error
content string
ready bool
pager pager.Model
}
func initialize(content string) func() (boba.Model, boba.Cmd) {
return func() (boba.Model, boba.Cmd) {
return model{
content: content,
}, getTerminalSize()
}
}
func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {
m, _ := mdl.(model)
switch msg := msg.(type) {
case boba.KeyMsg:
if msg.Type == boba.KeyCtrlC {
return m, boba.Quit
}
m.pager, _ = pager.Update(msg, m.pager)
case terminalSizeMsg:
if msg.Error() != nil {
m.err = msg.Error()
break
}
w, h := msg.Size()
m.pager = pager.NewModel(w, h)
m.pager.SetContent(m.content)
m.ready = true
}
return m, nil
}
func view(mdl boba.Model) string {
m, _ := mdl.(model)
if m.err != nil {
return "\nError:" + m.err.Error()
} else if m.ready {
return "\n" + pager.View(m.pager)
}
return "\nInitalizing..."
}
func getTerminalSize() boba.Cmd {
return boba.GetTerminalSize(func(w, h int, err error) boba.TerminalSizeMsg {
return terminalSizeMsg{width: w, height: h, err: err}
})
}