2020-01-17 22:21:51 -05:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2020-01-18 10:13:44 -05:00
|
|
|
|
"errors"
|
2020-01-17 22:21:51 -05:00
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
|
2020-05-12 16:39:08 -04:00
|
|
|
|
"github.com/charmbracelet/boba"
|
2020-05-12 17:05:16 -04:00
|
|
|
|
input "github.com/charmbracelet/boba/textinput"
|
2020-01-17 22:21:51 -05:00
|
|
|
|
)
|
|
|
|
|
|
2020-01-18 10:13:44 -05:00
|
|
|
|
type Model struct {
|
2020-04-22 11:00:30 -04:00
|
|
|
|
textInput input.Model
|
|
|
|
|
err error
|
2020-01-17 22:21:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type tickMsg struct{}
|
2020-04-22 11:00:30 -04:00
|
|
|
|
type errMsg error
|
2020-01-17 22:21:51 -05:00
|
|
|
|
|
|
|
|
|
func main() {
|
2020-05-12 16:39:08 -04:00
|
|
|
|
p := boba.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-05-12 16:39:08 -04:00
|
|
|
|
func initialize() (boba.Model, boba.Cmd) {
|
2020-05-12 17:05:16 -04:00
|
|
|
|
inputModel := input.NewModel()
|
2020-02-01 22:28:31 -05:00
|
|
|
|
inputModel.Placeholder = "Pikachu"
|
2020-05-12 17:05:16 -04:00
|
|
|
|
inputModel.Focus()
|
2020-02-01 22:28:31 -05:00
|
|
|
|
|
2020-01-18 22:18:19 -05:00
|
|
|
|
return Model{
|
2020-04-22 11:00:30 -04:00
|
|
|
|
textInput: inputModel,
|
|
|
|
|
err: nil,
|
2020-01-18 22:18:19 -05:00
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 16:39:08 -04:00
|
|
|
|
func update(msg boba.Msg, model boba.Model) (boba.Model, boba.Cmd) {
|
|
|
|
|
var cmd boba.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.
|
2020-04-22 11:00:30 -04:00
|
|
|
|
return Model{
|
|
|
|
|
err: errors.New("could not perform assertion on model in update"),
|
|
|
|
|
}, nil
|
2020-01-18 10:13:44 -05:00
|
|
|
|
}
|
2020-01-17 22:21:51 -05:00
|
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
2020-05-12 16:39:08 -04:00
|
|
|
|
case boba.KeyMsg:
|
2020-01-26 16:46:30 -05:00
|
|
|
|
switch msg.Type {
|
2020-05-12 16:39:08 -04:00
|
|
|
|
case boba.KeyCtrlC:
|
2020-01-17 22:21:51 -05:00
|
|
|
|
fallthrough
|
2020-05-12 16:39:08 -04:00
|
|
|
|
case boba.KeyEsc:
|
2020-05-12 17:05:16 -04:00
|
|
|
|
fallthrough
|
|
|
|
|
case boba.KeyEnter:
|
2020-05-12 16:39:08 -04:00
|
|
|
|
return m, boba.Quit
|
2020-01-17 22:21:51 -05:00
|
|
|
|
}
|
2020-01-18 10:13:44 -05:00
|
|
|
|
|
|
|
|
|
// We handle errors just like any other message
|
2020-04-22 11:00:30 -04:00
|
|
|
|
case errMsg:
|
|
|
|
|
m.err = msg
|
2020-01-18 10:13:44 -05:00
|
|
|
|
return m, nil
|
2020-01-17 22:21:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 11:00:30 -04:00
|
|
|
|
m.textInput, cmd = input.Update(msg, m.textInput)
|
2020-01-17 22:21:51 -05:00
|
|
|
|
return m, cmd
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 16:39:08 -04:00
|
|
|
|
func subscriptions(model boba.Model) boba.Subs {
|
2020-05-12 17:05:16 -04:00
|
|
|
|
m, ok := model.(Model)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
sub, err := input.MakeSub(m.textInput)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2020-05-12 16:39:08 -04:00
|
|
|
|
return boba.Subs{
|
2020-05-12 17:05:16 -04:00
|
|
|
|
"input": sub,
|
2020-01-25 21:27:43 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 16:39:08 -04:00
|
|
|
|
func view(model boba.Model) string {
|
2020-01-18 10:13:44 -05:00
|
|
|
|
m, ok := model.(Model)
|
|
|
|
|
if !ok {
|
|
|
|
|
return "Oh no: could not perform assertion on model."
|
2020-04-22 11:00:30 -04:00
|
|
|
|
} else if m.err != nil {
|
|
|
|
|
return fmt.Sprintf("Uh oh: %s", m.err)
|
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",
|
2020-04-22 11:00:30 -04:00
|
|
|
|
input.View(m.textInput),
|
2020-01-18 10:13:44 -05:00
|
|
|
|
"(esc to quit)",
|
2020-05-12 17:05:16 -04:00
|
|
|
|
) + "\n"
|
2020-01-17 22:21:51 -05:00
|
|
|
|
}
|