Another one! Bubble Tea Beta 3!
This is a fairly small update that makes key enhancements support even better. Let's get into it.
go get github.com/charmbracelet/bubbletea/v2@v2.0.0-beta.3
Keys
Better Keys by Default
Key disambiguation is now enabled by default, when supported. This means that ctrl+i, which would normally send a tab will now send an actual ctrl+i in supporting terminals. To check for support listen for tea.KeyboardEnhancementsMsg
in your update:
switch msg := msg.(type) {
case tea.KeyboardEnhancementsMsg:
if msg.SupportsKeyDisambiguation() {
// Yay!
}
}
Note that on Windows key disambiguation is always supported.
Does the terminal support key disambigation?
While we suggest designing your application in a way that gracefully upgrades when key disambiguation is available, you could detect for lack of support with a simple timeout:
// queryTimeoutMsg is a message that will get sent after a certain timeout.
type queryTimeoutMsg struct{}
// queryTimeout is a Bubble Tea command that waits for a timeout before sending a [queryTimeoutMsg].
func queryTimeout(d time.Duration) tea.Msg {
time.Sleep(200*time.Millisecond) // 200 ms is purly arbitrary
return queryTimeoutMsg{}
}
func (m model) Init() tea.Cmd {
return tea.Batch(
tea.RequestKeyboardEnhancements(),
queryTimout,
)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyboardEnhancementsMsg:
// We have more keys, heh
m.hasEnhancements = true
case queryTimeoutMsg:
if !m.hasEnhancements {
// Terminal doesn't support keyboard enhancements :'(
m.quit = true
}
}
}
Changelog
Bug fixes
- 0ed7727: fix(examples): we don't need to wait for background color query (#1407) (@aymanbagabas)
- 825e109: fix: always enable basic keyboard enhancements to disambiguate keys (@aymanbagabas)
- 07fa6f6: fix: don't send keyboard enhancements message on startup (@aymanbagabas)
- cb090c0: fix: lint: use isWindows() helper function (@aymanbagabas)
- 7aa6fec: fix: only request default key enhancements on non-windows platforms (@aymanbagabas)
Thoughts? Questions? We love hearing from you. Feel free to reach out on Twitter, The Fediverse, or on Discord.