11# Bubble Tea
22
33<p >
4- <picture>
5- <source media="(prefers-color-scheme: light)" srcset="https://stuff.charm.sh/bubbletea/bubble-tea-v2-light.png" width="308">
6- <source media="(prefers-color-scheme: dark)" srcset="https://stuff.charm.sh/bubbletea/bubble-tea-v2-dark.png" width="312">
7- <img src="https://stuff.charm.sh/bubbletea/bubble-tea-v2-light.png" width="308" />
8- </picture>
9- <br>
4+ <img src="https://github.com/user-attachments/assets/ad408275-8799-488f-9303-441e7f869535" width="350"><br>
105 <a href="https://github.com/charmbracelet/bubbletea/releases"><img src="https://img.shields.io/github/release/charmbracelet/bubbletea.svg" alt="Latest Release"></a>
11- <a href="https://pkg.go.dev/github.com/charmbracelet/bubbletea?tab=doc"><img src="https://godoc.org/github.com/charmbracelet/bubbletea?status.svg" alt="GoDoc"></a>
12- <a href="https://github.com/charmbracelet/bubbletea/actions"><img src="https://github.com/charmbracelet/bubbletea/actions/workflows/build.yml/badge.svg" alt="Build Status"></a>
13- <a href="https://www.phorm.ai/query?projectId=a0e324b6-b706-4546-b951-6671ea60c13f"><img src="https://stuff.charm.sh/misc/phorm-badge.svg" alt="phorm.ai"></a>
6+ <a href="https://pkg.go.dev/charm.land/bubbletea/v2?tab=doc"><img src="https://godoc.org/charm.land/bubbletea/v2?status.svg" alt="GoDoc"></a>
7+ <a href="https://github.com/charmbracelet/bubbletea/actions"><img src="https://github.com/charmbracelet/bubbletea/actions/workflows/build.yml/badge.svg?branch=main" alt="Build Status"></a>
148</p >
159
1610The fun, functional and stateful way to build terminal apps. A Go framework
@@ -23,19 +17,24 @@ complex terminal applications, either inline, full-window, or a mix of both.
2317
2418Bubble Tea is in use in production and includes a number of features and
2519performance optimizations we’ve added along the way. Among those is
26- a framerate-based renderer, mouse support, focus reporting and more.
20+ a high-performance cell-based renderer, built-in color downsampling,
21+ declarative views, high-fidelity keyboard and mouse handling, native clipboard
22+ support, and more.
2723
2824To get started, see the tutorial below, the [ examples] [ examples ] , the
29- [ docs] [ docs ] , the [ video tutorials ] [ youtube ] and some common [ resources] ( #libraries-we-use-with-bubble-tea ) .
25+ [ docs] [ docs ] , and some common [ resources] ( #libraries-we-use-with-bubble-tea ) .
3026
31- [ youtube ] : https://charm.sh/yt
27+ > [ !TIP]
28+ >
29+ > Upgrading from v1? Check out the [ upgrade guide] ( ./UPGRADE_GUIDE_V2.md ) , or
30+ > point your LLM at it and let it go to town.
3231
3332## By the way
3433
3534Be sure to check out [ Bubbles] [ bubbles ] , a library of common UI components for Bubble Tea.
3635
3736<p >
38- <a href="https://github.com/charmbracelet/bubbles"><img src="https://stuff.charm.sh/bubbles/bubbles-badge.png " width="174 " alt="Bubbles Badge"></a>
37+ <a href="https://github.com/charmbracelet/bubbles"><img src="https://github.com/user-attachments/assets/b6dc4638-b67a-4bfa-88d0-a8e8833c3ac9 " width="172 " alt="Bubbles Badge"></a>
3938 <a href="https://github.com/charmbracelet/bubbles"><img src="https://stuff.charm.sh/bubbles-examples/textinput.gif" width="400" alt="Text Input Example from Bubbles"></a>
4039</p >
4140
@@ -73,7 +72,7 @@ import (
7372 " fmt"
7473 " os"
7574
76- tea " github.com/charmbracelet /bubbletea/v2"
75+ tea " charm.land /bubbletea/v2"
7776)
7877```
7978
@@ -99,31 +98,39 @@ type model struct {
9998
10099## Initialization
101100
102- Next, we’ll define our application’s initial state in the ` Init ` method. ` Init `
103- can return a ` Cmd ` that could perform some initial I/O. For now, we don't need
104- to do any I/O, so for the command, we'll just return ` nil ` , which translates to
105- "no command."
101+ Next, we’ll define our application’s initial state. ` Init ` can return a ` Cmd `
102+ that could perform some initial I/O. For now, we don’t need to do any I/O, so
103+ for the command, we’ll just return ` nil ` , which translates to “no command.”
106104
107105``` go
108- func ( m model ) Init () ( tea . Model , tea . Cmd ) {
109- m = {
106+ func initialModel () model {
107+ return model {
110108 // Our to-do list is a grocery list
111109 choices: []string {" Buy carrots" , " Buy celery" , " Buy kohlrabi" },
112110
113111 // A map which indicates which choices are selected. We're using
114- // the map like a mathematical set. The keys refer to the indexes
112+ // the map like a mathematical set. The keys refer to the indexes
115113 // of the `choices` slice, above.
116114 selected: make (map [int ]struct {}),
117115 }
116+ }
117+ ```
118118
119+ After that, we’ll define our application’s initial state in the ` Init ` method. ` Init `
120+ can return a ` Cmd ` that could perform some initial I/O. For now, we don't need
121+ to do any I/O, so for the command, we'll just return ` nil ` , which translates to
122+ "no command."
123+
124+ ``` go
125+ func (m model ) Init () tea .Cmd {
119126 // Just return `nil`, which means "no I/O right now, please."
120- return m, nil
127+ return nil
121128}
122129```
123130
124131### The Update Method
125132
126- Next up is the update method. The update function is called when ” things
133+ Next up is the update method. The update function is called when “ things
127134happen.” Its job is to look at what has happened and return an updated model in
128135response. It can also return a ` Cmd ` to make more things happen, but for now
129136don't worry about that part.
@@ -167,8 +174,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
167174 m.cursor ++
168175 }
169176
170- // The "enter" key and the space bar (a literal space) toggle the
171- // selected state for the item that the cursor is pointing at.
177+ // The "enter" key and the space bar toggle the selected state
178+ // for the item that the cursor is pointing at.
172179 case " enter" , " space" :
173180 _ , ok := m.selected [m.cursor ]
174181 if ok {
@@ -192,15 +199,16 @@ the Bubble Tea runtime to quit, exiting the program.
192199### The View Method
193200
194201At last, it’s time to render our UI. Of all the methods, the view is the
195- simplest. We look at the model in its current state and use it to return
196- a ` string ` . That string is our UI!
202+ simplest. We look at the model in its current state and use it to build a
203+ ` tea.View ` . The view declares our UI content and, optionally, terminal features
204+ like alt screen mode, mouse tracking, cursor position, and more.
197205
198206Because the view describes the entire UI of your application, you don’t have to
199207worry about redrawing logic and stuff like that. Bubble Tea takes care of it
200208for you.
201209
202210``` go
203- func (m model ) View () string {
211+ func (m model ) View () tea . View {
204212 // The header
205213 s := " What should we buy at the market?\n\n "
206214
@@ -227,7 +235,7 @@ func (m model) View() string {
227235 s += " \n Press q to quit.\n "
228236
229237 // Send the UI for rendering
230- return s
238+ return tea. NewView (s)
231239}
232240```
233241
@@ -257,7 +265,7 @@ there are [Go Docs][docs].
257265
258266[ cmd ] : https://github.com/charmbracelet/bubbletea/tree/main/tutorials/commands/
259267[ examples ] : https://github.com/charmbracelet/bubbletea/tree/main/examples
260- [ docs ] : https://pkg.go.dev/github.com/charmbracelet/ bubbletea?tab=doc
268+ [ docs ] : https://pkg.go.dev/charm.land/ bubbletea/v2 ?tab=doc
261269
262270## Debugging
263271
@@ -320,7 +328,7 @@ your program in another window.
320328
321329## Bubble Tea in the Wild
322330
323- There are over [ 10 ,000 applications] ( https://github.com/charmbracelet/bubbletea/network/dependents ) built with Bubble Tea! Here are a handful of ’em.
331+ There are over [ 18 ,000 applications] ( https://github.com/charmbracelet/bubbletea/network/dependents ) built with Bubble Tea! Here are a handful of ’em.
324332
325333### Staff favourites
326334
@@ -333,14 +341,14 @@ There are over [10,000 applications](https://github.com/charmbracelet/bubbletea/
333341
334342### In Industry
335343
336- - Microsoft Azure – [ Aztify] ( https://github.com/Azure/aztfy ) : bring Microsoft Azure resources under Terraform
337- - Daytona – [ Daytona] ( https://github.com/daytonaio/daytona ) : open source dev environment manager
344+ - Microsoft Azure – [ Aztify] ( https://github.com/Azure/aztfy ) : bring Microsoft Azure resources under Terraform
345+ - Daytona – [ Daytona] ( https://github.com/daytonaio/daytona ) : an AI infrastructure platform
338346- Cockroach Labs – [ CockroachDB] ( https://github.com/cockroachdb/cockroach ) : a cloud-native, high-availability distributed SQL database
339- - Truffle Security Co. – [ Trufflehog] ( https://github.com/trufflesecurity/trufflehog ) : find leaked credentials
340- - NVIDIA – [ container-canary] ( https://github.com/NVIDIA/container-canary ) : a container validator
341- - AWS – [ eks-node-viewer] ( https://github.com/awslabs/eks-node-viewer ) : a tool for visualizing dynamic node usage within an EKS cluster
342- - MinIO – [ mc] ( https://github.com/minio/mc ) : the official [ MinIO] ( https://min.io ) client
343- - Ubuntu – [ Authd] ( https://github.com/ubuntu/authd ) : an authentication daemon for cloud-based identity providers
347+ - Truffle Security Co. – [ Trufflehog] ( https://github.com/trufflesecurity/trufflehog ) : find leaked credentials
348+ - NVIDIA – [ container-canary] ( https://github.com/NVIDIA/container-canary ) : a container validator
349+ - AWS – [ eks-node-viewer] ( https://github.com/awslabs/eks-node-viewer ) : a tool for visualizing dynamic node usage within an EKS cluster
350+ - MinIO – [ mc] ( https://github.com/minio/mc ) : the official [ MinIO] ( https://min.io ) client
351+ - Ubuntu – [ Authd] ( https://github.com/ubuntu/authd ) : an authentication daemon for cloud-based identity providers
344352
345353### Charm stuff
346354
@@ -389,6 +397,6 @@ of days past.
389397
390398Part of [ Charm] ( https://charm.sh ) .
391399
392- <a href =" https://charm.sh/ " ><img alt =" The Charm logo " src =" https://stuff.charm.sh/charm-badge .jpg " width =" 400 " ></a >
400+ <a href =" https://charm.sh/ " ><img alt =" The Charm logo " src =" https://stuff.charm.sh/charm-banner-next .jpg " width =" 400 " ></a >
393401
394402Charm热爱开源 • Charm loves open source • نحنُ نحب المصادر المفتوحة
0 commit comments