Skip to content
Myr

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

8 files+49 added−26 removed

modified:examples/fullscreen/main.go+10 added-6 removed
15 unmodified lines
1617func 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 lines
5253 return m, nil54}5556func 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}6465func 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}6061func 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 lines
24 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 )3031 if err := p.Start(); err != nil {32 log.Fatal(err)
36 unmodified lines
69 m.Input, cmd = input.Update(msg, m.Input)70 return m, cmd71}7273func 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}8384func 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 lines
modified:examples/simple/main.go+8 added-4 removed
19 unmodified lines
20type TickMsg struct{}2122func 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 lines
56}5758// This is a subscription which we setup in NewProgram(). It waits for one59// second, sends a tick, and then restarts.60func subscriptions(_ tea.Model) tea.Subs {61 return tea.Subs{60func 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 lines
30 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 lines
45}4647// SUBSCRIPTIONS4849func 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}6061func tick(model tea.Model) tea.Msg {62 time.Sleep(time.Second)63 return tickMsg{}64}
164 unmodified lines
modified:examples/go.mod+1 added-1 removed
3 unmodified lines
45replace github.com/charmbracelet/tea => ../67require (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
1github.com/charmbracelet/tea v0.0.0-20200118154546-df52853f9d94/go.mod h1:lijy1lXOKNwMjBu/jTT/DvR8yE9PhtX2olGFsCz9/Vk=2github.com/charmbracelet/tea v0.0.0-20200120185458-524cf2cffd81 h1:qwRuBEfzt4P7s4yOAanWmy/JoJNDszFnAPIiTMGka08=3github.com/charmbracelet/tea v0.0.0-20200120185458-524cf2cffd81/go.mod h1:UsFFdg04MNbcYi1r2FBtdDEFY07bObaYDKHhE1xZUaQ=1github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 h1:YQvJgppGVexnzIJ+KJlK9lBYA3+zXfdqZO/5Ngedtb0=2github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9/go.mod h1:z8JWtuxM0oA+dZfi7BkgBW2YGbyOTbWAixFs46W3SK4=3github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=4github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=5github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=6github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=7golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI=8golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
modified:go.mod+1 added
1 unmodified line
23go 1.1345require (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
1github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=2github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=3github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=4github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=5golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI=6golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=