From 7c0bbc7d3220931c62dea750f04f5755778ab9cb Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Sat, 1 May 2021 09:28:58 -0400 Subject: [PATCH] Clean up and normalize examples --- examples/countdown/main.go | 11 ++++---- examples/fullscreen/main.go | 16 +++--------- examples/glamour/main.go | 2 +- examples/http/main.go | 6 +---- examples/mouse/main.go | 2 +- examples/pager/main.go | 3 +-- examples/paginator/main.go | 6 ++--- examples/pipe/main.go | 10 +++---- examples/progress/main.go | 2 +- examples/result/main.go | 52 +++++++++++++++---------------------- examples/simple/main.go | 3 ++- examples/spinner/main.go | 22 +++++++--------- examples/spinners/main.go | 4 +-- examples/textinput/main.go | 6 +---- examples/textinputs/main.go | 3 +-- 15 files changed, 56 insertions(+), 92 deletions(-) diff --git a/examples/countdown/main.go b/examples/countdown/main.go index 0eeefd7..5bb33b2 100644 --- a/examples/countdown/main.go +++ b/examples/countdown/main.go @@ -37,6 +37,11 @@ func (m model) Init() tea.Cmd { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { + case tea.KeyMsg: + switch msg.String() { + case "ctrl+c", "q", "esc": + return m, tea.Quit + } case tickMsg: t := time.Time(msg) @@ -45,12 +50,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } m.lastTick = t return m, tick() - - case tea.KeyMsg: - switch msg.String() { - case "ctrl+c", "q": - return m, tea.Quit - } } return m, nil diff --git a/examples/fullscreen/main.go b/examples/fullscreen/main.go index 4ad9464..62259c7 100644 --- a/examples/fullscreen/main.go +++ b/examples/fullscreen/main.go @@ -16,19 +16,13 @@ type model int type tickMsg time.Time func main() { - p := tea.NewProgram(model(5)) - - // Bubble Tea will automatically exit the alternate screen buffer. - p.EnterAltScreen() - err := p.Start() - - if err != nil { + if err := tea.NewProgram(model(5)).Start(); err != nil { log.Fatal(err) } } func (m model) Init() tea.Cmd { - return tick() + return tea.Batch(tick(), tea.EnterAltScreen) } func (m model) Update(message tea.Msg) (tea.Model, tea.Cmd) { @@ -36,11 +30,7 @@ func (m model) Update(message tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyMsg: switch msg.String() { - case "ctrl+c": - fallthrough - case "esc": - fallthrough - case "q": + case "q", "esc", "ctrl+c": return m, tea.Quit } diff --git a/examples/glamour/main.go b/examples/glamour/main.go index c0ce21e..18f3fce 100644 --- a/examples/glamour/main.go +++ b/examples/glamour/main.go @@ -86,7 +86,7 @@ func (e example) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyMsg: switch msg.String() { - case "q", "ctrl+c": + case "q", "ctrl+c", "esc": return e, tea.Quit default: vp, cmd := e.viewport.Update(msg) diff --git a/examples/http/main.go b/examples/http/main.go index 00462a0..24f552f 100644 --- a/examples/http/main.go +++ b/examples/http/main.go @@ -40,11 +40,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyMsg: switch msg.String() { - case "esc": - fallthrough - case "ctrl+c": - fallthrough - case "q": + case "q", "ctrl+c", "esc": return m, tea.Quit default: return m, nil diff --git a/examples/mouse/main.go b/examples/mouse/main.go index 442051f..aac9600 100644 --- a/examples/mouse/main.go +++ b/examples/mouse/main.go @@ -35,7 +35,7 @@ func (m model) Init() tea.Cmd { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: - if s := msg.String(); s == "ctrl+c" || s == "q" { + if s := msg.String(); s == "ctrl+c" || s == "q" || s == "esc" { return m, tea.Quit } diff --git a/examples/pager/main.go b/examples/pager/main.go index 0270cd1..ca5df3f 100644 --- a/examples/pager/main.go +++ b/examples/pager/main.go @@ -87,8 +87,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: - // Ctrl+c exits - if k := msg.String(); k == "ctrl+c" || k == "q" { + if k := msg.String(); k == "ctrl+c" || k == "q" || k == "esc" { return m, tea.Quit } diff --git a/examples/paginator/main.go b/examples/paginator/main.go index 53cece5..9aa042c 100644 --- a/examples/paginator/main.go +++ b/examples/paginator/main.go @@ -24,8 +24,8 @@ func newModel() model { p := paginator.NewModel() p.Type = paginator.Dots p.PerPage = 10 - p.ActiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#847A85", Dark: "#979797"}).Render("•") - p.InactiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#C2B8C2", Dark: "#4D4D4D"}).Render("•") + p.ActiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "235", Dark: "252"}).Render("•") + p.InactiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "250", Dark: "238"}).Render("•") p.SetTotalPages(len(items)) return model{ @@ -48,7 +48,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: switch msg.String() { - case "q": + case "q", "esc", "ctrl+c": return m, tea.Quit } } diff --git a/examples/pipe/main.go b/examples/pipe/main.go index 57d48da..b44503b 100644 --- a/examples/pipe/main.go +++ b/examples/pipe/main.go @@ -1,11 +1,9 @@ package main -// An example of how to pipe in data to a Bubble Tea application. It's actually -// more of a proof that Bubble Tea will automatically listen for keystrokes -// when input is not a TTY, such as when data is piped or redirected in. -// -// In the case of this example we're listing for a single keystroke used to -// exit the program. +// An example illustating how to pipe in data to a Bubble Tea application. +// Moreso, this serves as proof that Bubble Tea will automatically listen for +// keystrokes when input is not a TTY, such as when data is piped or redirected +// in. import ( "bufio" diff --git a/examples/progress/main.go b/examples/progress/main.go index bda12f7..d56bb70 100644 --- a/examples/progress/main.go +++ b/examples/progress/main.go @@ -46,7 +46,7 @@ func (e example) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyMsg: switch msg.String() { - case "q", "ctrl+c": + case "q", "ctrl+c", "esc": return e, tea.Quit default: return e, nil diff --git a/examples/result/main.go b/examples/result/main.go index 8dd55b1..4182467 100644 --- a/examples/result/main.go +++ b/examples/result/main.go @@ -13,41 +13,13 @@ import ( tea "github.com/charmbracelet/bubbletea" ) -var ( - choices = []string{"Taro", "Coffee", "Lychee"} -) +var choices = []string{"Taro", "Coffee", "Lychee"} type model struct { cursor int choice chan string } -func main() { - // This is where we'll listen for the choice the user makes in the Bubble - // Tea program. - result := make(chan string, 1) - - // Pass the channel to the initialize function so our Bubble Tea program - // can send the final choice along when the time comes. - p := tea.NewProgram(model{cursor: 0, choice: result}) - if err := p.Start(); err != nil { - fmt.Println("Oh no:", err) - os.Exit(1) - } - - // Print out the final choice. - if r := <-result; r != "" { - fmt.Printf("\n---\nYou chose %s!\n", r) - } -} - -// Pass a channel to the model to listen to the result value. This is a -// function that returns the initialize function and is typically how you would -// pass arguments to a tea.Init function. -func initialModel(choice chan string) model { - return model{cursor: 0, choice: choice} -} - func (m model) Init() tea.Cmd { return nil } @@ -56,8 +28,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: switch msg.String() { - - case "ctrl+c", "q": + case "ctrl+c", "q", "esc": close(m.choice) // If we're quitting just chose the channel. return m, tea.Quit @@ -101,3 +72,22 @@ func (m model) View() string { return s.String() } + +func main() { + // This is where we'll listen for the choice the user makes in the Bubble + // Tea program. + result := make(chan string, 1) + + // Pass the channel to the initialize function so our Bubble Tea program + // can send the final choice along when the time comes. + p := tea.NewProgram(model{cursor: 0, choice: result}) + if err := p.Start(); err != nil { + fmt.Println("Oh no:", err) + os.Exit(1) + } + + // Print out the final choice. + if r := <-result; r != "" { + fmt.Printf("\n---\nYou chose %s!\n", r) + } +} diff --git a/examples/simple/main.go b/examples/simple/main.go index 48cc097..38989d6 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -12,7 +12,8 @@ import ( ) func main() { - // Log to a file. Useful in debugging. Not required. + // Log to a file. Useful in debugging since you can't really log to stdout. + // Not required. logfilePath := os.Getenv("BUBBLETEA_LOG") if logfilePath != "" { if _, err := tea.LogToFile(logfilePath, "simple"); err != nil { diff --git a/examples/spinner/main.go b/examples/spinner/main.go index 2dcc11a..93d8b73 100644 --- a/examples/spinner/main.go +++ b/examples/spinner/main.go @@ -23,14 +23,6 @@ type model struct { err error } -func main() { - p := tea.NewProgram(initialModel()) - if err := p.Start(); err != nil { - fmt.Println(err) - os.Exit(1) - } -} - func initialModel() model { s := spinner.NewModel() s.Spinner = spinner.Dot @@ -47,11 +39,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyMsg: switch msg.String() { - case "q": - fallthrough - case "esc": - fallthrough - case "ctrl+c": + case "q", "esc", "ctrl+c": m.quitting = true return m, tea.Quit default: @@ -81,3 +69,11 @@ func (m model) View() string { } return str } + +func main() { + p := tea.NewProgram(initialModel()) + if err := p.Start(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/examples/spinners/main.go b/examples/spinners/main.go index e466853..fed6c5b 100644 --- a/examples/spinners/main.go +++ b/examples/spinners/main.go @@ -51,6 +51,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: switch msg.String() { + case "ctrl+c", "q", "esc": + return m, tea.Quit case "h", "left": m.index-- if m.index <= 0 { @@ -65,8 +67,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } m.resetSpinner() return m, spinner.Tick - case "ctrl+c", "q": - return m, tea.Quit default: return m, nil } diff --git a/examples/textinput/main.go b/examples/textinput/main.go index 5a656f4..25617ae 100644 --- a/examples/textinput/main.go +++ b/examples/textinput/main.go @@ -50,11 +50,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: switch msg.Type { - case tea.KeyCtrlC: - fallthrough - case tea.KeyEsc: - fallthrough - case tea.KeyEnter: + case tea.KeyEnter, tea.KeyCtrlC, tea.KeyEsc: return m, tea.Quit } diff --git a/examples/textinputs/main.go b/examples/textinputs/main.go index d9d63b3..20b0ad5 100644 --- a/examples/textinputs/main.go +++ b/examples/textinputs/main.go @@ -67,8 +67,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: switch msg.String() { - - case "ctrl+c": + case "ctrl+c", "esc": return m, tea.Quit // Cycle between inputs