Skip to content
Myr

Update pager example per viewport API update

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christian Rocha · committed by Christian Rocha · parent bdb0476 · Browse files · 1 file+38 added−59 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+38 added−59 removed

  • other:+38 added−59 removed

1 file+38 added−59 removed

modified:examples/pager/main.go+38 added-59 removed
3 unmodified lines
4// component library.56import (7 "fmt"8 "io/ioutil"8 "os"9 "strings"1011 "github.com/charmbracelet/bubbles/viewport"12 tea "github.com/charmbracelet/bubbletea"13 "github.com/charmbracelet/lipgloss"14 "github.com/mattn/go-runewidth"15)1617const (18 // You generally won't need this unless you're processing stuff with some19 // pretty complicated ANSI escape sequences. Turn it on if you notice20 // flickering.21 //22 // Also note that high performance rendering only works for programs that23 // use the full size of the terminal. We're enabling that below with24 // tea.EnterAltScreen().25 useHighPerformanceRenderer = false17// You generally won't need this unless you're processing stuff with18// complicated ANSI escape sequences. Turn it on if you notice flickering.19//20// Also keep in mind that high performance rendering only works for programs21// that use the full size of the terminal. We're enabling that below with22// tea.EnterAltScreen().23const useHighPerformanceRenderer = false2427 headerHeight = 328 footerHeight = 329)3025func main() {3233 // Load some text to render34 content, err := ioutil.ReadFile("artichoke.md")26 // Load some text for our viewport27 content, err := os.ReadFile("artichoke.md")28 if err != nil {29 fmt.Println("could not load file:", err)30 os.Exit(1)31 }3240 // Set PAGER_LOG to a path to log to a file. For example:41 //42 // export PAGER_LOG=debug.log43 //44 // This becomes handy when debugging stuff since you can't debug to stdout45 // because the UI is occupying it!46 path := os.Getenv("PAGER_LOG")47 if path != "" {48 f, err := tea.LogToFile(path, "pager")49 if err != nil {50 fmt.Printf("Could not open file %s: %v", path, err)51 os.Exit(1)52 }53 defer f.Close()54 }5533 p := tea.NewProgram(34 model{content: string(content)},5859 // Use the full size of the terminal in its "alternate screen buffer"60 tea.WithAltScreen(),6162 // Also turn on mouse support so we can track the mouse wheel63 tea.WithMouseCellMotion(),35 tea.WithAltScreen(), // use the full size of the terminal in its "alternate screen buffer"36 tea.WithMouseCellMotion(), // turn on mouse support so we can track the mouse wheel37 )3839 if err := p.Start(); err != nil {40 fmt.Println("could not run program:", err)
23 unmodified lines
64 return m, tea.Quit65 }6667 case tea.WindowSizeMsg:68 headerHeight := lipgloss.Height(m.headerView())69 footerHeight := lipgloss.Height(m.footerView())95 verticalMargins := headerHeight + footerHeight70 verticalMarginHeight := headerHeight + footerHeight7172 if !m.ready {98 // Since this program is using the full size of the viewport we need99 // to wait until we've received the window dimensions before we100 // can initialize the viewport. The initial dimensions come in73 // Since this program is using the full size of the viewport we74 // need to wait until we've received the window dimensions before75 // we can initialize the viewport. The initial dimensions come in76 // quickly, though asynchronously, which is why we wait for them77 // here.103 m.viewport = viewport.Model{Width: msg.Width, Height: msg.Height - verticalMargins}78 m.viewport = viewport.NewModel(msg.Width, msg.Height-verticalMarginHeight)79 m.viewport.YPosition = headerHeight80 m.viewport.HighPerformanceRendering = useHighPerformanceRenderer81 m.viewport.SetContent(m.content)82 m.ready = true83
3 unmodified lines
87 // Render the viewport one line below the header.88 m.viewport.YPosition = headerHeight + 189 } else {90 m.viewport.Width = msg.Width115 m.viewport.Height = msg.Height - verticalMargins91 m.viewport.Height = msg.Height - verticalMarginHeight92 }9394 if useHighPerformanceRenderer {95 // Render (or re-render) the whole viewport. Necessary both to
3 unmodified lines
99 cmds = append(cmds, viewport.Sync(m.viewport))100 }101 }102127 // Because we're using the viewport's default update function (with pager-128 // style navigation) it's important that the viewport's update function:129 //130 // * Receives messages from the Bubble Tea runtime131 // * Returns commands to the Bubble Tea runtime132 //103 // Handle keyboard and mouse events in the viewport104 m.viewport, cmd = m.viewport.Update(msg)134 if useHighPerformanceRenderer {135 cmds = append(cmds, cmd)105 cmds = append(cmds, cmd)136 }106107 return m, tea.Batch(cmds...)108}109110func (m model) View() string {111 if !m.ready {112 return "\n Initializing..."113 }114 return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.viewport.View(), m.footerView())115}116117func (m model) headerView() string {118 headerTop := "╭───────────╮"119 headerMid := "│ Mr. Pager ├"120 headerBot := "╰───────────╯"149 headerMid += strings.Repeat("─", m.viewport.Width-runewidth.StringWidth(headerMid))150 header := fmt.Sprintf("%s\n%s\n%s", headerTop, headerMid, headerBot)121 headerMid += strings.Repeat("─", max(0, m.viewport.Width-runewidth.StringWidth(headerMid)))122 return fmt.Sprintf("%s\n%s\n%s", headerTop, headerMid, headerBot)123}124125func (m model) footerView() string {126 footerTop := "╭──────╮"127 footerMid := fmt.Sprintf("┤ %3.f%% │", m.viewport.ScrollPercent()*100)128 footerBot := "╰──────╯"155 gapSize := m.viewport.Width - runewidth.StringWidth(footerMid)129 gapSize := max(0, m.viewport.Width-runewidth.StringWidth(footerMid))130 footerTop = strings.Repeat(" ", gapSize) + footerTop131 footerMid = strings.Repeat("─", gapSize) + footerMid132 footerBot = strings.Repeat(" ", gapSize) + footerBot159 footer := fmt.Sprintf("%s\n%s\n%s", footerTop, footerMid, footerBot)133 return fmt.Sprintf("%s\n%s\n%s", footerTop, footerMid, footerBot)134}135161 return fmt.Sprintf("%s\n%s\n%s", header, m.viewport.View(), footer)136func max(a, b int) int {137 if a > b {138 return a139 }140 return b141}