2020-01-17 22:21:51 -05:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
// A simple program that counts down from 5 and then exits.
|
|
|
|
|
|
|
|
|
|
import (
|
2020-01-18 10:13:44 -05:00
|
|
|
|
"errors"
|
2020-01-17 22:21:51 -05:00
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
|
2020-01-18 11:04:09 -05:00
|
|
|
|
"github.com/charmbracelet/tea"
|
|
|
|
|
"github.com/charmbracelet/teaparty/input"
|
2020-01-17 22:21:51 -05:00
|
|
|
|
)
|
|
|
|
|
|
2020-01-18 10:13:44 -05:00
|
|
|
|
type Model struct {
|
|
|
|
|
Input input.Model
|
|
|
|
|
Error error
|
2020-01-17 22:21:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type tickMsg struct{}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
tea.UseSysLog("tea")
|
|
|
|
|
|
|
|
|
|
p := tea.NewProgram(
|
2020-01-18 22:18:19 -05:00
|
|
|
|
initialize,
|
2020-01-17 22:21:51 -05:00
|
|
|
|
update,
|
|
|
|
|
view,
|
2020-01-25 21:27:43 -05:00
|
|
|
|
subscriptions,
|
2020-01-17 22:21:51 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err := p.Start(); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-18 22:18:19 -05:00
|
|
|
|
func initialize() (tea.Model, tea.Cmd) {
|
2020-02-01 22:28:31 -05:00
|
|
|
|
inputModel := input.DefaultModel()
|
|
|
|
|
inputModel.Placeholder = "Pikachu"
|
|
|
|
|
|
2020-01-18 22:18:19 -05:00
|
|
|
|
return Model{
|
2020-02-01 22:28:31 -05:00
|
|
|
|
Input: inputModel,
|
2020-01-18 22:18:19 -05:00
|
|
|
|
Error: nil,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-18 10:13:44 -05:00
|
|
|
|
func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
|
2020-01-17 22:21:51 -05:00
|
|
|
|
var cmd tea.Cmd
|
2020-01-18 10:13:44 -05:00
|
|
|
|
m, ok := model.(Model)
|
|
|
|
|
if !ok {
|
|
|
|
|
// When we encounter errors in Update we simply add the error to the
|
|
|
|
|
// model so we can handle it in the view. We could also return a command
|
|
|
|
|
// that does something else with the error, like logs it via IO.
|
|
|
|
|
m.Error = errors.New("could not perform assertion on model")
|
|
|
|
|
return m, nil
|
|
|
|
|
}
|
2020-01-17 22:21:51 -05:00
|
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
|
case tea.KeyMsg:
|
2020-01-26 16:46:30 -05:00
|
|
|
|
switch msg.Type {
|
|
|
|
|
case tea.KeyCtrlC:
|
2020-01-17 22:21:51 -05:00
|
|
|
|
fallthrough
|
2020-01-26 16:46:30 -05:00
|
|
|
|
case tea.KeyEsc:
|
2020-01-17 22:21:51 -05:00
|
|
|
|
return m, tea.Quit
|
|
|
|
|
}
|
2020-01-18 10:13:44 -05:00
|
|
|
|
|
|
|
|
|
// We handle errors just like any other message
|
|
|
|
|
case tea.ErrMsg:
|
|
|
|
|
m.Error = msg
|
|
|
|
|
return m, nil
|
2020-01-17 22:21:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-18 10:13:44 -05:00
|
|
|
|
m.Input, cmd = input.Update(msg, m.Input)
|
2020-01-17 22:21:51 -05:00
|
|
|
|
return m, cmd
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-25 21:27:43 -05:00
|
|
|
|
func subscriptions(model tea.Model) tea.Subs {
|
|
|
|
|
return tea.Subs{
|
|
|
|
|
// We just hand off the subscription to the input component, giving
|
|
|
|
|
// it the model it expects.
|
|
|
|
|
"input": func(model tea.Model) tea.Msg {
|
|
|
|
|
m, _ := model.(Model)
|
|
|
|
|
return input.Blink(m.Input)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-18 10:13:44 -05:00
|
|
|
|
func view(model tea.Model) string {
|
|
|
|
|
m, ok := model.(Model)
|
|
|
|
|
if !ok {
|
|
|
|
|
return "Oh no: could not perform assertion on model."
|
|
|
|
|
} else if m.Error != nil {
|
|
|
|
|
return fmt.Sprintf("Uh oh: %s", m.Error)
|
2020-01-17 23:27:54 -05:00
|
|
|
|
}
|
2020-01-18 10:13:44 -05:00
|
|
|
|
return fmt.Sprintf(
|
|
|
|
|
"What’s your favorite Pokémon?\n\n%s\n\n%s",
|
|
|
|
|
input.View(m.Input),
|
|
|
|
|
"(esc to quit)",
|
|
|
|
|
)
|
2020-01-17 22:21:51 -05:00
|
|
|
|
}
|