Skip to content

Releases: charmbracelet/bubbletea

v2.0.0-alpha.1

18 Sep 18:39
v2.0.0-alpha.1
3274e41
Compare
Choose a tag to compare
v2.0.0-alpha.1 Pre-release
Pre-release

Who’s ready for Bubble Tea v2 Alpha?

We’re so excited for you to try Bubble Tea v2! Keep in mind that this is an alpha release and things may change.

Note

We don't take API changes lightly and strive to make the upgrade process as simple as possible. We believe the changes bring necessary improvements as well as pave the way for the future. If something feels way off, let us know.

Here are the the big things to look out for in v2:

Key handling is way better now

We added support for way, way, way better key handling in newer terminals. For example, now you can map to things like shift+enter and super+space, as long as the terminal supports progressive keyboard enhancement. You can also detect key releases too (we're looking at you, terminal game developers).

Init looks more like Update

We changed Init()’s signature to match Update() to make programs easier to follow and to make swapping models easier.

Upgrading

Upgrading to Bubble Tea v2 is easy. Just update your imports and follow the
instructions below.

go get github.com/charmbracelet/bubbletea/v2@v2.0.0-alpha.1

# If you're using Bubbles you'll also want to update that.
# Note that Huh isn't supported in v2 yet.
go get github.com/charmbracelet/bubbles/v2@v2.0.0-alpha.1

Init() signature

Change your Model's Init() signature to return a tea.Model and a tea.Cmd:

// Before:
func (m Model) Init() Cmd
    // do your thing
    return cmd
}

// After:
func (m Model) Init() (Model, Cmd)
    // oooh, I can return a new model now
    return m, cmd
Why the change?

Now you can use Init to initialize the Model with some state. By following this pattern Bubble Tea programs become easier to follow. Of course, you can still initialize the model before passing it to Program if you want, too.

It also becomes more natural to switch models in Update, which is a very useful
way to manage state. Consider the following:

func (m ListModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg.(type) {
    case SwitchToEditorMsg:
        // The user wants to edit an item. Switch to the editor model.
        return EditorModel{}.Init()
    }

    // ...
}

While the change to Init() may seem big, in practice we've found that it doesn't take much to update existing programs.

Keyboard enhancements (optional)

With Bubble Tea v2, you can get more out of your terminal. Progressive keyboard enhancements, allow you to use more key combinations. Just use the tea.WithKeyboardEnhancements option when creating a new program to get all the keys, in supported terminals only.

You can enable enhanced keyboard support by passing the tea.WithKeyboardEnhancements option to tea.NewProgram or by using the tea.EnableKeyboardEnhancements command.

p := tea.NewProgram(model, tea.WithKeyboardEnhancements())

// Or in your `Init` function:
func (m Model) Init() (tea.Model, tea.Cmd) {
    return m, tea.EnableKeyboardEnhancements()
}

By default, release events aren't included, but you can opt-into them with the tea.WithKeyReleases flag:

tea.WithKeyboardEnhancements(tea.WithKeyReleases)   // ProgramOption
tea.EnableKeyboardEnhancements(tea.WithKeyReleases) // Cmd

You can detect if a terminal supports keyboard enhancements by listening for tea.KeyboardEnhancementsMsg after enabling progressive enhancements.

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
    switch msg := msg.(type) {
    case tea.KeyboardEnhancementsMsg:
        // More keys, please!
    }
}

Note

This feature is enabled by default on Windows due to the fact that we use the Windows Console API to support other Windows features.

Which terminals support progressive enhancement?

Key messages

Key messages are now split into tea.KeyPressMsg and tea.KeyReleaseMsg. Use
tea.KeyMsg to match against both.

// Before:
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {
    case tea.KeyMsg:
        // I'm a key press message
    }
}

// After:
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {
    case tea.KeyMsg:
        // I'm a key press or release message
        switch key := msg.(type) {
        case tea.KeyPressMsg:
            // I'm a key press message
            return m, tea.Printf("You pressed %s", key)
        case tea.KeyReleaseMsg:
            // I'm a key release message ;)
            return m, tea.Printf("Key released: %s", key)
        }
    }
}

We no longer have key.Type and key.Runes fields. These have been replaced
with key.Code and key.Text respectively. A key code is just a rune that
represents the key message. It can be a special key like tea.KeyEnter,
tea.KeyTab, tea.KeyEscape, or a printable rune.

