Update pager example to use high performance renderer
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 00f7f5b · Browse files · 3 files·+44 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+44 added−19 removed
- other:+44 added−19 removed
Provenance
- Commit
603eaa2- Tree
037f655- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
00f7f5b
3 files changed+44 added−19 removed
modified:examples/pager/main.go+39 added-16 removed
2 unmodified lines3 import (4 "fmt"5 "io/ioutil"6 "os"7+ "strings"8 9 "github.com/charmbracelet/bubbles/viewport"10 tea "github.com/charmbracelet/bubbletea"11+ "github.com/mattn/go-runewidth"12 )13 14 const (13- viewportTopMargin = 214- viewportBottomMargin = 215+ useHighPerformanceRenderer = true16+17+ headerHeight = 318+ footerHeight = 319 )20 21 func main() {22 4 unmodified lines27 os.Exit(1)28 }29 30 // Set PAGER_LOG to a path to log to a file. For example,31+ //27- // export PAGER_LOG=debug.log32+ // export PAGER_LOG=debug.log33+ //34 if os.Getenv("PAGER_LOG") != "" {35 p := os.Getenv("PAGER_LOG")36 f, err := tea.LogToFile(p, "pager")37 if err != nil {45 unmodified lines83 return m, tea.Quit84 }85 86 case tea.WindowSizeMsg:81- viewportVerticalMargins := viewportTopMargin + viewportBottomMargin87+ verticalMargins := headerHeight + footerHeight88 89 if !m.ready {84- m.viewport = viewport.NewModel(msg.Width, msg.Height-viewportVerticalMargins)85- m.viewport.YPosition = viewportTopMargin86- m.viewport.HighPerformanceRendering = true90+ m.viewport = viewport.NewModel(msg.Width, msg.Height-verticalMargins)91+ m.viewport.YPosition = headerHeight92+ m.viewport.HighPerformanceRendering = useHighPerformanceRenderer93 m.viewport.SetContent(m.content)94 m.ready = true95 } else {96 m.viewport.Width = msg.Width91- m.viewport.Height = msg.Height - viewportBottomMargin97+ m.viewport.Height = msg.Height - verticalMargins98 }99 100 // Render (or re-render) the whole viewport101+ if useHighPerformanceRenderer {95- cmds = append(cmds, viewport.Sync(m.viewport))102+ cmds = append(cmds, viewport.Sync(m.viewport))103+ }104 }105 106 // Because we're using the viewport's default update function (with pager-107 // style navigation) it's important that the viewport's update function:108 //109 // * Recieves messages from the Bubble Tea runtime110 // * Returns commands to the Bubble Tea runtime111 m.viewport, cmd = viewport.Update(msg, m.viewport)112+ if useHighPerformanceRenderer {104- cmds = append(cmds, cmd)113+ cmds = append(cmds, cmd)114+ }115 116 return m, tea.Batch(cmds...)117 }118 119 func view(mdl tea.Model) string {110- m, ok := mdl.(model)111- if !ok {112- return "\n Error: could not perform assertion on model in view."113- }120+ m, _ := mdl.(model)121 122 if !m.ready {123 return "\n Initalizing..."124 }125 126+ headerTop := "╭───────────╮"127+ headerMid := "│ Mr. Pager ├"128+ headerBot := "╰───────────╯"129+ headerMid += strings.Repeat("─", m.viewport.Width-runewidth.StringWidth(headerMid))130+ header := fmt.Sprintf("%s\n%s\n%s", headerTop, headerMid, headerBot)131+132+ footerTop := "╭──────╮"133+ footerMid := "┤ %3.f%% │"134+ footerBot := "╰──────╯"135+ space := m.viewport.Width - runewidth.StringWidth(footerMid)136+ footerTop = strings.Repeat(" ", space) + footerTop137+ footerMid = strings.Repeat("─", space) + footerMid138+ footerBot = strings.Repeat(" ", space) + footerBot139+ footer := footerTop + "\n" + footerMid + "\n" + footerBot140+141 return fmt.Sprintf(120- "── Mr. Pager ──\n\n%s\n\n── %3.f%% ──",142+ "%s\n%s\n%s",143+ header,144 viewport.View(m.viewport),122- m.viewport.ScrollPercent()*100,145+ fmt.Sprintf(footer, m.viewport.ScrollPercent()*100),146 )147 }modified:examples/go.mod+1 added
6 unmodified lines7 require (8 github.com/charmbracelet/bubbles v0.0.0-20200526000837-87c7cd778f809 github.com/charmbracelet/bubbletea v0.8.110 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf177611+ github.com/mattn/go-runewidth v0.0.912 github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c013 )modified:examples/go.sum+4 added-3 removed
1-github.com/charmbracelet/bubbles v0.0.0-20200526000837-87c7cd778f80 h1:cfaoL1+tHPABTLEAg831PIFG96teW69Wamz9M025r5M=2-github.com/charmbracelet/bubbles v0.0.0-20200526000837-87c7cd778f80/go.mod h1:/AeLRFlL2Uf4X7U5LjnswTII6u4maPzMm1+vZfeUJKc=1 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=2 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=3 github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4=4 github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4=5 github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=6 github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=7 github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=8 github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=11-github.com/muesli/termenv v0.5.2/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=9+github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=10+github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=11 github.com/muesli/termenv v0.5.3-0.20200617122104-66d435cfe924 h1:oQHK4eixHzrl+DmUKH0x7SJgbkQBiggektj3ChSuE9I=12 github.com/muesli/termenv v0.5.3-0.20200617122104-66d435cfe924/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=13 github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c0 h1:IASsj5pQ478TEN9TLiuhP+ugUQEotZ1QdzQvcJUQ1dM=14 github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c0/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=1 unmodified line16 github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03/go.mod h1:Z9+Ul5bCbBKnbCvdOWbLqTHhJiYV414CURZJba6L8qA=17 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=18 golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw=19 golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=20+golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 h1:vEg9joUBmeBcK9iSJftGNf3coIG4HqZElCPehJsfAYM=21+golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=22 golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=23 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=24 golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=25 golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=4 unmodified lines