A powerful little TUI framework 🏗

Bubble Tea

Bubble Tea Title Treatment
Latest Release GoDoc Build Status

The fun, functional and stateful way to build terminal apps. A Go framework based on The Elm Architecture. Bubble Tea is well-suited for simple and complex terminal applications, either inline, full-window, or a mix of both.

Bubble Tea Example

Bubble Tea is in use in production and includes a number of features and performance optimizations we’ve added along the way. Among those is a standard framerate-based renderer, a renderer for high-performance scrollable regions which works alongside the main renderer, and mouse support.

To get started, see the tutorial below, the examples, the docs and some common resources.

By the way

Be sure to check out Bubbles, a library of common UI components for Bubble Tea.

Bubbles Badge   Text Input Example from Bubbles


Tutorial

Bubble Tea is based on the functional design paradigms of The Elm Architecture which happens work nicely with Go. It's a delightful way to build applications.

By the way, the non-annotated source code for this program is available on GitHub.

This tutorial assumes you have a working knowledge of Go.

Enough! Let's get to it.

For this tutorial we're making a to-do list.

To start we'll define our package and import some libraries. Our only external import will be the Bubble Tea library, which we'll call tea for short.

package main

import (
    "fmt"
    "os"

    tea "github.com/charmbracelet/bubbletea"
)

Bubble Tea programs are comprised of a model that describes the application state and three simple methods on that model:

  • Init, a function that returns an initial command for the application to run.
  • Update, a function that handles incoming events and updates the model accordingly.
  • View, a function that renders the UI based on the data in the model.

The Model

So let's start by defining our model which will store our application's state. It can be any type, but a struct usually makes the most sense.

type model struct {
    choices  []string           // items on the to-do list
    cursor   int                // which to-do list item our cursor is pointing at
    selected map[int]struct{}   // which to-do items are selected
}

Initialization

Next we'll define our application’s initial state. We’ll store our initial model in a simple variable, and then define the Init method. Init can return a Cmd that could perform some initial I/O. For now, we don't need to do any I/O, so for the command we'll just return nil, which translates to "no command."

var initialModel = model{
    // Our to-do list is just a grocery list
    choices:  []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},

    // A map which indicates which choices are selected. We're using
    // the  map like a mathematical set. The keys refer to the indexes
    // of the `choices` slice, above.
    selected: make(map[int]struct{}),
}

func (m model) Init() tea.Cmd {
    // Just return `nil`, which means "no I/O right now, please."
    return nil
}

The Update Method

Next we'll define the update method. The update function is called when "things happen." Its job is to look at what has happened and return an updated model in response to whatever happened. It can also return a Cmd and make more things happen, but for now don't worry about that part.

In our case, when a user presses the down arrow, update's job is to notice that the down arrow was pressed and move the cursor accordingly (or not).

The "something happened" comes in the form of a Msg, which can be any type. Messages are the result of some I/O that took place, such as a keypress, timer tick, or a response from a server.

We usually figure out which type of Msg we received with a type switch, but you could also use a type assertion.

For now, we'll just deal with tea.KeyMsg messages, which are automatically sent to the update function when keys are pressed.

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

    // Is it a key press?
    case tea.KeyMsg:

        // Cool, what was the actual key pressed?
        switch msg.String() {

        // These keys should exit the program.
        case "ctrl+c", "q":
            return m, tea.Quit

        // The "up" and "k" keys move the cursor up
        case "up", "k":
            if m.cursor > 0 {
                m.cursor--
            }

        // The "down" and "j" keys move the cursor down
        case "down", "j":
            if m.cursor < len(m.choices)-1 {
                m.cursor++
            }

        // The "enter" key and the spacebar (a literal space) toggle
        // the selected state for the item that the cursor is pointing at.
        case "enter", " ":
            _, ok := m.selected[m.cursor]
            if ok {
                delete(m.selected, m.cursor)
            } else {
                m.selected[m.cursor] = struct{}{}
            }
        }
    }

    // Return the updated model to the Bubble Tea runtime for processing.
    // Note that we're not returning a command.
    return m, nil
}

You may have noticed that "ctrl+c" and "q" above return a tea.Quit command with the model. That's a special command which instructs the Bubble Tea runtime to quit, exiting the program.

The View Method

At last, it's time to render our UI. Of all the methods, the view is the simplest. We look at the model in it's current state and use it to return a string. That string is our UI!

Because the view describes the entire UI of your application, you don't have to worry about redraw logic and stuff like that. Bubble Tea takes care of it for you.

