forked from Mirrors/bubbletea
Update examples to use new subscription model
This commit is contained in:
parent
444c36b31f
commit
651b881a02
|
@ -17,7 +17,7 @@ type tickMsg struct{}
|
||||||
func main() {
|
func main() {
|
||||||
tea.Fullscreen()
|
tea.Fullscreen()
|
||||||
defer tea.ExitFullscreen()
|
defer tea.ExitFullscreen()
|
||||||
err := tea.NewProgram(initialize, update, view, []tea.Sub{tick}).Start()
|
err := tea.NewProgram(initialize, update, view, subscriptions).Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -53,12 +53,16 @@ func update(message tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
|
||||||
return m, nil
|
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 {
|
func view(mdl tea.Model) string {
|
||||||
m, _ := mdl.(model)
|
m, _ := mdl.(model)
|
||||||
return fmt.Sprintf("\n\n Hi. This program will exit in %d seconds...", m)
|
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{}
|
|
||||||
}
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ go 1.13
|
||||||
replace github.com/charmbracelet/tea => ../
|
replace github.com/charmbracelet/tea => ../
|
||||||
|
|
||||||
require (
|
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/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9
|
||||||
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776
|
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
github.com/charmbracelet/tea v0.0.0-20200118154546-df52853f9d94/go.mod h1:lijy1lXOKNwMjBu/jTT/DvR8yE9PhtX2olGFsCz9/Vk=
|
github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 h1:YQvJgppGVexnzIJ+KJlK9lBYA3+zXfdqZO/5Ngedtb0=
|
||||||
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/go.mod h1:z8JWtuxM0oA+dZfi7BkgBW2YGbyOTbWAixFs46W3SK4=
|
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 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=
|
||||||
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=
|
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 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=
|
||||||
github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=
|
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=
|
golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
|
|
@ -25,17 +25,7 @@ func main() {
|
||||||
initialize,
|
initialize,
|
||||||
update,
|
update,
|
||||||
view,
|
view,
|
||||||
[]tea.Sub{
|
subscriptions,
|
||||||
// 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)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if err := p.Start(); err != nil {
|
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
|
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 {
|
func view(model tea.Model) string {
|
||||||
m, ok := model.(Model)
|
m, ok := model.(Model)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
|
@ -21,7 +21,7 @@ type TickMsg struct{}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Initialize our program
|
// 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 {
|
if err := p.Start(); err != nil {
|
||||||
log.Fatal(err)
|
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
|
// This is a subscription which we setup in NewProgram(). It waits for one
|
||||||
// second, sends a tick, and then restarts.
|
// second, sends a tick, and then restarts.
|
||||||
func tick(_ tea.Model) tea.Msg {
|
func subscriptions(_ tea.Model) tea.Subs {
|
||||||
|
return tea.Subs{
|
||||||
|
"tick": func(_ tea.Model) tea.Msg {
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
return TickMsg{}
|
return TickMsg{}
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ func main() {
|
||||||
initialize,
|
initialize,
|
||||||
update,
|
update,
|
||||||
view,
|
view,
|
||||||
[]tea.Sub{tick, frame},
|
subscriptions,
|
||||||
)
|
)
|
||||||
if err := p.Start(); err != nil {
|
if err := p.Start(); err != nil {
|
||||||
fmt.Println("could not start program:", err)
|
fmt.Println("could not start program:", err)
|
||||||
|
@ -46,6 +46,18 @@ func initialize() (tea.Model, tea.Cmd) {
|
||||||
|
|
||||||
// SUBSCRIPTIONS
|
// 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 {
|
func tick(model tea.Model) tea.Msg {
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
return tickMsg{}
|
return tickMsg{}
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -3,6 +3,7 @@ module github.com/charmbracelet/tea
|
||||||
go 1.13
|
go 1.13
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 // indirect
|
||||||
github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942
|
github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942
|
||||||
golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 // indirect
|
golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 // indirect
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
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 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=
|
||||||
github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=
|
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 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI=
|
||||||
|
|
Loading…
Reference in New Issue