Skip to content
Myr

Clean up and normalize examples

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christian Rocha · committed by Christian Rocha · parent 64ae19f · Browse files · 15 files+56 added−92 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

15 files+56 added−92 removed

  • other:+56 added−92 removed

15 files+56 added−92 removed

modified:examples/countdown/main.go+5 added-6 removed
35 unmodified lines
36}3738func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {39 switch msg := msg.(type) {40 case tea.KeyMsg:41 switch msg.String() {42 case "ctrl+c", "q", "esc":43 return m, tea.Quit44 }4546 case tickMsg:47 t := time.Time(msg)48 if t.After(m.timeout) {49 return m, tea.Quit50 }51 m.lastTick = t52 return m, tick()4849 case tea.KeyMsg:50 switch msg.String() {51 case "ctrl+c", "q":52 return m, tea.Quit53 }53 }5455 return m, nil56}
13 unmodified lines
modified:examples/fullscreen/main.go+3 added-13 removed
14 unmodified lines
1516type tickMsg time.Time1718func main() {19 p := tea.NewProgram(model(5))2021 // Bubble Tea will automatically exit the alternate screen buffer.22 p.EnterAltScreen()23 err := p.Start()2425 if err != nil {19 if err := tea.NewProgram(model(5)).Start(); err != nil {20 log.Fatal(err)21 }22}2324func (m model) Init() tea.Cmd {31 return tick()25 return tea.Batch(tick(), tea.EnterAltScreen)26}2728func (m model) Update(message tea.Msg) (tea.Model, tea.Cmd) {29 switch msg := message.(type) {3031 case tea.KeyMsg:32 switch msg.String() {39 case "ctrl+c":40 fallthrough41 case "esc":42 fallthrough43 case "q":33 case "q", "esc", "ctrl+c":34 return m, tea.Quit35 }3637 case tickMsg:
20 unmodified lines
modified:examples/glamour/main.go+1 added-1 removed
84 unmodified lines
85 return e, nil8687 case tea.KeyMsg:88 switch msg.String() {89 case "q", "ctrl+c":89 case "q", "ctrl+c", "esc":90 return e, tea.Quit91 default:92 vp, cmd := e.viewport.Update(msg)93 e.viewport = vp
27 unmodified lines
modified:examples/http/main.go+1 added-5 removed
38 unmodified lines
39 switch msg := msg.(type) {4041 case tea.KeyMsg:42 switch msg.String() {43 case "esc":44 fallthrough45 case "ctrl+c":46 fallthrough47 case "q":43 case "q", "ctrl+c", "esc":44 return m, tea.Quit45 default:46 return m, nil47 }
34 unmodified lines
modified:examples/mouse/main.go+1 added-1 removed
33 unmodified lines
3435func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {36 switch msg := msg.(type) {37 case tea.KeyMsg:38 if s := msg.String(); s == "ctrl+c" || s == "q" {38 if s := msg.String(); s == "ctrl+c" || s == "q" || s == "esc" {39 return m, tea.Quit40 }4142 case tea.MouseMsg:
17 unmodified lines
modified:examples/pager/main.go+1 added-2 removed
85 unmodified lines
86 )8788 switch msg := msg.(type) {89 case tea.KeyMsg:90 // Ctrl+c exits91 if k := msg.String(); k == "ctrl+c" || k == "q" {90 if k := msg.String(); k == "ctrl+c" || k == "q" || k == "esc" {91 return m, tea.Quit92 }9394 case tea.WindowSizeMsg:
63 unmodified lines
modified:examples/paginator/main.go+3 added-3 removed
22 unmodified lines
2324 p := paginator.NewModel()25 p.Type = paginator.Dots26 p.PerPage = 1027 p.ActiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#847A85", Dark: "#979797"}).Render("•")28 p.InactiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#C2B8C2", Dark: "#4D4D4D"}).Render("•")27 p.ActiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "235", Dark: "252"}).Render("•")28 p.InactiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "250", Dark: "238"}).Render("•")29 p.SetTotalPages(len(items))3031 return model{32 paginator: p,
14 unmodified lines
47 var cmd tea.Cmd48 switch msg := msg.(type) {49 case tea.KeyMsg:50 switch msg.String() {51 case "q":51 case "q", "esc", "ctrl+c":52 return m, tea.Quit53 }54 }55 m.paginator, cmd = m.paginator.Update(msg)
21 unmodified lines
modified:examples/pipe/main.go+4 added-6 removed
1package main23// An example of how to pipe in data to a Bubble Tea application. It's actually4// more of a proof that Bubble Tea will automatically listen for keystrokes5// when input is not a TTY, such as when data is piped or redirected in.6//7// In the case of this example we're listing for a single keystroke used to8// exit the program.3// An example illustating how to pipe in data to a Bubble Tea application.4// Moreso, this serves as proof that Bubble Tea will automatically listen for5// keystrokes when input is not a TTY, such as when data is piped or redirected6// in.78import (9 "bufio"10 "fmt"
83 unmodified lines
modified:examples/progress/main.go+1 added-1 removed
44 unmodified lines
45 switch msg := msg.(type) {4647 case tea.KeyMsg:48 switch msg.String() {49 case "q", "ctrl+c":49 case "q", "ctrl+c", "esc":50 return e, tea.Quit51 default:52 return e, nil53 }
31 unmodified lines
modified:examples/result/main.go+21 added-31 removed
11 unmodified lines
1213 tea "github.com/charmbracelet/bubbletea"14)1516var (17 choices = []string{"Taro", "Coffee", "Lychee"}16var choices = []string{"Taro", "Coffee", "Lychee"}18)1718type model struct {19 cursor int20 choice chan string21}2225func main() {26 // This is where we'll listen for the choice the user makes in the Bubble27 // Tea program.28 result := make(chan string, 1)2930 // Pass the channel to the initialize function so our Bubble Tea program31 // can send the final choice along when the time comes.32 p := tea.NewProgram(model{cursor: 0, choice: result})33 if err := p.Start(); err != nil {34 fmt.Println("Oh no:", err)35 os.Exit(1)36 }3738 // Print out the final choice.39 if r := <-result; r != "" {40 fmt.Printf("\n---\nYou chose %s!\n", r)41 }42}4344// Pass a channel to the model to listen to the result value. This is a45// function that returns the initialize function and is typically how you would46// pass arguments to a tea.Init function.47func initialModel(choice chan string) model {48 return model{cursor: 0, choice: choice}49}5023func (m model) Init() tea.Cmd {24 return nil25}2627func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {28 switch msg := msg.(type) {29 case tea.KeyMsg:30 switch msg.String() {5960 case "ctrl+c", "q":31 case "ctrl+c", "q", "esc":32 close(m.choice) // If we're quitting just chose the channel.33 return m, tea.Quit3435 case "enter":
35 unmodified lines
71 s.WriteString("\n(press q to quit)\n")7273 return s.String()74}7576func main() {77 // This is where we'll listen for the choice the user makes in the Bubble78 // Tea program.79 result := make(chan string, 1)8081 // Pass the channel to the initialize function so our Bubble Tea program82 // can send the final choice along when the time comes.83 p := tea.NewProgram(model{cursor: 0, choice: result})84 if err := p.Start(); err != nil {85 fmt.Println("Oh no:", err)86 os.Exit(1)87 }8889 // Print out the final choice.90 if r := <-result; r != "" {91 fmt.Printf("\n---\nYou chose %s!\n", r)92 }93}
modified:examples/simple/main.go+2 added-1 removed
10 unmodified lines
11 tea "github.com/charmbracelet/bubbletea"12)1314func main() {15 // Log to a file. Useful in debugging. Not required.15 // Log to a file. Useful in debugging since you can't really log to stdout.16 // Not required.17 logfilePath := os.Getenv("BUBBLETEA_LOG")18 if logfilePath != "" {19 if _, err := tea.LogToFile(logfilePath, "simple"); err != nil {20 log.Fatal(err)
52 unmodified lines
modified:examples/spinner/main.go+9 added-13 removed
21 unmodified lines
22 quitting bool23 err error24}2526func main() {27 p := tea.NewProgram(initialModel())28 if err := p.Start(); err != nil {29 fmt.Println(err)30 os.Exit(1)31 }32}3326func initialModel() model {27 s := spinner.NewModel()28 s.Spinner = spinner.Dot29 s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
8 unmodified lines
38 switch msg := msg.(type) {3940 case tea.KeyMsg:41 switch msg.String() {50 case "q":51 fallthrough52 case "esc":53 fallthrough54 case "ctrl+c":42 case "q", "esc", "ctrl+c":43 m.quitting = true44 return m, tea.Quit45 default:46 return m, nil
21 unmodified lines
68 return str + "\n"69 }70 return str71}7273func main() {74 p := tea.NewProgram(initialModel())75 if err := p.Start(); err != nil {76 fmt.Println(err)77 os.Exit(1)78 }79}
modified:examples/spinners/main.go+2 added-2 removed
49 unmodified lines
50func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {51 switch msg := msg.(type) {52 case tea.KeyMsg:53 switch msg.String() {54 case "ctrl+c", "q", "esc":55 return m, tea.Quit56 case "h", "left":57 m.index--58 if m.index <= 0 {59 m.index = len(spinners) - 1
6 unmodified lines
66 m.index = 067 }68 m.resetSpinner()69 return m, spinner.Tick68 case "ctrl+c", "q":69 return m, tea.Quit70 default:71 return m, nil72 }73 case spinner.TickMsg:
27 unmodified lines
modified:examples/textinput/main.go+1 added-5 removed
48 unmodified lines
4950 switch msg := msg.(type) {51 case tea.KeyMsg:52 switch msg.Type {53 case tea.KeyCtrlC:54 fallthrough55 case tea.KeyEsc:56 fallthrough57 case tea.KeyEnter:53 case tea.KeyEnter, tea.KeyCtrlC, tea.KeyEsc:54 return m, tea.Quit55 }5657 // We handle errors just like any other message
16 unmodified lines
modified:examples/textinputs/main.go+1 added-2 removed
65 unmodified lines
6667 switch msg := msg.(type) {68 case tea.KeyMsg:69 switch msg.String() {7071 case "ctrl+c":70 case "ctrl+c", "esc":71 return m, tea.Quit7273 // Cycle between inputs74 case "tab", "shift+tab", "enter", "up", "down":
101 unmodified lines