func (m model) View() string {
    // The header
    s := "What should we buy at the market?\n\n"

    // Iterate over our choices
    for i, choice := range m.choices {

        // Is the cursor pointing at this choice?
        cursor := " " // no cursor
        if m.cursor == i {
            cursor = ">" // cursor!
        }

        // Is this choice selected?
        checked := " " // not selected
        if _, ok := m.selected[i]; ok {
            checked = "x" // selected!
        }

        // Render the row
        s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)
    }

    // The footer
    s += "\nPress q to quit.\n"

    // Send the UI for rendering
    return s
}

All Together Now

The last step is to simply run our program. We pass our initial model to tea.NewProgram and let it rip:

func main() {
    p := tea.NewProgram(initialModel)
    if err := p.Start(); err != nil {
        fmt.Printf("Alas, there's been an error: %v", err)
        os.Exit(1)
    }
}

What's Next?

This tutorial covers the basics of building an interactive terminal UI, but in the real world you'll also need to perform I/O. To learn about that have a look at the Command Tutorial. It's pretty simple.

There are also several Bubble Tea examples available and, of course, there are Go Docs.

Libraries we use with Bubble Tea

  • Bubbles: Common Bubble Tea components such as text inputs, viewports, spinners and so on
  • Lip Gloss: Style, format and layout tools for terminal applications. Built on Termenv and Reflow below
  • Termenv: Advanced ANSI styling for terminal applications
  • Reflow: Advanced ANSI-aware methods for working with text

Bubble Tea in the Wild

For some Bubble Tea programs in production, see:

  • Glow: a markdown reader, browser and online markdown stash
  • The Charm Tool: the Charm user account manager
  • kboard: a typing game
  • tasktimer: a dead-simple task timer
  • fork-cleaner: cleans up old and inactive forks in your GitHub account
  • STTG: teletext client for SVT, Sweden’s national public television station
  • gitflow-toolkit: a GitFlow submission tool
  • ticker: a terminal stock watcher and stock position tracker
  • tz: an aid for scheduling across multiple time zones
  • httpit: a rapid http(s) benchmark tool
  • gembro: a Gemini browser

Feedback

We'd love to hear your thoughts on this tutorial. Feel free to drop us a note!

Acknowledgments

Bubble Tea is based on the paradigms of The Elm Architecture by Evan Czaplicki et alia and the excellent go-tea by TJ Holowaychuk.

License

MIT


Part of Charm.

The Charm logo

Charm热爱开源! / Charm loves open source!

Owner
Charm
We build tools to make the command line glamorous
Charm
Similar Resources

TUI Flappy Bird. It‘s a lil bit jank tbh

EBIRD TUI Flappy Bird. It's a lil bit jank tbh. Build and Install Build dependen

Dec 22, 2021

Tabouli: a TUI for interacting with firmware/embedded devices that support a CLI via serial interface/virtual COM Port

Tabouli: a TUI for interacting with firmware/embedded devices that support a CLI via serial interface/virtual COM Port

Tabouli Information Tabouli is a TUI for interacting with firmware/embedded devi

Apr 2, 2022

A TUI implementation of the popular word quiz wordle!

gordle A TUI implementation of the popular word quiz Wordle! Building Build the cli command: $ go build ./cmd/cli Empty output on build success Buil

Dec 21, 2022

Podman-tui - A Terminal User Interface to interact with the podman (v3.x)

Podman-tui - A Terminal User Interface to interact with the podman (v3.x)

