Skip to content

Commit

Permalink
feat: store last window-size-msg in context
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Jun 5, 2024
1 parent d382b3a commit bca513b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
14 changes: 14 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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?
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
6 changes: 6 additions & 0 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit bca513b

Please sign in to comment.