2020-05-11 21:03:04 -04:00
|
|
|
package main
|
|
|
|
|
2020-10-14 11:51:04 -04:00
|
|
|
// An example program demonstrating the pager component from the Bubbles
|
|
|
|
// component library.
|
|
|
|
|
2020-05-11 21:03:04 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"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"
|
2021-09-02 13:25:37 -04:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
2020-05-11 21:03:04 -04:00
|
|
|
)
|
|
|
|
|
2021-09-02 13:25:37 -04:00
|
|
|
// You generally won't need this unless you're processing stuff with
|
|
|
|
// complicated ANSI escape sequences. Turn it on if you notice flickering.
|
|
|
|
//
|
|
|
|
// Also keep in mind that high performance rendering only works for programs
|
|
|
|
// that use the full size of the terminal. We're enabling that below with
|
|
|
|
// tea.EnterAltScreen().
|
|
|
|
const useHighPerformanceRenderer = false
|
2020-06-17 15:00:13 -04:00
|
|
|
|
2021-09-02 18:13:56 -04:00
|
|
|
var (
|
|
|
|
titleStyle = func() lipgloss.Style {
|
|
|
|
b := lipgloss.RoundedBorder()
|
|
|
|
b.Right = "├"
|
|
|
|
return lipgloss.NewStyle().BorderStyle(b).Padding(0, 1)
|
|
|
|
}()
|
|
|
|
|
|
|
|
infoStyle = func() lipgloss.Style {
|
|
|
|
b := lipgloss.RoundedBorder()
|
|
|
|
b.Left = "┤"
|
|
|
|
return titleStyle.Copy().BorderStyle(b)
|
|
|
|
}()
|
|
|
|
)
|
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-10-15 19:48:42 -04:00
|
|
|
func (m model) Init() tea.Cmd {
|
|
|
|
return nil
|
2020-05-15 16:08:58 -04:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
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:
|
2021-05-01 09:28:58 -04:00
|
|
|
if k := msg.String(); k == "ctrl+c" || k == "q" || k == "esc" {
|
2020-05-25 19:26:40 -04:00
|
|
|
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:
|
2021-09-02 13:25:37 -04:00
|
|
|
headerHeight := lipgloss.Height(m.headerView())
|
|
|
|
footerHeight := lipgloss.Height(m.footerView())
|
|
|
|
verticalMarginHeight := headerHeight + footerHeight
|
2020-06-17 15:00:13 -04:00
|
|
|
|
2020-05-25 20:24:17 -04:00
|
|
|
if !m.ready {
|
2021-09-02 13:25:37 -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
|
2020-06-19 14:14:15 -04:00
|
|
|
// quickly, though asynchronously, which is why we wait for them
|
|
|
|
// here.
|
2021-09-02 18:13:56 -04:00
|
|
|
m.viewport = viewport.New(msg.Width, msg.Height-verticalMarginHeight)
|
2021-09-02 13:25:37 -04:00
|
|
|
m.viewport.YPosition = headerHeight
|
2020-06-18 21:08:26 -04:00
|
|
|
m.viewport.HighPerformanceRendering = useHighPerformanceRenderer
|
2020-05-25 20:24:17 -04:00
|
|
|
m.viewport.SetContent(m.content)
|
|
|
|
m.ready = true
|
2021-09-17 16:31:31 -04:00
|
|
|
|
|
|
|
// This is only necessary for high performance rendering, which in
|
|
|
|
// most cases you won't need.
|
|
|
|
//
|
|
|
|
// Render the viewport one line below the header.
|
|
|
|
m.viewport.YPosition = headerHeight + 1
|
2020-05-26 09:53:15 -04:00
|
|
|
} else {
|
2020-06-18 13:54:12 -04:00
|
|
|
m.viewport.Width = msg.Width
|
2021-09-02 13:25:37 -04:00
|
|
|
m.viewport.Height = msg.Height - verticalMarginHeight
|
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
|
|
|
}
|
|
|
|
|
2021-09-02 13:25:37 -04:00
|
|
|
// Handle keyboard and mouse events in the viewport
|
2020-10-31 20:58:24 -04:00
|
|
|
m.viewport, cmd = m.viewport.Update(msg)
|
2021-09-02 13:25:37 -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-10-15 19:48:42 -04:00
|
|
|
func (m model) View() string {
|
2020-06-18 13:54:12 -04:00
|
|
|
if !m.ready {
|
2021-03-11 20:11:26 -05:00
|
|
|
return "\n Initializing..."
|
2020-06-18 13:54:12 -04:00
|
|
|
}
|
2021-09-02 13:25:37 -04:00
|
|
|
return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.viewport.View(), m.footerView())
|
|
|
|
}
|
2020-05-26 09:53:15 -04:00
|
|
|
|
2021-09-02 13:25:37 -04:00
|
|
|
func (m model) headerView() string {
|
2021-09-02 18:13:56 -04:00
|
|
|
title := titleStyle.Render("Mr. Pager")
|
|
|
|
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(title)))
|
|
|
|
return lipgloss.JoinHorizontal(lipgloss.Center, title, line)
|
2021-09-02 13:25:37 -04:00
|
|
|
}
|
2020-06-18 21:08:26 -04:00
|
|
|
|
2021-09-02 13:25:37 -04:00
|
|
|
func (m model) footerView() string {
|
2021-09-02 18:13:56 -04:00
|
|
|
info := infoStyle.Render(fmt.Sprintf("%3.f%%", m.viewport.ScrollPercent()*100))
|
|
|
|
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(info)))
|
|
|
|
return lipgloss.JoinHorizontal(lipgloss.Center, line, info)
|
2021-09-02 13:25:37 -04:00
|
|
|
}
|
2020-06-18 21:08:26 -04:00
|
|
|
|
2021-09-02 13:25:37 -04:00
|
|
|
func max(a, b int) int {
|
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
2020-05-26 09:53:15 -04:00
|
|
|
}
|
2021-09-02 18:13:56 -04:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Load some text for our viewport
|
2022-10-30 23:42:57 -04:00
|
|
|
content, err := os.ReadFile("artichoke.md")
|
2021-09-02 18:13:56 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("could not load file:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := tea.NewProgram(
|
|
|
|
model{content: string(content)},
|
|
|
|
tea.WithAltScreen(), // use the full size of the terminal in its "alternate screen buffer"
|
|
|
|
tea.WithMouseCellMotion(), // turn on mouse support so we can track the mouse wheel
|
|
|
|
)
|
|
|
|
|
2022-10-07 17:56:12 -04:00
|
|
|
if _, err := p.Run(); err != nil {
|
2021-09-02 18:13:56 -04:00
|
|
|
fmt.Println("could not run program:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|