diff --git a/examples/fullscreen/main.go b/examples/fullscreen/main.go index 870be2f..1d53cf3 100644 --- a/examples/fullscreen/main.go +++ b/examples/fullscreen/main.go @@ -17,7 +17,7 @@ type tickMsg struct{} func main() { tea.Fullscreen() defer tea.ExitFullscreen() - err := tea.NewProgram(initialize, update, view, []tea.Sub{tick}).Start() + err := tea.NewProgram(initialize, update, view, subscriptions).Start() if err != nil { log.Fatal(err) } @@ -53,12 +53,16 @@ func update(message tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { return m, nil } +func subscriptions(_ tea.Model) tea.Subs { + return tea.Subs{ + "tick": func(_ tea.Model) tea.Msg { + time.Sleep(time.Second) + return tickMsg{} + }, + } +} + func view(mdl tea.Model) string { m, _ := mdl.(model) return fmt.Sprintf("\n\n Hi. This program will exit in %d seconds...", m) } - -func tick(_ tea.Model) tea.Msg { - time.Sleep(time.Second) - return tickMsg{} -} diff --git a/examples/go.mod b/examples/go.mod index 754cb23..d5ebaae 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -5,7 +5,7 @@ go 1.13 replace github.com/charmbracelet/tea => ../ require ( - github.com/charmbracelet/tea v0.0.0-20200120190704-1e3967608725 + github.com/charmbracelet/tea v0.0.0-20200125213400-f64f86f18301 github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 ) diff --git a/examples/go.sum b/examples/go.sum index adb769c..987a27e 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -1,9 +1,8 @@ -github.com/charmbracelet/tea v0.0.0-20200118154546-df52853f9d94/go.mod h1:lijy1lXOKNwMjBu/jTT/DvR8yE9PhtX2olGFsCz9/Vk= -github.com/charmbracelet/tea v0.0.0-20200120185458-524cf2cffd81 h1:qwRuBEfzt4P7s4yOAanWmy/JoJNDszFnAPIiTMGka08= -github.com/charmbracelet/tea v0.0.0-20200120185458-524cf2cffd81/go.mod h1:UsFFdg04MNbcYi1r2FBtdDEFY07bObaYDKHhE1xZUaQ= +github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 h1:YQvJgppGVexnzIJ+KJlK9lBYA3+zXfdqZO/5Ngedtb0= github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9/go.mod h1:z8JWtuxM0oA+dZfi7BkgBW2YGbyOTbWAixFs46W3SK4= github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA= github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs= github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0= github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ= +golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI= golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/examples/input/main.go b/examples/input/main.go index 9296ba6..c4e0cd8 100644 --- a/examples/input/main.go +++ b/examples/input/main.go @@ -25,17 +25,7 @@ func main() { initialize, update, view, - []tea.Sub{ - // We just hand off the subscription to the input component, giving - // it the model it expects. - func(model tea.Model) tea.Msg { - m, ok := model.(Model) - if !ok { - return tea.NewErrMsg("could not perform assertion on model") - } - return input.Blink(m.Input) - }, - }, + subscriptions, ) if err := p.Start(); err != nil { @@ -80,6 +70,17 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) { return m, cmd } +func subscriptions(model tea.Model) tea.Subs { + return tea.Subs{ + // We just hand off the subscription to the input component, giving + // it the model it expects. + "input": func(model tea.Model) tea.Msg { + m, _ := model.(Model) + return input.Blink(m.Input) + }, + } +} + func view(model tea.Model) string { m, ok := model.(Model) if !ok { diff --git a/examples/simple/main.go b/examples/simple/main.go index 9baeb5e..56e111f 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -21,7 +21,7 @@ type TickMsg struct{} func main() { // Initialize our program - p := tea.NewProgram(initialize, update, view, []tea.Sub{tick}) + p := tea.NewProgram(initialize, update, view, subscriptions) if err := p.Start(); err != nil { log.Fatal(err) } @@ -57,7 +57,11 @@ func view(model tea.Model) string { // This is a subscription which we setup in NewProgram(). It waits for one // second, sends a tick, and then restarts. -func tick(_ tea.Model) tea.Msg { - time.Sleep(time.Second) - return TickMsg{} +func subscriptions(_ tea.Model) tea.Subs { + return tea.Subs{ + "tick": func(_ tea.Model) tea.Msg { + time.Sleep(time.Second) + return TickMsg{} + }, + } } diff --git a/examples/views/main.go b/examples/views/main.go index af968cf..6a3281b 100644 --- a/examples/views/main.go +++ b/examples/views/main.go @@ -31,7 +31,7 @@ func main() { initialize, update, view, - []tea.Sub{tick, frame}, + subscriptions, ) if err := p.Start(); err != nil { fmt.Println("could not start program:", err) @@ -46,6 +46,18 @@ func initialize() (tea.Model, tea.Cmd) { // SUBSCRIPTIONS +func subscriptions(model tea.Model) tea.Subs { + m, _ := model.(Model) + if !m.Chosen || m.Loaded { + return tea.Subs{ + "tick": tick, + } + } + return tea.Subs{ + "frame": frame, + } +} + func tick(model tea.Model) tea.Msg { time.Sleep(time.Second) return tickMsg{} diff --git a/go.mod b/go.mod index d192701..bd05df9 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/charmbracelet/tea go 1.13 require ( + github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 // indirect github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 // indirect ) diff --git a/go.sum b/go.sum index 5d1a519..f729c2d 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA= +github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs= github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0= github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ= golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI=