2020-05-11 21:03:04 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2020-05-25 20:10:15 -04:00
|
|
|
"github.com/charmbracelet/bubbles/viewport"
|
2020-05-25 19:26:40 -04:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2020-05-11 21:03:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-05-15 16:08:58 -04:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2020-05-25 20:24:17 -04:00
|
|
|
// Set PAGER_LOG to a path to log to a file. For example,
|
|
|
|
// export PAGER_LOG=debug.log
|
|
|
|
if os.Getenv("PAGER_LOG") != "" {
|
|
|
|
p := os.Getenv("PAGER_LOG")
|
|
|
|
f, err := tea.LogToFile(p, "pager")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Could not open file %s: %v", p, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
}
|
|
|
|
|
2020-05-15 16:08:58 -04:00
|
|
|
// Use the full size of the terminal in its "Alternate Screen Buffer"
|
2020-05-25 19:26:40 -04:00
|
|
|
tea.AltScreen()
|
|
|
|
defer tea.ExitAltScreen()
|
2020-05-15 16:08:58 -04:00
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
if err := tea.NewProgram(
|
2020-05-15 16:08:58 -04:00
|
|
|
initialize(string(content)),
|
|
|
|
update,
|
|
|
|
view,
|
2020-05-12 17:56:30 -04:00
|
|
|
).Start(); err != nil {
|
2020-05-11 21:03:04 -04:00
|
|
|
fmt.Println("could not run program:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 16:08:58 -04:00
|
|
|
|
|
|
|
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 }
|
|
|
|
|
2020-05-25 20:24:17 -04:00
|
|
|
type resizeMsg struct{}
|
|
|
|
|
2020-05-15 16:08:58 -04:00
|
|
|
type model struct {
|
2020-05-25 19:26:40 -04:00
|
|
|
err error
|
|
|
|
content string
|
|
|
|
ready bool
|
|
|
|
viewport viewport.Model
|
2020-05-15 16:08:58 -04:00
|
|
|
}
|
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
func initialize(content string) func() (tea.Model, tea.Cmd) {
|
|
|
|
return func() (tea.Model, tea.Cmd) {
|
2020-05-15 16:08:58 -04:00
|
|
|
return model{
|
2020-05-25 20:24:17 -04:00
|
|
|
content: content, // keep content in the model
|
|
|
|
}, tea.Batch(
|
|
|
|
|
|
|
|
// Get terminal size asynchronously
|
|
|
|
getTerminalSize(),
|
|
|
|
|
|
|
|
// We're not doing anything with it in this example, but this
|
|
|
|
// is now you'd listen for resizes.
|
|
|
|
tea.OnResize(func() tea.Msg { return resizeMsg{} }),
|
|
|
|
)
|
2020-05-15 16:08:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
|
2020-05-15 16:08:58 -04:00
|
|
|
m, _ := mdl.(model)
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
2020-05-25 19:26:40 -04:00
|
|
|
case tea.KeyMsg:
|
|
|
|
if msg.Type == tea.KeyCtrlC {
|
|
|
|
return m, tea.Quit
|
2020-05-15 16:08:58 -04:00
|
|
|
}
|
2020-05-25 19:26:40 -04:00
|
|
|
m.viewport, _ = viewport.Update(msg, m.viewport)
|
2020-05-25 20:24:17 -04:00
|
|
|
|
2020-05-15 16:08:58 -04:00
|
|
|
case terminalSizeMsg:
|
|
|
|
if msg.Error() != nil {
|
|
|
|
m.err = msg.Error()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
w, h := msg.Size()
|
2020-05-25 20:24:17 -04:00
|
|
|
if !m.ready {
|
|
|
|
m.viewport = viewport.NewModel(w, h)
|
|
|
|
m.viewport.SetContent(m.content)
|
|
|
|
m.ready = true
|
|
|
|
}
|
|
|
|
|
|
|
|
case resizeMsg:
|
|
|
|
return m, getTerminalSize()
|
2020-05-15 16:08:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
func view(mdl tea.Model) string {
|
2020-05-15 16:08:58 -04:00
|
|
|
m, _ := mdl.(model)
|
|
|
|
if m.err != nil {
|
|
|
|
return "\nError:" + m.err.Error()
|
|
|
|
} else if m.ready {
|
2020-05-25 19:26:40 -04:00
|
|
|
return "\n" + viewport.View(m.viewport)
|
2020-05-15 16:08:58 -04:00
|
|
|
}
|
|
|
|
return "\nInitalizing..."
|
|
|
|
}
|
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
func getTerminalSize() tea.Cmd {
|
|
|
|
return tea.GetTerminalSize(func(w, h int, err error) tea.TerminalSizeMsg {
|
2020-05-15 16:08:58 -04:00
|
|
|
return terminalSizeMsg{width: w, height: h, err: err}
|
|
|
|
})
|
|
|
|
}
|