Cleanup header/footer rendering in pager example with Lip Gloss
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 7517c1b · Browse files · 1 file·+40 added−33 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+40 added−33 removed
- other:+40 added−33 removed
Provenance
- Commit
ee92bed- Tree
2de2020- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
7517c1b
1 file changed+40 added−33 removed
modified:examples/pager/main.go+40 added-33 removed
3 unmodified lines4 // component library.5 6 import (7 "fmt"8+ "io/ioutil"9 "os"10 "strings"11 12 "github.com/charmbracelet/bubbles/viewport"13 tea "github.com/charmbracelet/bubbletea"14 "github.com/charmbracelet/lipgloss"14- "github.com/mattn/go-runewidth"15 )16 17 // You generally won't need this unless you're processing stuff with18 // complicated ANSI escape sequences. Turn it on if you notice flickering.2 unmodified lines21 // that use the full size of the terminal. We're enabling that below with22 // tea.EnterAltScreen().23 const useHighPerformanceRenderer = false24 25-func main() {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- }25+var (26+ titleStyle = func() lipgloss.Style {27+ b := lipgloss.RoundedBorder()28+ b.Right = "├"29+ return lipgloss.NewStyle().BorderStyle(b).Padding(0, 1)30+ }()31 33- p := tea.NewProgram(34- model{content: string(content)},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- )32+ infoStyle = func() lipgloss.Style {33+ b := lipgloss.RoundedBorder()34+ b.Left = "┤"35+ return titleStyle.Copy().BorderStyle(b)36+ }()37+)38 39- if err := p.Start(); err != nil {40- fmt.Println("could not run program:", err)41- os.Exit(1)42- }43-}44-39 type model struct {40 content string41 ready bool42 viewport viewport.Model25 unmodified lines68 // need to wait until we've received the window dimensions before69 // we can initialize the viewport. The initial dimensions come in70 // quickly, though asynchronously, which is why we wait for them71 // here.78- m.viewport = viewport.NewModel(msg.Width, msg.Height-verticalMarginHeight)72+ m.viewport = viewport.New(msg.Width, msg.Height-verticalMarginHeight)73 m.viewport.YPosition = headerHeight74 m.viewport.HighPerformanceRendering = useHighPerformanceRenderer75 m.viewport.SetContent(m.content)76 m.ready = true31 unmodified lines108 return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.viewport.View(), m.footerView())109 }110 111 func (m model) headerView() string {118- headerTop := "╭───────────╮"119- headerMid := "│ Mr. Pager ├"120- 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)112+ title := titleStyle.Render("Mr. Pager")113+ line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(title)))114+ return lipgloss.JoinHorizontal(lipgloss.Center, title, line)115 }116 117 func (m model) footerView() string {126- footerTop := "╭──────╮"127- footerMid := fmt.Sprintf("┤ %3.f%% │", m.viewport.ScrollPercent()*100)128- footerBot := "╰──────╯"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) + footerBot133- return fmt.Sprintf("%s\n%s\n%s", footerTop, footerMid, footerBot)118+ info := infoStyle.Render(fmt.Sprintf("%3.f%%", m.viewport.ScrollPercent()*100))119+ line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(info)))120+ return lipgloss.JoinHorizontal(lipgloss.Center, line, info)121 }122 123 func max(a, b int) int {124 if a > b {125 return a126 }127 return b128 }129+130+func main() {131+ // Load some text for our viewport132+ content, err := ioutil.ReadFile("artichoke.md")133+ if err != nil {134+ fmt.Println("could not load file:", err)135+ os.Exit(1)136+ }137+138+ p := tea.NewProgram(139+ model{content: string(content)},140+ tea.WithAltScreen(), // use the full size of the terminal in its "alternate screen buffer"141+ tea.WithMouseCellMotion(), // turn on mouse support so we can track the mouse wheel142+ )143+144+ if err := p.Start(); err != nil {145+ fmt.Println("could not run program:", err)146+ os.Exit(1)147+ }148+}