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
Provenance
- Commit
7c0bbc7- Tree
258a3ab- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
64ae19f
15 files changed+56 added−92 removed
modified:examples/countdown/main.go+5 added-6 removed
35 unmodified lines36 }37 38 func (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+ }45 46 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()48-49- case tea.KeyMsg:50- switch msg.String() {51- case "ctrl+c", "q":52- return m, tea.Quit53- }53 }54 55 return m, nil56 }13 unmodified linesmodified:examples/fullscreen/main.go+3 added-13 removed
14 unmodified lines15 16 type tickMsg time.Time17 18 func main() {19- p := tea.NewProgram(model(5))20-21- // Bubble Tea will automatically exit the alternate screen buffer.22- p.EnterAltScreen()23- err := p.Start()24-25- if err != nil {19+ if err := tea.NewProgram(model(5)).Start(); err != nil {20 log.Fatal(err)21 }22 }23 24 func (m model) Init() tea.Cmd {31- return tick()25+ return tea.Batch(tick(), tea.EnterAltScreen)26 }27 28 func (m model) Update(message tea.Msg) (tea.Model, tea.Cmd) {29 switch msg := message.(type) {30 31 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 }36 37 case tickMsg:20 unmodified linesmodified:examples/glamour/main.go+1 added-1 removed
84 unmodified lines85 return e, nil86 87 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 = vp27 unmodified linesmodified:examples/http/main.go+1 added-5 removed
38 unmodified lines39 switch msg := msg.(type) {40 41 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 linesmodified:examples/mouse/main.go+1 added-1 removed
33 unmodified lines34 35 func (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 }41 42 case tea.MouseMsg:17 unmodified linesmodified:examples/pager/main.go+1 added-2 removed
85 unmodified lines86 )87 88 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 }93 94 case tea.WindowSizeMsg:63 unmodified linesmodified:examples/paginator/main.go+3 added-3 removed
22 unmodified lines23 24 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))30 31 return model{32 paginator: p,14 unmodified lines47 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 linesmodified:examples/pipe/main.go+4 added-6 removed
1 package main2 3-// 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.7 8 import (9 "bufio"10 "fmt"83 unmodified linesmodified:examples/progress/main.go+1 added-1 removed
44 unmodified lines45 switch msg := msg.(type) {46 47 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 linesmodified:examples/result/main.go+21 added-31 removed
11 unmodified lines12 13 tea "github.com/charmbracelet/bubbletea"14 )15 16-var (17- choices = []string{"Taro", "Coffee", "Lychee"}16+var choices = []string{"Taro", "Coffee", "Lychee"}18-)17 18 type model struct {19 cursor int20 choice chan string21 }22 25-func 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)29-30- // 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- }37-38- // Print out the final choice.39- if r := <-result; r != "" {40- fmt.Printf("\n---\nYou chose %s!\n", r)41- }42-}43-44-// 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.47-func initialModel(choice chan string) model {48- return model{cursor: 0, choice: choice}49-}50-23 func (m model) Init() tea.Cmd {24 return nil25 }26 27 func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {28 switch msg := msg.(type) {29 case tea.KeyMsg:30 switch msg.String() {59-60- 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.Quit34 35 case "enter":35 unmodified lines71 s.WriteString("\n(press q to quit)\n")72 73 return s.String()74 }75+76+func 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)80+81+ // 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+ }88+89+ // 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 lines11 tea "github.com/charmbracelet/bubbletea"12 )13 14 func 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 linesmodified:examples/spinner/main.go+9 added-13 removed
21 unmodified lines22 quitting bool23 err error24 }25 26-func main() {27- p := tea.NewProgram(initialModel())28- if err := p.Start(); err != nil {29- fmt.Println(err)30- os.Exit(1)31- }32-}33-26 func initialModel() model {27 s := spinner.NewModel()28 s.Spinner = spinner.Dot29 s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))8 unmodified lines38 switch msg := msg.(type) {39 40 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, nil21 unmodified lines68 return str + "\n"69 }70 return str71 }72+73+func 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 lines50 func (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) - 16 unmodified lines66 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 linesmodified:examples/textinput/main.go+1 added-5 removed
48 unmodified lines49 50 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 }56 57 // We handle errors just like any other message16 unmodified linesmodified:examples/textinputs/main.go+1 added-2 removed
65 unmodified lines66 67 switch msg := msg.(type) {68 case tea.KeyMsg:69 switch msg.String() {70-71- case "ctrl+c":70+ case "ctrl+c", "esc":71 return m, tea.Quit72 73 // Cycle between inputs74 case "tab", "shift+tab", "enter", "up", "down":101 unmodified lines