From 847c8b88a01798c2ed3225305590568374def9b5 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Tue, 26 May 2020 09:53:15 -0400 Subject: [PATCH] Implement resize handling in pager example --- examples/pager/main.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/examples/pager/main.go b/examples/pager/main.go index 4ee7149..3cedfa9 100644 --- a/examples/pager/main.go +++ b/examples/pager/main.go @@ -67,13 +67,8 @@ func initialize(content string) func() (tea.Model, tea.Cmd) { return model{ content: content, // keep content in the model }, tea.Batch( - - // Get terminal size asynchronously getTerminalSize(), - - // We're not doing anything with it in this example, but this - // is now you'd listen for resizes. - tea.OnResize(func() tea.Msg { return resizeMsg{} }), + listenForResize(), ) } } @@ -98,10 +93,13 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { m.viewport = viewport.NewModel(w, h) m.viewport.SetContent(m.content) m.ready = true + } else { + m.viewport.Width = w + m.viewport.Height = h } case resizeMsg: - return m, getTerminalSize() + return m, tea.Batch(getTerminalSize(), listenForResize()) } return m, nil @@ -122,3 +120,9 @@ func getTerminalSize() tea.Cmd { return terminalSizeMsg{width: w, height: h, err: err} }) } + +func listenForResize() tea.Cmd { + return tea.OnResize(func() tea.Msg { + return resizeMsg{} + }) +}