podman-tui podman-tui is a Terminal User Interface to interact with the podman (

Dec 23, 2022

The Cloud Aviator: TUI client for cloud services (AWS, Vultr, Heroku, Render.com, ...)

The Cloud Aviator: TUI client for cloud services (AWS, Vultr, Heroku, Render.com, ...)

=== T H E C L O U D A V I A T O R === ⠀⠀⠀⠀⠀⠀⠀⠀⢶⣦⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣷⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

Dec 17, 2022

Handy little CLI for interacting with OCI data

oci-tool Handy little CLI for interacting with OCI data Installation go get github.com/csweichel/oci-tool I use Gitpod for developing this tool; you s

May 17, 2022

Go-Suit is a very very wacky version of a bash terminal but in go, however with a little twitst

Go-Suit Go-Suit is a very very wacky version of a bash terminal but in go, however with a little twitst languages - Go-Lang packages Third Party - g

May 19, 2022

Little golang app that allows you to download a youtube video as mp3, and optionally embed ID3 tags -Cover Art, Artist ...-

yt2mp3 Little golang app that allows you to download a youtube video as mp3, and optionally embed ID3 tags -Cover Art, Artist ...- Instructions At the

Dec 25, 2021

Flag is a simple but powerful command line option parsing library for Go support infinite level subcommand

Flag Flag is a simple but powerful commandline flag parsing library for Go. Documentation Documentation can be found at Godoc Supported features bool

Sep 26, 2022
Comments
  • feat(deps): bump github.com/mattn/go-isatty from 0.0.16 to 0.0.17

    feat(deps): bump github.com/mattn/go-isatty from 0.0.16 to 0.0.17

    Bumps github.com/mattn/go-isatty from 0.0.16 to 0.0.17.

    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
  • Can't redirect output of non-interactive application

    Can't redirect output of non-interactive application

    I use bubbletea for the sole purpose of generating and printing a table in my application. So no interactive features there (until now!). I expected, that I could perform a benchmark-measurement with hyperfine, however it seems like execution is blocked in this case, because hyperfine redirects stdout and stderr.

    Sure enough, if I redirect the output of my program to > /dev/null, then my program never exits.

    Is this something I can solve by writing my application in a different way? Is this a bug with bubbletea?

  • could not decode rune

    could not decode rune

    i am using the mouse input and always getting a program exit after a while because of "could not decode rune"

    I would expect it continues instead of failing on a invalid utf 8 rune.

    		r, width := utf8.DecodeRune(b[i:])
    		if r == utf8.RuneError {
    			return nil, errors.New("could not decode rune")
    		}
    
  • bug: loss of input on ReleaseTerminal / Bubbletea shutdown

    bug: loss of input on ReleaseTerminal / Bubbletea shutdown

    It is not currently possible to integrate Bubbletea-based programs in scripts or unit tests that buffer terminal input across multiple runs of the Bubbletea event loop.

    The specifics are explained here: https://dr-knz.net/bubbletea-control-inversion.html

    In summary, the input reader function is "greedy" but drops/forgets input event messages during ReleaseTerminal(). This is where the input is lost.

    The text linked above outlines a solution:

    • [ ] invert control of the input reader function, i.e. this PR: https://github.com/charmbracelet/bubbletea/pull/569
    • [ ] synchronise the reading from the external input with Update calls.
    • [ ] for the benefit of programs (like Bubbline) that need to share control of the terminal, introduce a new API to suspend the Bubbletea event loop.
TUI process monitor written in Go
TUI process monitor written in Go

pst This is TUI process monitor written in Go. Features Monitor process's list, info, tree, open files, Kill process Support OS Mac Linux Requirements

Nov 25, 2022
A CLI / TUI for Microsoft Teams
A CLI / TUI for Microsoft Teams

teams-cli A Command Line Interface (or TUI) to interact with Microsoft Teams Status The CLI only let you log-in and fetches your user and conversation

Dec 22, 2022
a TUI for signal messenger, written in Go
a TUI for signal messenger, written in Go

siggo A terminal ui for signal-cli, written in Go. Features vim-style ux useful for quick messages or use $EDITOR to compose fancy ones emoji support,

Jan 2, 2023
Canard. A command line TUI client for the journalist RSS aggregator.
Canard. A command line TUI client for the journalist RSS aggregator.

Canard Canard. A command line TUI client for the Journalist RSS aggregator. Installation Download a binary from the releases page. Or build it yoursel

Jan 6, 2023
🧭 TUI for command navigation
🧭 TUI for command navigation

devgo a command-line launcher Install latest version curl -o- https://raw.githubusercontent.com/TheWinds/devgo/main/install.sh | bash special version

Apr 19, 2022
A terminal UI (TUI) for HashiCorp Nomad
A terminal UI (TUI) for HashiCorp Nomad

Damon - A terminal Dashboard for HashiCorp Nomad Damon is a terminal user interface (TUI) for Nomad. It provides functionality to observe and interact

Jan 6, 2023
A TUI multitool for day-to-day operations for software applications.

Bench (WIP) A TUI multitool for day-to-day operations for software applications. Lets you do common operations needed during IT work that are common e

Dec 5, 2021
🦜 Navigate github repos in a tui

goh Navigate github repos in a tui Why I am constantly refering to my github repos and repos from others for code snippets that are relevant to what I

Dec 10, 2021
A tui for playing media from a caddy fileserver

kwatch a little tui interface to play media from a caddy fileserver. options: -a: server address -u: server http username -p: server http password -o:

Aug 9, 2022
Light weight Terminal User Interface (TUI) to pick material colors written by Go.
Light weight Terminal User Interface (TUI) to pick material colors written by Go.

mcpick Light weight Terminal User Interface (TUI) to pick material colors. You do NOT need to take your hands off the keyboard to pick colors. Getting

Dec 27, 2022