Clean up and normalize examples

This commit is contained in:
Christian Rocha 2021-05-01 09:28:58 -04:00
parent 64ae19f37e
commit 7c0bbc7d32
15 changed files with 56 additions and 92 deletions

View File

@ -37,6 +37,11 @@ func (m model) Init() tea.Cmd {
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q", "esc":
return m, tea.Quit
}
case tickMsg: case tickMsg:
t := time.Time(msg) t := time.Time(msg)
@ -45,12 +50,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
m.lastTick = t m.lastTick = t
return m, tick() return m, tick()
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
}
} }
return m, nil return m, nil

View File

@ -16,19 +16,13 @@ type model int
type tickMsg time.Time type tickMsg time.Time
func main() { func main() {
p := tea.NewProgram(model(5)) if err := tea.NewProgram(model(5)).Start(); err != nil {
// Bubble Tea will automatically exit the alternate screen buffer.
p.EnterAltScreen()
err := p.Start()
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
func (m model) Init() tea.Cmd { 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) { 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: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "ctrl+c": case "q", "esc", "ctrl+c":
fallthrough
case "esc":
fallthrough
case "q":
return m, tea.Quit return m, tea.Quit
} }

View File

@ -86,7 +86,7 @@ func (e example) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "q", "ctrl+c": case "q", "ctrl+c", "esc":
return e, tea.Quit return e, tea.Quit
default: default:
vp, cmd := e.viewport.Update(msg) vp, cmd := e.viewport.Update(msg)

View File

@ -40,11 +40,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "esc": case "q", "ctrl+c", "esc":
fallthrough
case "ctrl+c":
fallthrough
case "q":
return m, tea.Quit return m, tea.Quit
default: default:
return m, nil return m, nil

View File

@ -35,7 +35,7 @@ func (m model) Init() tea.Cmd {
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: 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 return m, tea.Quit
} }

View File

@ -87,8 +87,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
// Ctrl+c exits if k := msg.String(); k == "ctrl+c" || k == "q" || k == "esc" {
if k := msg.String(); k == "ctrl+c" || k == "q" {
return m, tea.Quit return m, tea.Quit
} }

View File

@ -24,8 +24,8 @@ func newModel() model {
p := paginator.NewModel() p := paginator.NewModel()
p.Type = paginator.Dots p.Type = paginator.Dots
p.PerPage = 10 p.PerPage = 10
p.ActiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#847A85", Dark: "#979797"}).Render("•") p.ActiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "235", Dark: "252"}).Render("•")
p.InactiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#C2B8C2", Dark: "#4D4D4D"}).Render("•") p.InactiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "250", Dark: "238"}).Render("•")
p.SetTotalPages(len(items)) p.SetTotalPages(len(items))
return model{ return model{
@ -48,7 +48,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "q": case "q", "esc", "ctrl+c":
return m, tea.Quit return m, tea.Quit
} }
} }

View File

@ -1,11 +1,9 @@
package main package main
// An example of how to pipe in data to a Bubble Tea application. It's actually // An example illustating how to pipe in data to a Bubble Tea application.
// more of a proof that Bubble Tea will automatically listen for keystrokes // Moreso, this serves as proof that Bubble Tea will automatically listen for
// when input is not a TTY, such as when data is piped or redirected in. // 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.
import ( import (
"bufio" "bufio"

View File

@ -46,7 +46,7 @@ func (e example) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "q", "ctrl+c": case "q", "ctrl+c", "esc":
return e, tea.Quit return e, tea.Quit
default: default:
return e, nil return e, nil

View File

@ -13,41 +13,13 @@ import (
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
) )
var ( var choices = []string{"Taro", "Coffee", "Lychee"}
choices = []string{"Taro", "Coffee", "Lychee"}
)
type model struct { type model struct {
cursor int cursor int
choice chan string 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 { func (m model) Init() tea.Cmd {
return nil return nil
} }
@ -56,8 +28,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "ctrl+c", "q", "esc":
case "ctrl+c", "q":
close(m.choice) // If we're quitting just chose the channel. close(m.choice) // If we're quitting just chose the channel.
return m, tea.Quit return m, tea.Quit
@ -101,3 +72,22 @@ func (m model) View() string {
return s.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)
}
}

View File

@ -12,7 +12,8 @@ import (
) )
func main() { 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") logfilePath := os.Getenv("BUBBLETEA_LOG")
if logfilePath != "" { if logfilePath != "" {
if _, err := tea.LogToFile(logfilePath, "simple"); err != nil { if _, err := tea.LogToFile(logfilePath, "simple"); err != nil {

View File

@ -23,14 +23,6 @@ type model struct {
err error err error
} }
func main() {
p := tea.NewProgram(initialModel())
if err := p.Start(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func initialModel() model { func initialModel() model {
s := spinner.NewModel() s := spinner.NewModel()
s.Spinner = spinner.Dot s.Spinner = spinner.Dot
@ -47,11 +39,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "q": case "q", "esc", "ctrl+c":
fallthrough
case "esc":
fallthrough
case "ctrl+c":
m.quitting = true m.quitting = true
return m, tea.Quit return m, tea.Quit
default: default:
@ -81,3 +69,11 @@ func (m model) View() string {
} }
return str return str
} }
func main() {
p := tea.NewProgram(initialModel())
if err := p.Start(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

View File

@ -51,6 +51,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "ctrl+c", "q", "esc":
return m, tea.Quit
case "h", "left": case "h", "left":
m.index-- m.index--
if m.index <= 0 { if m.index <= 0 {
@ -65,8 +67,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
m.resetSpinner() m.resetSpinner()
return m, spinner.Tick return m, spinner.Tick
case "ctrl+c", "q":
return m, tea.Quit
default: default:
return m, nil return m, nil
} }

View File

@ -50,11 +50,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.Type { switch msg.Type {
case tea.KeyCtrlC: case tea.KeyEnter, tea.KeyCtrlC, tea.KeyEsc:
fallthrough
case tea.KeyEsc:
fallthrough
case tea.KeyEnter:
return m, tea.Quit return m, tea.Quit
} }

View File

@ -67,8 +67,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "ctrl+c", "esc":
case "ctrl+c":
return m, tea.Quit return m, tea.Quit
// Cycle between inputs // Cycle between inputs