Programs should take an init type/function as the first argument

This is in line with the way Elm works. Also update examples.
This commit is contained in:
Christian Rocha
2020-01-18 22:18:19 -05:00
parent 068c7291fb
commit 6a0489592f
7 changed files with 44 additions and 25 deletions

View File

@@ -17,12 +17,16 @@ type tickMsg struct{}
func main() {
tea.Fullscreen()
defer tea.ExitFullscreen()
err := tea.NewProgram(model(5), update, view, []tea.Sub{tick}).Start()
err := tea.NewProgram(initialize, update, view, []tea.Sub{tick}).Start()
if err != nil {
log.Fatal(err)
}
}
func initialize() (tea.Model, tea.Cmd) {
return model(5), nil
}
func update(message tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
m, _ := mdl.(model)

View File

@@ -1,5 +0,0 @@
module input-example
go 1.13
require github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 // indirect

View File

@@ -1,6 +0,0 @@
github.com/charmbracelet/tea v0.0.0-20200118154546-df52853f9d94 h1:m2xhUqOw6OcefbPBR9Il0J0n0gB1663NoKU+vvkiLdU=
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/teaparty v0.0.0-20200118155738-c83a0bee59b9/go.mod h1:z8JWtuxM0oA+dZfi7BkgBW2YGbyOTbWAixFs46W3SK4=
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=

View File

@@ -22,10 +22,7 @@ func main() {
tea.UseSysLog("tea")
p := tea.NewProgram(
Model{
Input: input.DefaultModel(),
Error: nil,
},
initialize,
update,
view,
[]tea.Sub{
@@ -46,6 +43,13 @@ func main() {
}
}
func initialize() (tea.Model, tea.Cmd) {
return Model{
Input: input.DefaultModel(),
Error: nil,
}, nil
}
func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
m, ok := model.(Model)

View File

@@ -21,12 +21,16 @@ type TickMsg struct{}
func main() {
// Initialize our program
p := tea.NewProgram(Model(5), update, view, []tea.Sub{tick})
p := tea.NewProgram(initialize, update, view, []tea.Sub{tick})
if err := p.Start(); err != nil {
log.Fatal(err)
}
}
func initialize() (tea.Model, tea.Cmd) {
return Model(5), nil
}
// Update is called when messages are recived. The idea is that you inspect
// the message and update the model (or send back a new one) accordingly. You
// can also return a commmand, which is a function that peforms I/O and

View File

@@ -28,7 +28,7 @@ type frameMsg struct{}
func main() {
p := tea.NewProgram(
Model{0, false, 10, 0, 0, false},
initialize,
update,
view,
[]tea.Sub{tick, frame},
@@ -38,6 +38,12 @@ func main() {
}
}
// INIT
func initialize() (tea.Model, tea.Cmd) {
return Model{0, false, 10, 0, 0, false}, nil
}
// SUBSCRIPTIONS
func tick(model tea.Model) tea.Msg {