// Before:
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {
    case tea.KeyMsg:
        switch msg.Type {
        case tea.KeyEnter:
            // Enter key
        case tea.KeyRune:
            // A printable rune
            switch msg.Runes[0] {
            case 'a':
                // The letter 'a'
            }
        }
    }
}

// After:
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {
    case tea.KeyMsg:
        switch msg.Code {
        case tea.KeyEnter:
            // Enter key
        case 'a':
            // The letter 'a'
        }
    }
}

The new key.Text field signifies a printable key event. If the key event has
a non-empty Text field, it means the key event is a printable key event. In
that case, key.Code is always going to be the first rune of key.Text.

// Before:
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {
    case tea.KeyMsg:
        if msg.Type == tea.KeyRune {
            // A printable rune
            switch string(msg.Runes) {
            case "😃":
                // Smiley face
            }
        }
    }
}

// After:
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {
    case tea.KeyMsg:
        // A printable rune
        switch msg.Text {
        case "😃":
            // Smiley face
        }
    }
}

Instead of matching against msg.Type == tea.KeyCtrl... keys, key modifiers
are now part of the key event itself as key.Mod. Shifted keys now have their
own key code in key.ShiftedCode. Typing shift+b will produce
key.Code == 'b', key.ShiftedCode == 'B', key.Text == "B", and key.Mod == tea.ModShift.

// Before:
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {
    case tea.KeyMsg:
        switch msg.Type {
        case tea.KeyCtrlC:
            // ctrl+c
        case tea.KeyCtrlA:
            // ctrl+a
        default: // 🙄
            // can i catch all ctrl+<key> combinations?
            if msg.Alt {
                // but i want to catch ctrl+alt+<key> combinations too 🤔
                return m, tea.Printf("idk what to do with '%s'", msg)
            }
            switch msg.Runes[0] {
            case 'B': // shift+a
                // ugh, i forgot caps lock was on
                return m, tea.Printf("You typed '%s'", msg.Runes)
            }
        }
    }
}

// After:
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {
    case tea.KeyMsg:
        switch msg.Mod {
        case tea.ModCtrl: // We're only interested in ctrl+<key>
            switch msg.Code {
            case 'c':
                // ctrl+c
            case 'a':
                // ctrl+a
            default:
                return m, tea.Printf("That's an interesting key combo! %s", msg)
            }
        default:
            if msg.Mod.Contains(tea.ModCtrl|tea.ModAlt) {
                return m, tea.Printf("I'm a '%s' 😎!", msg)
            }
            if len(msg.Text) > 0 {
                switch msg.String() {
                case "shift+b":
                    // It doesn't matter if caps lock is on or off, we got your back!
                    return m, tea.Printf("You typed '%s'", msg.Text) // "B"
                }
            }
        }
    }
}

