Skip to content

Commit

Permalink
Clean up and normalize examples
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed May 1, 2021
1 parent 64ae19f commit 4331b0b
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 92 deletions.
11 changes: 5 additions & 6 deletions examples/countdown/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
16 changes: 3 additions & 13 deletions examples/fullscreen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,21 @@ 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) {
switch msg := message.(type) {

case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
fallthrough
case "esc":
fallthrough
case "q":
case "q", "esc", "ctrl+c":
return m, tea.Quit
}

Expand Down
2 changes: 1 addition & 1 deletion examples/glamour/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions examples/http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/mouse/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions examples/pager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions examples/paginator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
}
}
Expand Down
10 changes: 4 additions & 6 deletions examples/pipe/main.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion examples/progress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 21 additions & 31 deletions examples/result/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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

Expand Down Expand Up @@ -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)
}
}
3 changes: 2 additions & 1 deletion examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 9 additions & 13 deletions examples/spinner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)
}
}
4 changes: 2 additions & 2 deletions examples/spinners/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
6 changes: 1 addition & 5 deletions examples/textinput/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions examples/textinputs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4331b0b

Please sign in to comment.