2020-05-11 21:03:04 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-06-18 21:08:26 -04:00
|
|
|
"strings"
|
2020-05-11 21:03:04 -04:00
|
|
|
|
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-06-18 21:08:26 -04:00
|
|
|
"github.com/mattn/go-runewidth"
|
2020-05-11 21:03:04 -04:00
|
|
|
)
|
|
|
|
|
2020-06-17 15:00:13 -04:00
|
|
|
const (
|
2020-06-19 13:22:08 -04:00
|
|
|
// You usually won't need this unless you're processing some pretty stuff
|
|
|
|
// with some pretty complicated ANSI escape sequences. Turn it on if you
|
|
|
|
// notice flickering.
|
|
|
|
//
|
|
|
|
// Also note that high performance rendering only works for programs that
|
|
|
|
// use the full size of the terminal. We're enabling that below with
|
|
|
|
// tea.AltScreen().
|
|
|
|
useHighPerformanceRenderer = false
|
2020-06-18 21:08:26 -04:00
|
|
|
|
|
|
|
headerHeight = 3
|
|
|
|
footerHeight = 3
|
2020-06-17 15:00:13 -04:00
|
|
|
)
|
|
|
|
|
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-06-19 14:14:15 -04:00
|
|
|
// Set PAGER_LOG to a path to log to a file. For example:
|
2020-06-18 21:08:26 -04:00
|
|
|
//
|
|
|
|
// export PAGER_LOG=debug.log
|
|
|
|
//
|
2020-06-19 14:14:15 -04:00
|
|
|
// This becomes handy when debugging stuff since you can't debug to stdout
|
|
|
|
// because the UI is occupying it!
|
2020-06-23 12:01:23 -04:00
|
|
|
path := os.Getenv("PAGER_LOG")
|
|
|
|
if path != "" {
|
|
|
|
f, err := tea.LogToFile(path, "pager")
|
2020-05-25 20:24:17 -04:00
|
|
|
if err != nil {
|
2020-06-23 12:01:23 -04:00
|
|
|
fmt.Printf("Could not open file %s: %v", path, err)
|
2020-05-25 20:24:17 -04:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
}
|
|
|
|
|
2020-06-23 12:01:23 -04:00
|
|
|
p := tea.NewProgram(initialize(string(content)), update, view)
|
|
|
|
|
2020-06-19 14:14:15 -04:00
|
|
|
// Use the full size of the terminal in its "alternate screen buffer"
|
2020-06-23 12:01:23 -04:00
|
|
|
p.EnterAltScreen()
|
|
|
|
defer p.ExitAltScreen()
|
|
|
|
|
|
|
|
// We also turn on mouse support so we can track the mouse wheel
|
|
|
|
p.EnableMouseCellMotion()
|
|
|
|
defer p.DisableMouseCellMotion()
|
|
|
|
|
|
|
|
if err := p.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-19 14:14:15 -04:00
|
|
|
// Store content in the model so we can hand it off to the viewport
|
|
|
|
// later.
|
|
|
|
content: content,
|
2020-06-18 13:54:12 -04:00
|
|
|
}, 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:
|
2020-06-19 14:14:15 -04:00
|
|
|
// Ctrl+c exits
|
2020-05-25 19:26:40 -04:00
|
|
|
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-18 21:08:26 -04:00
|
|
|
verticalMargins := headerHeight + footerHeight
|
2020-06-17 15:00:13 -04:00
|
|
|
|
2020-05-25 20:24:17 -04:00
|
|
|
if !m.ready {
|
2020-06-19 14:14:15 -04:00
|
|
|
// Since this program is using the full size of the viewport we need
|
|
|
|
// to wait until we've received the window dimensions before we
|
|
|
|
// can initialize the viewport. The initial dimensions come in
|
|
|
|
// quickly, though asynchronously, which is why we wait for them
|
|
|
|
// here.
|
2020-07-29 20:05:54 -04:00
|
|
|
m.viewport = viewport.Model{Width: msg.Width, Height: msg.Height - verticalMargins}
|
2020-06-18 21:08:26 -04:00
|
|
|
m.viewport.YPosition = headerHeight
|
|
|
|
m.viewport.HighPerformanceRendering = useHighPerformanceRenderer
|
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
|
2020-06-18 21:08:26 -04:00
|
|
|
m.viewport.Height = msg.Height - verticalMargins
|
2020-05-25 20:24:17 -04:00
|
|
|
}
|
|
|
|
|
2020-06-18 21:08:26 -04:00
|
|
|
if useHighPerformanceRenderer {
|
2020-06-19 14:14:15 -04:00
|
|
|
// Render (or re-render) the whole viewport. Necessary both to
|
|
|
|
// initialize the viewport and when the window is resized.
|
|
|
|
//
|
|
|
|
// This is needed for high-performance rendering only.
|
2020-06-18 21:08:26 -04:00
|
|
|
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
|
2020-06-19 14:14:15 -04:00
|
|
|
//
|
2020-06-17 19:43:33 -04:00
|
|
|
m.viewport, cmd = viewport.Update(msg, m.viewport)
|
2020-06-18 21:08:26 -04:00
|
|
|
if useHighPerformanceRenderer {
|
|
|
|
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 21:08:26 -04:00
|
|
|
m, _ := mdl.(model)
|
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 21:08:26 -04:00
|
|
|
headerTop := "╭───────────╮"
|
|
|
|
headerMid := "│ Mr. Pager ├"
|
|
|
|
headerBot := "╰───────────╯"
|
|
|
|
headerMid += strings.Repeat("─", m.viewport.Width-runewidth.StringWidth(headerMid))
|
|
|
|
header := fmt.Sprintf("%s\n%s\n%s", headerTop, headerMid, headerBot)
|
|
|
|
|
|
|
|
footerTop := "╭──────╮"
|
2020-06-19 12:29:59 -04:00
|
|
|
footerMid := fmt.Sprintf("┤ %3.f%% │", m.viewport.ScrollPercent()*100)
|
2020-06-18 21:08:26 -04:00
|
|
|
footerBot := "╰──────╯"
|
2020-06-19 12:29:59 -04:00
|
|
|
gapSize := m.viewport.Width - runewidth.StringWidth(footerMid)
|
|
|
|
footerTop = strings.Repeat(" ", gapSize) + footerTop
|
|
|
|
footerMid = strings.Repeat("─", gapSize) + footerMid
|
|
|
|
footerBot = strings.Repeat(" ", gapSize) + footerBot
|
2020-06-19 14:14:15 -04:00
|
|
|
footer := fmt.Sprintf("%s\n%s\n%s", footerTop, footerMid, footerBot)
|
2020-06-18 21:08:26 -04:00
|
|
|
|
2020-06-19 12:29:59 -04:00
|
|
|
return fmt.Sprintf("%s\n%s\n%s", header, viewport.View(m.viewport), footer)
|
2020-05-26 09:53:15 -04:00
|
|
|
}
|