Skip to content
Myr

Implement subscriptions, and update example accordingly

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christian Rocha · committed by Christian Rocha · parent 97bbcdd · Browse files · 2 files+79 added−32 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+79 added−32 removed

  • other:+79 added−32 removed

2 files+79 added−32 removed

modified:example/main.go+38 added-20 removed
1 unmodified line
23import (4 "fmt"5 "tea"6 "time"7)88type Model int9type Model struct {10 Choice int11 Ticks int12}1314type TickMsg struct{}1516const tpl = `What to do today?1718%s1920Elapsed: %d seconds.2122(press j/k or up/down to select, q or esc to quit)`2324func main() {11 p := tea.NewProgram(0, update, view)25 p := tea.NewProgram(Model{0, 0}, update, view, []tea.Sub{tick})26 if err := p.Start(); err != nil {27 fmt.Println("could not start program:", err)28 }29}
1 unmodified line
31func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {32 m, _ := model.(Model)3334 switch msg := msg.(type) {3536 case tea.KeyPressMsg:37 switch msg {2338 case "j":39 fallthrough40 case "down":27 m += 128 if m > 3 {29 m = 341 m.Choice += 142 if m.Choice > 3 {43 m.Choice = 344 }3145 case "k":46 fallthrough47 case "up":35 m -= 136 if m < 0 {37 m = 048 m.Choice -= 149 if m.Choice < 0 {50 m.Choice = 051 }3952 case "q":53 fallthrough54 case "esc":55 fallthrough56 case "ctrl+c":57 return m, tea.Quit4658 }5960 case TickMsg:61 m.Ticks += 162 }6364 return m, nil65}6667// Subscription68func tick(_ tea.Model) tea.Msg {69 time.Sleep(time.Second * 1)70 return TickMsg{}71}7273func view(model tea.Model) string {74 m, _ := model.(Model)75 c := m.Choice7677 choices := fmt.Sprintf(78 "%s\n%s\n%s\n%s",58 checkbox("Plant carrots", m == 0),59 checkbox("Go to the market", m == 1),60 checkbox("Read something", m == 2),61 checkbox("See friends", m == 3),79 checkbox("Plant carrots", c == 0),80 checkbox("Go to the market", c == 1),81 checkbox("Read something", c == 2),82 checkbox("See friends", c == 3),83 )8464 return fmt.Sprintf(65 "What to do today?\n\n%s\n\n(press j/k or up/down to select, q or esc to quit)",66 choices,67 )85 return fmt.Sprintf(tpl, choices, m.Ticks)86}8788func checkbox(label string, checked bool) string {89 check := " "
5 unmodified lines
modified:tea.go+41 added-12 removed
18 unmodified lines
1920// Cmd is an IO operation. If it's nil it's considered a no-op.21type Cmd func() Msg2223// Sub is an event subscription. If it's nil it's considered a no-op. But why24// would you subscribe to nil?25type Sub func(Model) Msg2627// Update is called when a message is received. It may update the model and/or28// send a command.29type Update func(Msg, Model) (Model, Cmd)30
1 unmodified line
32type View func(Model) string3334// Program is a terminal user interface35type Program struct {32 model Model33 update Update34 view View35 rw io.ReadWriter36 model Model37 update Update38 view View39 subscriptions []Sub40 rw io.ReadWriter41}4243// Quit command44func Quit() Msg {
3 unmodified lines
48// Signals that the program should quit49type quitMsg struct{}5051// NewProgram creates a new Program47func NewProgram(model Model, update Update, view View) *Program {52func NewProgram(model Model, update Update, view View, subs []Sub) *Program {53 return &Program{49 model: model,50 update: update,51 view: view,52 // TODO: subscriptions54 model: model,55 update: update,56 view: view,57 subscriptions: subs,58 }59}6061// Start initializes the program
23 unmodified lines
85 hideCursor()86 p.render(model, true)8788 // Subscribe to user input84 // TODO: move to program struct to allow for subscriptions to other things,85 // too, like timers, frames, download/upload progress and so on.89 // TODO: should we move this to the end-user program level or just keep this90 // here, since it blocks nicely and user input will probably be something91 // users typically need?92 go func() {93 for {94 select {95 default:
18 unmodified lines
114 }115 }116 }()117118 // Subscriptions. Subscribe to user input.119 // TODO: should the blocking `for` be here, or in the end-user portion120 // of the program?121 go func() {122 select {123 case <-done:124 return125 default:126 if len(p.subscriptions) > 0 {127 for _, sub := range p.subscriptions {128 if sub != nil {129 go func() {130 for {131 msgs <- sub(p.model)132 }133 }()134 }135 }136 }137 }138 }()139140 // Handle updates and draw141 for {142 select {143 case msg := <-msgs:
46 unmodified lines
190 clearLine()191 }192}193194// ClearScreen clears the visible portion of the terminal166func clearScreen() {195func ClearScreen() {196 fmt.Printf(esc + "2J" + esc + "3J" + esc + "1;1H")197}