github charmbracelet/bubbletea v2.0.0-beta.3

latest releases: v1.3.8, v1.3.7, v2.0.0-beta.4...
pre-release4 months ago

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


The Charm logo

Thoughts? Questions? We love hearing from you. Feel free to reach out on Twitter, The Fediverse, or on Discord.

Don't miss a new bubbletea release

NewReleases is sending notifications on new releases.