From 116ab34eb8a73374b3d90c6e19a1ea14dc896000 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Thu, 18 Jun 2020 12:33:58 -0400 Subject: [PATCH] Sync pager viewport when starting up and resizing --- examples/pager/main.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/examples/pager/main.go b/examples/pager/main.go index 6569f14..8bcb804 100644 --- a/examples/pager/main.go +++ b/examples/pager/main.go @@ -81,6 +81,11 @@ func initialize(content string) func() (tea.Model, tea.Cmd) { func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { m, _ := mdl.(model) + var ( + cmd tea.Cmd + cmds []tea.Cmd + ) + switch msg := msg.(type) { case tea.KeyMsg: if msg.Type == tea.KeyCtrlC { @@ -107,6 +112,9 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { m.viewport.Height = h - viewportBottomMargin } + // Render (or re-render) the whole viewport + cmds = append(cmds, viewport.Sync(m.viewport)) + case resizeMsg: return m, tea.Batch(getTerminalSize(), listenForResize()) } @@ -116,10 +124,10 @@ 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 - var cmd tea.Cmd m.viewport, cmd = viewport.Update(msg, m.viewport) + cmds = append(cmds, cmd) - return m, cmd + return m, tea.Batch(cmds...) } func view(mdl tea.Model) string { @@ -128,6 +136,7 @@ func view(mdl tea.Model) string { return "\nError:" + m.err.Error() } else if m.ready { + return viewport.View(m.viewport) return fmt.Sprintf( "── Mr. Pager ──\n\n%s\n\n── %3.f%% ──", viewport.View(m.viewport),