The easiest way to match against key events is to use msg.String():

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {
    case tea.KeyMsg:
        switch msg.String() {
        case "space":
            // Space bar returns "space" now :D
            return m, tea.Println("You pressed the space bar!")
        case "ctrl+c":
            // Copy to the clipboard.
            return m, tea.SetClipboard("Howdy")
        case "ctrl+v":
            // Read the clipboard (not supported by all terminals).
            return m, tea.ReadClipboard()
        case "alt+enter":
            // Fullscreen mode, anyone?
        case "shift+x":
            // Just an upper case 'x'
            return m, tea.Println("You typed %q", msg.Text) // "X"
        case "shift+enter":
            // Awesome, right?
        case "ctrl+alt+super+enter":
            // Yes, ...
Read more

v1.1.1

11 Sep 17:06
v1.1.1
Compare
Choose a tag to compare

Don't panic!

Panicking is a part of life…and a part of workin’ in Go. This release addresses two edge cases where a panic() could tank Bubble Tea and break your terminal:

Panics outside of Bubble Tea

If a panic occurs outside of Bubble Tea you can use Program.Kill to restore the terminal state before exiting:

func main() {
	p := tea.NewProgram(model{})

	go func() {
		time.Sleep(3 * time.Second)
		defer p.Kill()
		panic("Urgh")
	}()

	if _, err := p.Run(); err != nil {
		log.Fatal(err)
	}
}

Panics in Cmds

If a panic occurs in a Cmd Bubble Tea will now automatically restore the terminal to its natural state before exiting.

type model struct{}

// This command will totally panic.
func pancikyCmd() tea.Msg {
	panic("Oh no! Jk.")
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := msg.(type) {
	case tea.KeyMsg:
		switch msg.String() {
		case "enter":
			// Panic time! But everything will be OK.
			return m, pancikyCmd
		}
	}
	return m, nil
}

Happy panicking (if that makes any sense).

Changelog

Fixed!


The Charm logo

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

v1.1.0

30 Aug 13:23
v1.1.0
e58efab
Compare
Choose a tag to compare

Let’s Focus

Lose focus much? This release contains support for focus-blur window events.

Usage

All you need to do is to add the program option to your application:

p := tea.NewProgram(model{}, tea.WithReportFocus())
if _, err := p.Run(); err != nil {
	fmt.Fprintln(os.Stderr, "Oof:", err)
	os.Exit(1)
}

Then later in your Update function, you can listen for focus-blur messages:

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := msg.(type) {
	case tea.FocusMsg:
		// Focused!
	case tea.BlurMsg:
		// Not focused :(
        }
        return m, nil
}

For details, see WithReportFocus.

Tmux

If you're using tmux, make sure you enable the focus-events option in your config.

set-option -g focus-events on

Happy focusing (whatever that means)!


The Charm logo

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

v1.0.1

29 Aug 17:48
v1.0.1
c69bd97
Compare
Choose a tag to compare

This release that fixes the way carriage returns are handled with using the WithoutRenderer ProgramOption and improves the way it works overall by not altering the terminal the way we normally do when starting a Program. For details see #1120.


The Charm logo

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

v1.0.0

28 Aug 15:06
v1.0.0
105d88a
Compare
Choose a tag to compare

At last: v1.0.0

This is an honorary release denoting that Bubble Tea is now stable. Thank you, open source community, for all your love, support, and great taste in beverage over the past four years.

Stay tuned for v2: we have some great things coming.


The Charm logo

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

v0.27.1

22 Aug 17:19
v0.27.1
17443d8
Compare
Choose a tag to compare

This is a lil’ workaround for a hang that can occur when starting a program using Lip Gloss. For details see #1107.

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.

v0.27.0

16 Aug 16:21
v0.27.0
d6a19f0
Compare
Choose a tag to compare

Suspending, environment hacking, and more

Hi! This release has three nice little features and some bug fixes. Let's take a look:

Suspending and resuming

At last, now you can programmatically suspend and resume programs with the tea.Suspend command and handle resumes with the tea.ResumeMsg message:

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := msg.(type) {

	// Suspend with ctrl+z!
	case tea.KeyMsg:
		switch msg.String() {
		case "ctrl+z":
			m.suspended = true
			return m, tea.Suspend
		}

	// Handle resumes
	case tea.ResumeMsg:
		m.suspended = false
		return m, nil
	}

	// ...
}

Example

There's also a tea.SuspendMsg that flows through Update on suspension.

Special thanks to @knz for prototyping the original implementation of this.

Setting the environment

When Bubble Tea is behind Wish you may have needed to pass environment variables from the remote session to the Program. Now you can with the all new tea.WithEnvironment:

var sess ssh.Session // ssh.Session is a type from the github.com/charmbracelet/ssh package
pty, _, _ := sess.Pty()
environ := append(sess.Environ(), "TERM="+pty.Term)
p := tea.NewProgram(model, tea.WithEnvironment(environ)

Requesting the window dimensions

All the Bubble Tea pros know that you get a tea.WindowSizeMsg when the Program starts and when the window resizes. Now you can just query it on demand too with the tea.WindowSize command.

Changelog

New!

Fixed


The Charm logo

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

v0.26.6

24 Jun 18:12
v0.26.6
60a57ea
Compare
Choose a tag to compare

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.

v0.26.5

21 Jun 01:02
v0.26.5
42a7dd8
Compare
Choose a tag to compare

Fix special keys input handling on Windows using the latest Windows Console Input driver.

Changelog

New Features

Bug fixes

Other work


The Charm logo

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

v0.26.4

30 May 18:31
eb78a8c
Compare
Choose a tag to compare

Fix panics! Using program.SetWindowTitle and others may panic if they were called before the program starts.

Also note that program.SetWindowTitle is now deprecated. To set the window title use tea.SetWindowTitle command.

What's Changed

  • chore(deps): bump github.com/charmbracelet/x/ansi from 0.1.1 to 0.1.2 by @dependabot in #1026
  • chore(deps): bump github.com/charmbracelet/lipgloss from 0.10.0 to 0.11.0 in /examples by @dependabot in #1025
  • fix: program renderer commands by @aymanbagabas in #1030

Full Changelog: v0.26.3...v0.26.4


The Charm logo

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