{"id":13396953,"url":"https://github.com/charmbracelet/bubbletea","last_synced_at":"2025-05-12T17:42:00.377Z","repository":{"id":37056377,"uuid":"233130089","full_name":"charmbracelet/bubbletea","owner":"charmbracelet","description":"A powerful little TUI framework 🏗","archived":false,"fork":false,"pushed_at":"2025-05-05T15:08:35.000Z","size":5398,"stargazers_count":31293,"open_issues_count":115,"forks_count":900,"subscribers_count":116,"default_branch":"main","last_synced_at":"2025-05-05T15:09:23.862Z","etag":null,"topics":["cli","elm-architecture","framework","functional","go","golang","hacktoberfest","tui"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/charmbracelet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-01-10T21:04:03.000Z","updated_at":"2025-05-05T14:51:23.000Z","dependencies_parsed_at":"2023-07-14T05:46:33.468Z","dependency_job_id":"9ecb0535-f5b3-421f-8e7c-d474ac23c952","html_url":"https://github.com/charmbracelet/bubbletea","commit_stats":{"total_commits":876,"total_committers":127,"mean_commits":6.897637795275591,"dds":0.4817351598173516,"last_synced_commit":"7899f0fdd0d697c0468d4bd9ac5bcb2f8a81c75d"},"previous_names":["charmbracelet/boba","charmbracelet/tea"],"tags_count":87,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charmbracelet%2Fbubbletea","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charmbracelet%2Fbubbletea/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charmbracelet%2Fbubbletea/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charmbracelet%2Fbubbletea/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/charmbracelet","download_url":"https://codeload.github.com/charmbracelet/bubbletea/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253789505,"owners_count":21964686,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","elm-architecture","framework","functional","go","golang","hacktoberfest","tui"],"created_at":"2024-07-30T18:01:08.330Z","updated_at":"2025-05-12T17:41:58.236Z","avatar_url":"https://github.com/charmbracelet.png","language":"Go","readme":"# Bubble Tea\n\n\u003cp\u003e\n    \u003cpicture\u003e\n      \u003csource media=\"(prefers-color-scheme: light)\" srcset=\"https://stuff.charm.sh/bubbletea/bubble-tea-v2-light.png\" width=\"308\"\u003e\n      \u003csource media=\"(prefers-color-scheme: dark)\" srcset=\"https://stuff.charm.sh/bubbletea/bubble-tea-v2-dark.png\" width=\"312\"\u003e\n      \u003cimg src=\"https://stuff.charm.sh/bubbletea/bubble-tea-v2-light.png\" width=\"308\" /\u003e\n    \u003c/picture\u003e\n    \u003cbr\u003e\n    \u003ca href=\"https://github.com/charmbracelet/bubbletea/releases\"\u003e\u003cimg src=\"https://img.shields.io/github/release/charmbracelet/bubbletea.svg\" alt=\"Latest Release\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://pkg.go.dev/github.com/charmbracelet/bubbletea?tab=doc\"\u003e\u003cimg src=\"https://godoc.org/github.com/charmbracelet/bubbletea?status.svg\" alt=\"GoDoc\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://github.com/charmbracelet/bubbletea/actions\"\u003e\u003cimg src=\"https://github.com/charmbracelet/bubbletea/actions/workflows/build.yml/badge.svg\" alt=\"Build Status\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://www.phorm.ai/query?projectId=a0e324b6-b706-4546-b951-6671ea60c13f\"\u003e\u003cimg src=\"https://stuff.charm.sh/misc/phorm-badge.svg\" alt=\"phorm.ai\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nThe fun, functional and stateful way to build terminal apps. A Go framework\nbased on [The Elm Architecture][elm]. Bubble Tea is well-suited for simple and\ncomplex terminal applications, either inline, full-window, or a mix of both.\n\n\u003cp\u003e\n    \u003cimg src=\"https://stuff.charm.sh/bubbletea/bubbletea-example.gif\" width=\"100%\" alt=\"Bubble Tea Example\"\u003e\n\u003c/p\u003e\n\nBubble Tea is in use in production and includes a number of features and\nperformance optimizations we’ve added along the way. Among those is\na framerate-based renderer, mouse support, focus reporting and more.\n\nTo get started, see the tutorial below, the [examples][examples], the\n[docs][docs], the [video tutorials][youtube] and some common [resources](#libraries-we-use-with-bubble-tea).\n\n[youtube]: https://charm.sh/yt\n\n## By the way\n\nBe sure to check out [Bubbles][bubbles], a library of common UI components for Bubble Tea.\n\n\u003cp\u003e\n    \u003ca href=\"https://github.com/charmbracelet/bubbles\"\u003e\u003cimg src=\"https://stuff.charm.sh/bubbles/bubbles-badge.png\" width=\"174\" alt=\"Bubbles Badge\"\u003e\u003c/a\u003e\u0026nbsp;\u0026nbsp;\n    \u003ca href=\"https://github.com/charmbracelet/bubbles\"\u003e\u003cimg src=\"https://stuff.charm.sh/bubbles-examples/textinput.gif\" width=\"400\" alt=\"Text Input Example from Bubbles\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n---\n\n## Tutorial\n\nBubble Tea is based on the functional design paradigms of [The Elm\nArchitecture][elm], which happens to work nicely with Go. It's a delightful way\nto build applications.\n\nThis tutorial assumes you have a working knowledge of Go.\n\nBy the way, the non-annotated source code for this program is available\n[on GitHub][tut-source].\n\n[elm]: https://guide.elm-lang.org/architecture/\n[tut-source]: https://github.com/charmbracelet/bubbletea/tree/main/tutorials/basics\n\n### Enough! Let's get to it.\n\nFor this tutorial, we're making a shopping list.\n\nTo start we'll define our package and import some libraries. Our only external\nimport will be the Bubble Tea library, which we'll call `tea` for short.\n\n```go\npackage main\n\n// These imports will be used later on the tutorial. If you save the file\n// now, Go might complain they are unused, but that's fine.\n// You may also need to run `go mod tidy` to download bubbletea and its\n// dependencies.\nimport (\n    \"fmt\"\n    \"os\"\n\n    tea \"github.com/charmbracelet/bubbletea\"\n)\n```\n\nBubble Tea programs are comprised of a **model** that describes the application\nstate and three simple methods on that model:\n\n- **Init**, a function that returns an initial command for the application to run.\n- **Update**, a function that handles incoming events and updates the model accordingly.\n- **View**, a function that renders the UI based on the data in the model.\n\n### The Model\n\nSo let's start by defining our model which will store our application's state.\nIt can be any type, but a `struct` usually makes the most sense.\n\n```go\ntype model struct {\n    choices  []string           // items on the to-do list\n    cursor   int                // which to-do list item our cursor is pointing at\n    selected map[int]struct{}   // which to-do items are selected\n}\n```\n\n### Initialization\n\nNext, we’ll define our application’s initial state. In this case, we’re defining\na function to return our initial model, however, we could just as easily define\nthe initial model as a variable elsewhere, too.\n\n```go\nfunc initialModel() model {\n\treturn model{\n\t\t// Our to-do list is a grocery list\n\t\tchoices:  []string{\"Buy carrots\", \"Buy celery\", \"Buy kohlrabi\"},\n\n\t\t// A map which indicates which choices are selected. We're using\n\t\t// the  map like a mathematical set. The keys refer to the indexes\n\t\t// of the `choices` slice, above.\n\t\tselected: make(map[int]struct{}),\n\t}\n}\n```\n\nNext, we define the `Init` method. `Init` can return a `Cmd` that could perform\nsome initial I/O. For now, we don't need to do any I/O, so for the command,\nwe'll just return `nil`, which translates to \"no command.\"\n\n```go\nfunc (m model) Init() tea.Cmd {\n    // Just return `nil`, which means \"no I/O right now, please.\"\n    return nil\n}\n```\n\n### The Update Method\n\nNext up is the update method. The update function is called when ”things\nhappen.” Its job is to look at what has happened and return an updated model in\nresponse. It can also return a `Cmd` to make more things happen, but for now\ndon't worry about that part.\n\nIn our case, when a user presses the down arrow, `Update`’s job is to notice\nthat the down arrow was pressed and move the cursor accordingly (or not).\n\nThe “something happened” comes in the form of a `Msg`, which can be any type.\nMessages are the result of some I/O that took place, such as a keypress, timer\ntick, or a response from a server.\n\nWe usually figure out which type of `Msg` we received with a type switch, but\nyou could also use a type assertion.\n\nFor now, we'll just deal with `tea.KeyMsg` messages, which are automatically\nsent to the update function when keys are pressed.\n\n```go\nfunc (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {\n    switch msg := msg.(type) {\n\n    // Is it a key press?\n    case tea.KeyMsg:\n\n        // Cool, what was the actual key pressed?\n        switch msg.String() {\n\n        // These keys should exit the program.\n        case \"ctrl+c\", \"q\":\n            return m, tea.Quit\n\n        // The \"up\" and \"k\" keys move the cursor up\n        case \"up\", \"k\":\n            if m.cursor \u003e 0 {\n                m.cursor--\n            }\n\n        // The \"down\" and \"j\" keys move the cursor down\n        case \"down\", \"j\":\n            if m.cursor \u003c len(m.choices)-1 {\n                m.cursor++\n            }\n\n        // The \"enter\" key and the spacebar (a literal space) toggle\n        // the selected state for the item that the cursor is pointing at.\n        case \"enter\", \" \":\n            _, ok := m.selected[m.cursor]\n            if ok {\n                delete(m.selected, m.cursor)\n            } else {\n                m.selected[m.cursor] = struct{}{}\n            }\n        }\n    }\n\n    // Return the updated model to the Bubble Tea runtime for processing.\n    // Note that we're not returning a command.\n    return m, nil\n}\n```\n\nYou may have noticed that \u003ckbd\u003ectrl+c\u003c/kbd\u003e and \u003ckbd\u003eq\u003c/kbd\u003e above return\na `tea.Quit` command with the model. That’s a special command which instructs\nthe Bubble Tea runtime to quit, exiting the program.\n\n### The View Method\n\nAt last, it’s time to render our UI. Of all the methods, the view is the\nsimplest. We look at the model in its current state and use it to return\na `string`. That string is our UI!\n\nBecause the view describes the entire UI of your application, you don’t have to\nworry about redrawing logic and stuff like that. Bubble Tea takes care of it\nfor you.\n\n```go\nfunc (m model) View() string {\n    // The header\n    s := \"What should we buy at the market?\\n\\n\"\n\n    // Iterate over our choices\n    for i, choice := range m.choices {\n\n        // Is the cursor pointing at this choice?\n        cursor := \" \" // no cursor\n        if m.cursor == i {\n            cursor = \"\u003e\" // cursor!\n        }\n\n        // Is this choice selected?\n        checked := \" \" // not selected\n        if _, ok := m.selected[i]; ok {\n            checked = \"x\" // selected!\n        }\n\n        // Render the row\n        s += fmt.Sprintf(\"%s [%s] %s\\n\", cursor, checked, choice)\n    }\n\n    // The footer\n    s += \"\\nPress q to quit.\\n\"\n\n    // Send the UI for rendering\n    return s\n}\n```\n\n### All Together Now\n\nThe last step is to simply run our program. We pass our initial model to\n`tea.NewProgram` and let it rip:\n\n```go\nfunc main() {\n    p := tea.NewProgram(initialModel())\n    if _, err := p.Run(); err != nil {\n        fmt.Printf(\"Alas, there's been an error: %v\", err)\n        os.Exit(1)\n    }\n}\n```\n\n## What’s Next?\n\nThis tutorial covers the basics of building an interactive terminal UI, but\nin the real world you'll also need to perform I/O. To learn about that have a\nlook at the [Command Tutorial][cmd]. It's pretty simple.\n\nThere are also several [Bubble Tea examples][examples] available and, of course,\nthere are [Go Docs][docs].\n\n[cmd]: https://github.com/charmbracelet/bubbletea/tree/main/tutorials/commands/\n[examples]: https://github.com/charmbracelet/bubbletea/tree/main/examples\n[docs]: https://pkg.go.dev/github.com/charmbracelet/bubbletea?tab=doc\n\n## Debugging\n\n### Debugging with Delve\n\nSince Bubble Tea apps assume control of stdin and stdout, you’ll need to run\ndelve in headless mode and then connect to it:\n\n```bash\n# Start the debugger\n$ dlv debug --headless --api-version=2 --listen=127.0.0.1:43000 .\nAPI server listening at: 127.0.0.1:43000\n\n# Connect to it from another terminal\n$ dlv connect 127.0.0.1:43000\n```\n\nIf you do not explicitly supply the `--listen` flag, the port used will vary\nper run, so passing this in makes the debugger easier to use from a script\nor your IDE of choice.\n\nAdditionally, we pass in `--api-version=2` because delve defaults to version 1\nfor backwards compatibility reasons. However, delve recommends using version 2\nfor all new development and some clients may no longer work with version 1.\nFor more information, see the [Delve documentation](https://github.com/go-delve/delve/tree/master/Documentation/api).\n\n### Logging Stuff\n\nYou can’t really log to stdout with Bubble Tea because your TUI is busy\noccupying that! You can, however, log to a file by including something like\nthe following prior to starting your Bubble Tea program:\n\n```go\nif len(os.Getenv(\"DEBUG\")) \u003e 0 {\n\tf, err := tea.LogToFile(\"debug.log\", \"debug\")\n\tif err != nil {\n\t\tfmt.Println(\"fatal:\", err)\n\t\tos.Exit(1)\n\t}\n\tdefer f.Close()\n}\n```\n\nTo see what’s being logged in real time, run `tail -f debug.log` while you run\nyour program in another window.\n\n## Libraries we use with Bubble Tea\n\n- [Bubbles][bubbles]: Common Bubble Tea components such as text inputs, viewports, spinners and so on\n- [Lip Gloss][lipgloss]: Style, format and layout tools for terminal applications\n- [Harmonica][harmonica]: A spring animation library for smooth, natural motion\n- [BubbleZone][bubblezone]: Easy mouse event tracking for Bubble Tea components\n- [ntcharts][ntcharts]: A terminal charting library built for Bubble Tea and [Lip Gloss][lipgloss]\n\n[bubbles]: https://github.com/charmbracelet/bubbles\n[lipgloss]: https://github.com/charmbracelet/lipgloss\n[harmonica]: https://github.com/charmbracelet/harmonica\n[bubblezone]: https://github.com/lrstanley/bubblezone\n[ntcharts]: https://github.com/NimbleMarkets/ntcharts\n\n## Bubble Tea in the Wild\n\nThere are over [10,000 applications](https://github.com/charmbracelet/bubbletea/network/dependents) built with Bubble Tea! Here are a handful of ’em.\n\n### Staff favourites\n\n- [chezmoi](https://github.com/twpayne/chezmoi): securely manage your dotfiles across multiple machines\n- [circumflex](https://github.com/bensadeh/circumflex): read Hacker News in the terminal\n- [gh-dash](https://www.github.com/dlvhdr/gh-dash): a GitHub CLI extension for PRs and issues\n- [Tetrigo](https://github.com/Broderick-Westrope/tetrigo): Tetris in the terminal\n- [Signls](https://github.com/emprcl/signls): a generative midi sequencer designed for composition and live performance\n- [Superfile](https://github.com/yorukot/superfile): a super file manager\n\n### In Industry\n\n- Microsoft Azure – [Aztify](https://github.com/Azure/aztfy): bring Microsoft Azure resources under Terraform\n- Daytona – [Daytona](https://github.com/daytonaio/daytona): open source dev environment manager\n- Cockroach Labs – [CockroachDB](https://github.com/cockroachdb/cockroach): a cloud-native, high-availability distributed SQL database\n- Truffle Security Co. – [Trufflehog](https://github.com/trufflesecurity/trufflehog): find leaked credentials\n- NVIDIA – [container-canary](https://github.com/NVIDIA/container-canary): a container validator\n- AWS – [eks-node-viewer](https://github.com/awslabs/eks-node-viewer): a tool for visualizing dynamic node usage within an EKS cluster\n- MinIO – [mc](https://github.com/minio/mc): the official [MinIO](https://min.io) client\n- Ubuntu – [Authd](https://github.com/ubuntu/authd): an authentication daemon for cloud-based identity providers\n\n### Charm stuff\n\n- [Glow](https://github.com/charmbracelet/glow): a markdown reader, browser, and online markdown stash\n- [Huh?](https://github.com/charmbracelet/huh): an interactive prompt and form toolkit\n- [Mods](https://github.com/charmbracelet/mods): AI on the CLI, built for pipelines\n- [Wishlist](https://github.com/charmbracelet/wishlist): an SSH directory (and bastion!)\n\n### There’s so much more where that came from\n\nFor more applications built with Bubble Tea see [Charm \u0026 Friends][community].\nIs there something cool you made with Bubble Tea you want to share? [PRs][community] are\nwelcome!\n\n## Contributing\n\nSee [contributing][contribute].\n\n[contribute]: https://github.com/charmbracelet/bubbletea/contribute\n\n## Feedback\n\nWe’d love to hear your thoughts on this project. Feel free to drop us a note!\n\n- [Twitter](https://twitter.com/charmcli)\n- [The Fediverse](https://mastodon.social/@charmcli)\n- [Discord](https://charm.sh/chat)\n\n## Acknowledgments\n\nBubble Tea is based on the paradigms of [The Elm Architecture][elm] by Evan\nCzaplicki et alia and the excellent [go-tea][gotea] by TJ Holowaychuk. It’s\ninspired by the many great [_Zeichenorientierte Benutzerschnittstellen_][zb]\nof days past.\n\n[elm]: https://guide.elm-lang.org/architecture/\n[gotea]: https://github.com/tj/go-tea\n[zb]: https://de.wikipedia.org/wiki/Zeichenorientierte_Benutzerschnittstelle\n[community]: https://github.com/charm-and-friends/charm-in-the-wild\n\n## License\n\n[MIT](https://github.com/charmbracelet/bubbletea/raw/main/LICENSE)\n\n---\n\nPart of [Charm](https://charm.sh).\n\n\u003ca href=\"https://charm.sh/\"\u003e\u003cimg alt=\"The Charm logo\" src=\"https://stuff.charm.sh/charm-badge.jpg\" width=\"400\"\u003e\u003c/a\u003e\n\nCharm热爱开源 • Charm loves open source • نحنُ نحب المصادر المفتوحة\n","funding_links":[],"categories":["Go","Popular","Command Line","开源类库","HarmonyOS","命令行","Programming","CLI frameworks","Open source library","Uncategorized","Build Automation","前端开发框架及项目","Tools","Programming Languages","语言资源库","cli","Go 🐹","Repositories","\u003ca name=\"Go\"\u003e\u003c/a\u003eGo","Project","TUI Frameworks \u0026 Libraries","ASCII UI/UX Frameworks \u0026 Foundations","Table of Contents"],"sub_categories":["Advanced Console UIs","命令行","Windows Manager","高级控制台用户界面","Golang","Command Line","Uncategorized","其他_文本生成、文本对话","Go","go","Miscellaneous","Imported: Upstream Snippet Gallery"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharmbracelet%2Fbubbletea","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcharmbracelet%2Fbubbletea","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharmbracelet%2Fbubbletea/lists"}