forked from Mirrors/bubbletea
Clean up and normalize examples
This commit is contained in:
parent
64ae19f37e
commit
7c0bbc7d32
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue