-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: tea contexts and query terminal capabilities
- Loading branch information
1 parent
656565f
commit a434f25
Showing
11 changed files
with
231 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package tea | ||
|
||
import ( | ||
"context" | ||
"image/color" | ||
|
||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
// Context represents a Bubble Tea program's context. It is passed to the | ||
// program's Init, Update, and View functions to provide information about the | ||
// program's state and to allow them to interact with the terminal. | ||
type Context interface { | ||
context.Context | ||
|
||
// BackgroundColor returns the current background color of the terminal. | ||
// It returns nil if the terminal's doesn't support querying the background | ||
// color. | ||
BackgroundColor() color.Color | ||
|
||
// SupportsEnhancedKeyboard reports whether the terminal supports enhanced | ||
// keyboard keys. On Windows, this means it supports virtual keys like and | ||
// the Windows Console API. On Unix, this means it supports the Kitty | ||
// Keyboard Protocol. | ||
SupportsEnhancedKeyboard() bool | ||
|
||
// NewStyle returns a new Lip Gloss style that is suitable for the program's | ||
// environment. | ||
NewStyle() lipgloss.Style | ||
|
||
// ColorProfile returns the terminal's color profile. | ||
ColorProfile() lipgloss.Profile | ||
|
||
// what else? | ||
} | ||
|
||
type teaContext struct { | ||
context.Context | ||
|
||
profile lipgloss.Profile | ||
kittyFlags int | ||
backgroundColor color.Color | ||
hasLightBg bool // cached value | ||
} | ||
|
||
func newContext(ctx context.Context) *teaContext { | ||
c := new(teaContext) | ||
c.Context = ctx | ||
c.kittyFlags = -1 | ||
return c | ||
} | ||
|
||
func (c *teaContext) BackgroundColor() color.Color { | ||
return c.backgroundColor | ||
} | ||
|
||
func (c *teaContext) SupportsEnhancedKeyboard() bool { | ||
return c.kittyFlags >= 0 | ||
} | ||
|
||
func (c *teaContext) NewStyle() lipgloss.Style { | ||
return lipgloss.NewStyle().ColorProfile(c.profile).HasLightBackground(c.hasLightBg) | ||
} | ||
|
||
func (c *teaContext) ColorProfile() lipgloss.Profile { | ||
return c.profile | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.