~/Fast TUIs in Golang with Bubbletea

Jan 14, 2022


The Bubbletea library is widely used for building fast text user interfaces in Golang. It is lightweight and offers a modern functional model inspired by The Elm Architecture.

Install Bubbletea with:

1
go get github.com/charmbracelet/bubbletea

A minimal Bubbletea TUI looks like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main

import (
  "fmt"
  "os"

  tea "github.com/charmbracelet/bubbletea"
)

type model struct {
  text string
}

func (m model) Init() tea.Cmd { return nil }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  switch msg := msg.(type) {
  case tea.KeyMsg:
    if msg.String() == "ctrl+c" {
      return m, tea.Quit
    }
  }
  return m, nil
}
func (m model) View() string {
  return m.text
}

func main() {
  p := tea.NewProgram(model{text: "Hello, TUI World!"})
  if err := p.Start(); err != nil {
    fmt.Println("Error:", err)
    os.Exit(1)
  }
}

Bubbletea documentation covers TUI techniques such as handling user input and live updates. For components, use sibling libraries like bubbles.

Pros of Bubbletea:

To summarize, combine Golangs performance with Bubbletea for reliable and fast TUIs.

Tags: [golang] [tui] [bubbletea]