feat: add a cmd to request window size (#988)

* feat: add a cmd to request window size

Fixes: https://github.com/charmbracelet/bubbletea/issues/987#issuecomment-2068452658

* Update commands.go

Co-authored-by: Christian Rocha <christian@rocha.is>

---------

Co-authored-by: Christian Rocha <christian@rocha.is>
This commit is contained in:
Ayman Bagabas 2024-07-16 12:56:15 -04:00 committed by GitHub
parent 4497aa9eef
commit 7d708384a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 0 deletions

View file

@ -201,3 +201,16 @@ func SetWindowTitle(title string) Cmd {
return setWindowTitleMsg(title)
}
}
type windowSizeMsg struct{}
// WindowSize is a command that queries the terminal for its current size. It
// delivers the results to Update via a [WindowSizeMsg]. Keep in mind that
// WindowSizeMsgs will automatically be delivered to Update when the [Program]
// starts and when the window dimensions change so in many cases you will not
// need to explicitly invoke this command.
func WindowSize() Cmd {
return func() Msg {
return windowSizeMsg{}
}
}

3
tea.go
View file

@ -422,6 +422,9 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
case setWindowTitleMsg:
p.SetWindowTitle(string(msg))
case windowSizeMsg:
p.checkResize()
}
// Process internal messages for the renderer.