forked from Mirrors/bubbletea
Update pager example to use high performance renderer
This commit is contained in:
parent
00f7f5be93
commit
603eaa27a6
|
@ -8,5 +8,6 @@ require (
|
|||
github.com/charmbracelet/bubbles v0.0.0-20200526000837-87c7cd778f80
|
||||
github.com/charmbracelet/bubbletea v0.8.1
|
||||
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776
|
||||
github.com/mattn/go-runewidth v0.0.9
|
||||
github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c0
|
||||
)
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
github.com/charmbracelet/bubbles v0.0.0-20200526000837-87c7cd778f80 h1:cfaoL1+tHPABTLEAg831PIFG96teW69Wamz9M025r5M=
|
||||
github.com/charmbracelet/bubbles v0.0.0-20200526000837-87c7cd778f80/go.mod h1:/AeLRFlL2Uf4X7U5LjnswTII6u4maPzMm1+vZfeUJKc=
|
||||
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=
|
||||
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=
|
||||
github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4=
|
||||
|
@ -8,7 +6,8 @@ github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tW
|
|||
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/muesli/termenv v0.5.2/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=
|
||||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/muesli/termenv v0.5.3-0.20200617122104-66d435cfe924 h1:oQHK4eixHzrl+DmUKH0x7SJgbkQBiggektj3ChSuE9I=
|
||||
github.com/muesli/termenv v0.5.3-0.20200617122104-66d435cfe924/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=
|
||||
github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c0 h1:IASsj5pQ478TEN9TLiuhP+ugUQEotZ1QdzQvcJUQ1dM=
|
||||
|
@ -18,6 +17,8 @@ github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03/go.mod h1:Z9+Ul5bCbBKnbCv
|
|||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw=
|
||||
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 h1:vEg9joUBmeBcK9iSJftGNf3coIG4HqZElCPehJsfAYM=
|
||||
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
|
|
@ -4,14 +4,18 @@ import (
|
|||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/bubbles/viewport"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/mattn/go-runewidth"
|
||||
)
|
||||
|
||||
const (
|
||||
viewportTopMargin = 2
|
||||
viewportBottomMargin = 2
|
||||
useHighPerformanceRenderer = true
|
||||
|
||||
headerHeight = 3
|
||||
footerHeight = 3
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -24,7 +28,9 @@ func main() {
|
|||
}
|
||||
|
||||
// Set PAGER_LOG to a path to log to a file. For example,
|
||||
// export PAGER_LOG=debug.log
|
||||
//
|
||||
// export PAGER_LOG=debug.log
|
||||
//
|
||||
if os.Getenv("PAGER_LOG") != "" {
|
||||
p := os.Getenv("PAGER_LOG")
|
||||
f, err := tea.LogToFile(p, "pager")
|
||||
|
@ -78,21 +84,23 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
|
|||
}
|
||||
|
||||
case tea.WindowSizeMsg:
|
||||
viewportVerticalMargins := viewportTopMargin + viewportBottomMargin
|
||||
verticalMargins := headerHeight + footerHeight
|
||||
|
||||
if !m.ready {
|
||||
m.viewport = viewport.NewModel(msg.Width, msg.Height-viewportVerticalMargins)
|
||||
m.viewport.YPosition = viewportTopMargin
|
||||
m.viewport.HighPerformanceRendering = true
|
||||
m.viewport = viewport.NewModel(msg.Width, msg.Height-verticalMargins)
|
||||
m.viewport.YPosition = headerHeight
|
||||
m.viewport.HighPerformanceRendering = useHighPerformanceRenderer
|
||||
m.viewport.SetContent(m.content)
|
||||
m.ready = true
|
||||
} else {
|
||||
m.viewport.Width = msg.Width
|
||||
m.viewport.Height = msg.Height - viewportBottomMargin
|
||||
m.viewport.Height = msg.Height - verticalMargins
|
||||
}
|
||||
|
||||
// Render (or re-render) the whole viewport
|
||||
cmds = append(cmds, viewport.Sync(m.viewport))
|
||||
if useHighPerformanceRenderer {
|
||||
cmds = append(cmds, viewport.Sync(m.viewport))
|
||||
}
|
||||
}
|
||||
|
||||
// Because we're using the viewport's default update function (with pager-
|
||||
|
@ -101,24 +109,39 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
|
|||
// * Recieves messages from the Bubble Tea runtime
|
||||
// * Returns commands to the Bubble Tea runtime
|
||||
m.viewport, cmd = viewport.Update(msg, m.viewport)
|
||||
cmds = append(cmds, cmd)
|
||||
if useHighPerformanceRenderer {
|
||||
cmds = append(cmds, cmd)
|
||||
}
|
||||
|
||||
return m, tea.Batch(cmds...)
|
||||
}
|
||||
|
||||
func view(mdl tea.Model) string {
|
||||
m, ok := mdl.(model)
|
||||
if !ok {
|
||||
return "\n Error: could not perform assertion on model in view."
|
||||
}
|
||||
m, _ := mdl.(model)
|
||||
|
||||
if !m.ready {
|
||||
return "\n Initalizing..."
|
||||
}
|
||||
|
||||
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 := "╭──────╮"
|
||||
footerMid := "┤ %3.f%% │"
|
||||
footerBot := "╰──────╯"
|
||||
space := m.viewport.Width - runewidth.StringWidth(footerMid)
|
||||
footerTop = strings.Repeat(" ", space) + footerTop
|
||||
footerMid = strings.Repeat("─", space) + footerMid
|
||||
footerBot = strings.Repeat(" ", space) + footerBot
|
||||
footer := footerTop + "\n" + footerMid + "\n" + footerBot
|
||||
|
||||
return fmt.Sprintf(
|
||||
"── Mr. Pager ──\n\n%s\n\n── %3.f%% ──",
|
||||
"%s\n%s\n%s",
|
||||
header,
|
||||
viewport.View(m.viewport),
|
||||
m.viewport.ScrollPercent()*100,
|
||||
fmt.Sprintf(footer, m.viewport.ScrollPercent()*100),
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue