Skip to content

Commit

Permalink
fix: query window-size in a goroutine (#1059)
Browse files Browse the repository at this point in the history
We need to run checkResize in a goroutine, otherwise, it will block.

Related: #988 (comment)
Fixes: 7d70838 (feat: add a cmd to request window size (#988))
  • Loading branch information
aymanbagabas authored Jul 18, 2024
1 parent 7d70838 commit 7c1bfc0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
44 changes: 44 additions & 0 deletions examples/window-size/main.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 7c1bfc0

Please sign in to comment.