From 7c1bfc0e55e65bc6d52ec1e126d97ee45ddbeb08 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 18 Jul 2024 09:57:59 -0400 Subject: [PATCH] fix: query window-size in a goroutine (#1059) We need to run checkResize in a goroutine, otherwise, it will block. Related: https://github.com/charmbracelet/bubbletea/pull/988#issuecomment-2231811332 Fixes: 7d708384a105 (feat: add a cmd to request window size (#988)) --- examples/window-size/main.go | 44 ++++++++++++++++++++++++++++++++++++ tea.go | 2 +- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 examples/window-size/main.go diff --git a/examples/window-size/main.go b/examples/window-size/main.go new file mode 100644 index 0000000000..f605659873 --- /dev/null +++ b/examples/window-size/main.go @@ -0,0 +1,44 @@ +package main + +// A simple program that queries and displays the window-size. + +import ( + "log" + + tea "github.com/charmbracelet/bubbletea" +) + +func main() { + p := tea.NewProgram(model{}) + if _, err := p.Run(); err != nil { + log.Fatal(err) + } +} + +type model struct{} + +func (m model) Init() tea.Cmd { + return nil +} + +func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.KeyMsg: + if s := msg.String(); s == "ctrl+c" || s == "q" || s == "esc" { + return m, tea.Quit + } + + return m, tea.WindowSize() + + case tea.WindowSizeMsg: + return m, tea.Printf("%dx%d", msg.Width, msg.Height) + } + + return m, nil +} + +func (m model) View() string { + s := "When you're done press q to quit. Press any other key to query the window-size.\n" + + return s +} diff --git a/tea.go b/tea.go index 2fe2aca4a4..2e4db17cca 100644 --- a/tea.go +++ b/tea.go @@ -424,7 +424,7 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) { p.SetWindowTitle(string(msg)) case windowSizeMsg: - p.checkResize() + go p.checkResize() } // Process internal messages for the renderer.