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
Provenance
- Commit
147c8df- Tree
0628408- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
97bbcdd
2 files changed+79 added−32 removed
modified:example/main.go+38 added-20 removed
1 unmodified line2 3 import (4 "fmt"5 "tea"6+ "time"7 )8 8-type Model int9+type Model struct {10+ Choice int11+ Ticks int12+}13 14+type TickMsg struct{}15+16+const tpl = `What to do today?17+18+%s19+20+Elapsed: %d seconds.21+22+(press j/k or up/down to select, q or esc to quit)`23+24 func 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 line31 func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {32 m, _ := model.(Model)33 34 switch msg := msg.(type) {35+36 case tea.KeyPressMsg:37 switch msg {23-38 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 }31-45 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 }39-52 case "q":53 fallthrough54 case "esc":55 fallthrough56 case "ctrl+c":57 return m, tea.Quit46-58 }59+60+ case TickMsg:61+ m.Ticks += 162 }63 64 return m, nil65 }66 67+// Subscription68+func tick(_ tea.Model) tea.Msg {69+ time.Sleep(time.Second * 1)70+ return TickMsg{}71+}72+73 func view(model tea.Model) string {74 m, _ := model.(Model)75+ c := m.Choice76 77 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 )84 64- 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 }87 88 func checkbox(label string, checked bool) string {89 check := " "5 unmodified linesmodified:tea.go+41 added-12 removed
18 unmodified lines19 20 // Cmd is an IO operation. If it's nil it's considered a no-op.21 type Cmd func() Msg22 23+// Sub is an event subscription. If it's nil it's considered a no-op. But why24+// would you subscribe to nil?25+type Sub func(Model) Msg26+27 // Update is called when a message is received. It may update the model and/or28 // send a command.29 type Update func(Msg, Model) (Model, Cmd)30 1 unmodified line32 type View func(Model) string33 34 // Program is a terminal user interface35 type Program struct {32- model Model33- update Update34- view View35- rw io.ReadWriter36+ model Model37+ update Update38+ view View39+ subscriptions []Sub40+ rw io.ReadWriter41 }42 43 // Quit command44 func Quit() Msg {3 unmodified lines48 // Signals that the program should quit49 type quitMsg struct{}50 51 // NewProgram creates a new Program47-func NewProgram(model Model, update Update, view View) *Program {52+func 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 }60 61 // Start initializes the program23 unmodified lines85 hideCursor()86 p.render(model, true)87 88 // 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 lines114 }115 }116 }()117 118+ // 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+ }()139+140 // Handle updates and draw141 for {142 select {143 case msg := <-msgs:46 unmodified lines190 clearLine()191 }192 }193 194+// ClearScreen clears the visible portion of the terminal166-func clearScreen() {195+func ClearScreen() {196 fmt.Printf(esc + "2J" + esc + "3J" + esc + "1;1H")197 }