diff --git a/context.go b/context.go index 2f0c01e8e2..9870fd4f41 100644 --- a/context.go +++ b/context.go @@ -41,6 +41,9 @@ type Context interface { // ColorProfile returns the terminal's color profile. ColorProfile() lipgloss.Profile + // WindowSize returns the terminal's current size. + WindowSize() (width, height int) + // what else? } @@ -55,6 +58,10 @@ var ( // ContextKeyKittyFlags is a key for storing the terminal's Kitty flags in a // Context. ContextKeyKittyFlags = &ContextKey{"kitty-flags"} + + // ContextKeyWindowSize is a key for storing the terminal's window size in + // a Context. + ContextKeyWindowSize = &ContextKey{"window-size"} ) type teaContext struct { @@ -122,3 +129,10 @@ func (c *teaContext) NewStyle() (s lipgloss.Style) { func (c *teaContext) ColorProfile() lipgloss.Profile { return c.profile } + +func (c *teaContext) WindowSize() (width, height int) { + if s, ok := c.Value(ContextKeyWindowSize).(WindowSizeMsg); ok { + return s.Width, s.Height + } + return 0, 0 +} diff --git a/tea.go b/tea.go index acb72f265e..022af18ff0 100644 --- a/tea.go +++ b/tea.go @@ -337,6 +337,12 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) { case QuitMsg: return model, nil + case WindowSizeMsg: + p.ctx.SetValue(ContextKeyWindowSize, msg) + + case KittyKeyboardMsg: + p.ctx.SetValue(ContextKeyKittyFlags, int(msg)) + case BackgroundColorMsg: p.handleContextMessages(msg)