Configs2026年4月12日·1 分钟阅读

Bubble Tea — Powerful TUI Framework for Go

Bubble Tea is a powerful little TUI framework for Go based on The Elm Architecture. Build terminal applications with composable components, events, and commands. Part of the Charm ecosystem (Lip Gloss, Gum, Wish, Huh). Powers lazygit, soft-serve, and many CLI tools.

AI
AI Open Source · Community
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

go get github.com/charmbracelet/bubbletea
package main

import (
    "fmt"
    tea "github.com/charmbracelet/bubbletea"
)

type model struct {
    choices  []string
    cursor   int
    selected map[int]struct{}
}

func initialModel() model {
    return model{
        choices:  []string{"React", "Vue", "Svelte", "Solid", "Qwik"},
        selected: make(map[int]struct{}),
    }
}

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:
        switch msg.String() {
        case "q", "ctrl+c":
            return m, tea.Quit
        case "up", "k":
            if m.cursor > 0 { m.cursor-- }
        case "down", "j":
            if m.cursor < len(m.choices)-1 { m.cursor++ }
        case " ":
            if _, ok := m.selected[m.cursor]; ok {
                delete(m.selected, m.cursor)
            } else {
                m.selected[m.cursor] = struct{}{}
            }
        }
    }
    return m, nil
}

func (m model) View() string {
    s := "Pick your frameworks:\n\n"
    for i, choice := range m.choices {
        cursor := " "
        if m.cursor == i { cursor = ">" }
        checked := " "
        if _, ok := m.selected[i]; ok { checked = "x" }
        s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)
    }
    return s + "\nq to quit\n"
}

func main() {
    tea.NewProgram(initialModel()).Run()
}
介绍

Bubble Tea is a Go framework for building terminal user interfaces based on The Elm Architecture (Model-Update-View). Part of the Charm ecosystem that also includes Lip Gloss (styling), Bubbles (components), Gum (CLI scripting), and Huh (forms). Used in lazygit, soft-serve, glow, and many Go CLI tools.

What Bubble Tea Does

  • Elm Architecture — Model, Update, View pattern
  • Components — via Bubbles library (text input, spinner, table, list, paginator)
  • Styling — via Lip Gloss (colors, borders, alignment)
  • Mouse support — click and scroll events
  • Async commands — Cmd pattern for side effects
  • Sub-programs — compose multiple TUI programs
  • Renderer — efficient diff-based terminal rendering
  • Cross-platform — Windows, macOS, Linux

Comparison

TUI Framework Language Architecture
Bubble Tea Go Elm (MVU)
Ratatui Rust Immediate mode
Textual Python Widget tree
Ink TypeScript React-like
cursive Rust Callback

常见问题 FAQ

Q: Elm Architecture 是什么? A: 单向数据流:Model(状态)→ View(渲染)→ Update(处理消息)→ 新 Model。简洁且可测试。

Q: 和 Charm 其他工具关系? A: Bubble Tea 是核心框架;Bubbles 是预置组件;Lip Gloss 是样式;Gum 是 shell 脚本中使用 Charm 组件;Huh 是表单库。

来源与致谢 Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产