Update examples to use new subscription model
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 444c36b · Browse files · 8 files·+49 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+49 added−26 removed
- other:+49 added−26 removed
Provenance
- Commit
651b881- Tree
ad21d4b- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
444c36b
8 files changed+49 added−26 removed
modified:examples/fullscreen/main.go+10 added-6 removed
15 unmodified lines16 17 func main() {18 tea.Fullscreen()19 defer tea.ExitFullscreen()20- err := tea.NewProgram(initialize, update, view, []tea.Sub{tick}).Start()20+ err := tea.NewProgram(initialize, update, view, subscriptions).Start()21 if err != nil {22 log.Fatal(err)23 }24 }27 unmodified lines52 53 return m, nil54 }55 56+func subscriptions(_ tea.Model) tea.Subs {57+ return tea.Subs{58+ "tick": func(_ tea.Model) tea.Msg {59+ time.Sleep(time.Second)60+ return tickMsg{}61+ },62+ }63+}64+65 func view(mdl tea.Model) string {66 m, _ := mdl.(model)67 return fmt.Sprintf("\n\n Hi. This program will exit in %d seconds...", m)68 }60-61-func tick(_ tea.Model) tea.Msg {62- time.Sleep(time.Second)63- return tickMsg{}64-}modified:examples/input/main.go+12 added-11 removed
23 unmodified lines24 p := tea.NewProgram(25 initialize,26 update,27 view,28- []tea.Sub{29- // We just hand off the subscription to the input component, giving30- // it the model it expects.31- func(model tea.Model) tea.Msg {32- m, ok := model.(Model)33- if !ok {34- return tea.NewErrMsg("could not perform assertion on model")35- }36- return input.Blink(m.Input)37- },38- },28+ subscriptions,29 )30 31 if err := p.Start(); err != nil {32 log.Fatal(err)36 unmodified lines69 m.Input, cmd = input.Update(msg, m.Input)70 return m, cmd71 }72 73+func subscriptions(model tea.Model) tea.Subs {74+ return tea.Subs{75+ // We just hand off the subscription to the input component, giving76+ // it the model it expects.77+ "input": func(model tea.Model) tea.Msg {78+ m, _ := model.(Model)79+ return input.Blink(m.Input)80+ },81+ }82+}83+84 func view(model tea.Model) string {85 m, ok := model.(Model)86 if !ok {87 return "Oh no: could not perform assertion on model."9 unmodified linesmodified:examples/simple/main.go+8 added-4 removed
19 unmodified lines20 type TickMsg struct{}21 22 func main() {23 // Initialize our program24- p := tea.NewProgram(initialize, update, view, []tea.Sub{tick})24+ p := tea.NewProgram(initialize, update, view, subscriptions)25 if err := p.Start(); err != nil {26 log.Fatal(err)27 }28 }27 unmodified lines56 }57 58 // This is a subscription which we setup in NewProgram(). It waits for one59 // second, sends a tick, and then restarts.60+func subscriptions(_ tea.Model) tea.Subs {61+ return tea.Subs{60-func tick(_ tea.Model) tea.Msg {61- time.Sleep(time.Second)62- return TickMsg{}62+ "tick": func(_ tea.Model) tea.Msg {63+ time.Sleep(time.Second)64+ return TickMsg{}65+ },66+ }67 }modified:examples/views/main.go+13 added-1 removed
29 unmodified lines30 p := tea.NewProgram(31 initialize,32 update,33 view,34- []tea.Sub{tick, frame},34+ subscriptions,35 )36 if err := p.Start(); err != nil {37 fmt.Println("could not start program:", err)38 }6 unmodified lines45 }46 47 // SUBSCRIPTIONS48 49+func subscriptions(model tea.Model) tea.Subs {50+ m, _ := model.(Model)51+ if !m.Chosen || m.Loaded {52+ return tea.Subs{53+ "tick": tick,54+ }55+ }56+ return tea.Subs{57+ "frame": frame,58+ }59+}60+61 func tick(model tea.Model) tea.Msg {62 time.Sleep(time.Second)63 return tickMsg{}64 }164 unmodified linesmodified:examples/go.mod+1 added-1 removed
3 unmodified lines4 5 replace github.com/charmbracelet/tea => ../6 7 require (8- github.com/charmbracelet/tea v0.0.0-20200120190704-1e39676087258+ github.com/charmbracelet/tea v0.0.0-20200125213400-f64f86f183019 github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b910 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf177611 )modified:examples/go.sum+2 added-3 removed
1-github.com/charmbracelet/tea v0.0.0-20200118154546-df52853f9d94/go.mod h1:lijy1lXOKNwMjBu/jTT/DvR8yE9PhtX2olGFsCz9/Vk=2-github.com/charmbracelet/tea v0.0.0-20200120185458-524cf2cffd81 h1:qwRuBEfzt4P7s4yOAanWmy/JoJNDszFnAPIiTMGka08=3-github.com/charmbracelet/tea v0.0.0-20200120185458-524cf2cffd81/go.mod h1:UsFFdg04MNbcYi1r2FBtdDEFY07bObaYDKHhE1xZUaQ=1+github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 h1:YQvJgppGVexnzIJ+KJlK9lBYA3+zXfdqZO/5Ngedtb0=2 github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9/go.mod h1:z8JWtuxM0oA+dZfi7BkgBW2YGbyOTbWAixFs46W3SK4=3 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=4 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=5 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=6 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=7+golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI=8 golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=modified:go.mod+1 added
1 unmodified line2 3 go 1.134 5 require (6+ github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 // indirect7 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e9428 golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 // indirect9 )modified:go.sum+2 added
1+github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=2+github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=3 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=4 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=5 golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI=6 golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=