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
|
|
|
)
|
|
|
|
|
2020-06-17 15:00:13 -04:00
|
|
|
const (
|
|
|
|
viewportTopMargin = 2
|
|
|
|
viewportBottomMargin = 2
|
|
|
|
)
|
|
|
|
|
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 model struct {
|
2020-05-25 19:26:40 -04:00
|
|
|
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-06-18 13:54:12 -04:00
|
|
|
content: content, // keep content in the model
|
|
|
|
}, nil
|
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)
|
|
|
|
|
2020-06-18 12:33:58 -04:00
|
|
|
var (
|
|
|
|
cmd tea.Cmd
|
|
|
|
cmds []tea.Cmd
|
|
|
|
)
|
|
|
|
|
2020-05-15 16:08:58 -04:00
|
|
|
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 20:24:17 -04:00
|
|
|
|
2020-06-18 13:54:12 -04:00
|
|
|
case tea.WindowSizeMsg:
|
2020-06-17 15:00:13 -04:00
|
|
|
viewportVerticalMargins := viewportTopMargin + viewportBottomMargin
|
|
|
|
|
2020-05-25 20:24:17 -04:00
|
|
|
if !m.ready {
|
2020-06-18 13:54:12 -04:00
|
|
|
m.viewport = viewport.NewModel(msg.Width, msg.Height-viewportVerticalMargins)
|
2020-06-17 19:43:33 -04:00
|
|
|
m.viewport.YPosition = viewportTopMargin
|
|
|
|
m.viewport.HighPerformanceRendering = true
|
2020-05-25 20:24:17 -04:00
|
|
|
m.viewport.SetContent(m.content)
|
|
|
|
m.ready = true
|
2020-05-26 09:53:15 -04:00
|
|
|
} else {
|
2020-06-18 13:54:12 -04:00
|
|
|
m.viewport.Width = msg.Width
|
|
|
|
m.viewport.Height = msg.Height - viewportBottomMargin
|
2020-05-25 20:24:17 -04:00
|
|
|
}
|
|
|
|
|
2020-06-18 12:33:58 -04:00
|
|
|
// Render (or re-render) the whole viewport
|
|
|
|
cmds = append(cmds, viewport.Sync(m.viewport))
|
2020-05-15 16:08:58 -04:00
|
|
|
}
|
|
|
|
|
2020-06-17 19:43:33 -04:00
|
|
|
// Because we're using the viewport's default update function (with pager-
|
|
|
|
// style navigation) it's important that the viewport's update function:
|
|
|
|
//
|
|
|
|
// * Recieves messages from the Bubble Tea runtime
|
|
|
|
// * Returns commands to the Bubble Tea runtime
|
|
|
|
m.viewport, cmd = viewport.Update(msg, m.viewport)
|
2020-06-18 12:33:58 -04:00
|
|
|
cmds = append(cmds, cmd)
|
2020-06-17 19:43:33 -04:00
|
|
|
|
2020-06-18 12:33:58 -04:00
|
|
|
return m, tea.Batch(cmds...)
|
2020-05-15 16:08:58 -04:00
|
|
|
}
|
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
func view(mdl tea.Model) string {
|
2020-06-18 13:54:12 -04:00
|
|
|
m, ok := mdl.(model)
|
|
|
|
if !ok {
|
|
|
|
return "\n Error: could not perform assertion on model in view."
|
2020-05-15 16:08:58 -04:00
|
|
|
}
|
|
|
|
|
2020-06-18 13:54:12 -04:00
|
|
|
if !m.ready {
|
|
|
|
return "\n Initalizing..."
|
|
|
|
}
|
2020-05-26 09:53:15 -04:00
|
|
|
|
2020-06-18 13:54:12 -04:00
|
|
|
return fmt.Sprintf(
|
|
|
|
"── Mr. Pager ──\n\n%s\n\n── %3.f%% ──",
|
|
|
|
viewport.View(m.viewport),
|
|
|
|
m.viewport.ScrollPercent()*100,
|
|
|
|
)
|
2020-05-26 09:53:15 -04:00
|
|
|
}
|