mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 11:09:17 -07:00
Initial commit. First pass.
This commit is contained in:
66
example/main.go
Normal file
66
example/main.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"tea"
|
||||
)
|
||||
|
||||
type Model int
|
||||
|
||||
func main() {
|
||||
p := tea.NewProgram(0, update, view)
|
||||
p.Start()
|
||||
}
|
||||
|
||||
func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
|
||||
m, _ := model.(Model)
|
||||
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyPressMsg:
|
||||
switch msg {
|
||||
|
||||
case "j":
|
||||
m += 1
|
||||
if m > 3 {
|
||||
m = 3
|
||||
}
|
||||
|
||||
case "k":
|
||||
m -= 1
|
||||
if m < 0 {
|
||||
m = 0
|
||||
}
|
||||
|
||||
case "q":
|
||||
return m, tea.Quit
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func view(model tea.Model) string {
|
||||
m, _ := model.(Model)
|
||||
|
||||
choices := fmt.Sprintf(
|
||||
"%s\n%s\n%s\n%s",
|
||||
checkbox("Plant carrots", m == 0),
|
||||
checkbox("Go to the market", m == 1),
|
||||
checkbox("Read something", m == 2),
|
||||
checkbox("See friends", m == 3),
|
||||
)
|
||||
|
||||
return fmt.Sprintf(
|
||||
"What to do today?\n\n%s.\n\n(press j/k to select or q to quit)",
|
||||
choices,
|
||||
)
|
||||
}
|
||||
|
||||
func checkbox(label string, checked bool) string {
|
||||
check := " "
|
||||
if checked {
|
||||
check = "x"
|
||||
}
|
||||
return fmt.Sprintf("[%s] %s", check, label)
|
||||
}
|
||||
Reference in New Issue
Block a user