Programs should take an init type/function as the first argument
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This is in line with the way Elm works. Also update examples.
authored by Christian Rocha · committed by Christian Rocha · parent 068c729 · Browse files · 7 files·+44 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+44 added−25 removed
- other:+44 added−25 removed
Provenance
- Commit
6a04895- Tree
ad6e694- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
068c729
7 files changed+44 added−25 removed
modified:examples/fullscreen/main.go+5 added-1 removed
15 unmodified lines16 17 func main() {18 tea.Fullscreen()19 defer tea.ExitFullscreen()20- err := tea.NewProgram(model(5), update, view, []tea.Sub{tick}).Start()20+ err := tea.NewProgram(initialize, update, view, []tea.Sub{tick}).Start()21 if err != nil {22 log.Fatal(err)23 }24 }25 26+func initialize() (tea.Model, tea.Cmd) {27+ return model(5), nil28+}29+30 func update(message tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {31 m, _ := mdl.(model)32 33 switch msg := message.(type) {31 unmodified linesdeleted:examples/input/go.mod-5 removed
1-module input-example2-3-go 1.134-5-require github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 // indirectdeleted:examples/input/go.sum-6 removed
1-github.com/charmbracelet/tea v0.0.0-20200118154546-df52853f9d94 h1:m2xhUqOw6OcefbPBR9Il0J0n0gB1663NoKU+vvkiLdU=2-github.com/charmbracelet/tea v0.0.0-20200118154546-df52853f9d94/go.mod h1:lijy1lXOKNwMjBu/jTT/DvR8yE9PhtX2olGFsCz9/Vk=3-github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 h1:YQvJgppGVexnzIJ+KJlK9lBYA3+zXfdqZO/5Ngedtb0=4-github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9/go.mod h1:z8JWtuxM0oA+dZfi7BkgBW2YGbyOTbWAixFs46W3SK4=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=modified:examples/input/main.go+8 added-4 removed
20 unmodified lines21 func main() {22 tea.UseSysLog("tea")23 24 p := tea.NewProgram(25- Model{26- Input: input.DefaultModel(),27- Error: nil,28- },25+ initialize,26 update,27 view,28 []tea.Sub{29 // We just hand off the subscription to the input component, giving12 unmodified lines42 log.Fatal(err)43 }44 }45 46+func initialize() (tea.Model, tea.Cmd) {47+ return Model{48+ Input: input.DefaultModel(),49+ Error: nil,50+ }, nil51+}52+53 func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {54 var cmd tea.Cmd55 m, ok := model.(Model)56 if !ok {39 unmodified linesmodified:examples/simple/main.go+5 added-1 removed
19 unmodified lines20 type TickMsg struct{}21 22 func main() {23 // Initialize our program24- p := tea.NewProgram(Model(5), update, view, []tea.Sub{tick})24+ p := tea.NewProgram(initialize, update, view, []tea.Sub{tick})25 if err := p.Start(); err != nil {26 log.Fatal(err)27 }28 }29 30+func initialize() (tea.Model, tea.Cmd) {31+ return Model(5), nil32+}33+34 // Update is called when messages are recived. The idea is that you inspect35 // the message and update the model (or send back a new one) accordingly. You36 // can also return a commmand, which is a function that peforms I/O and37 // returns a message.26 unmodified linesmodified:examples/views/main.go+7 added-1 removed
26 unmodified lines27 type frameMsg struct{}28 29 func main() {30 p := tea.NewProgram(31- Model{0, false, 10, 0, 0, false},31+ initialize,32 update,33 view,34 []tea.Sub{tick, frame},35 )1 unmodified line37 fmt.Println("could not start program:", err)38 }39 }40 41+// INIT42+43+func initialize() (tea.Model, tea.Cmd) {44+ return Model{0, false, 10, 0, 0, false}, nil45+}46+47 // SUBSCRIPTIONS48 49 func tick(model tea.Model) tea.Msg {50 time.Sleep(time.Second)166 unmodified linesmodified:tea.go+19 added-7 removed
20 unmodified lines21 22 // Sub is an event subscription. If it returns nil it's considered a no-op.23 type Sub func(Model) Msg24 25+// Init is the first function that will be called. It returns your initial26+// model and runs an optional command27+type Init func() (Model, Cmd)28+29 // Update is called when a message is received. It may update the model and/or30 // send a command.31 type Update func(Msg, Model) (Model, Cmd)32 1 unmodified line34 type View func(Model) string35 36 // Program is a terminal user interface37 type Program struct {34- model Model38+ init Init39 update Update40 view View41 subscriptions []Sub42 rw io.ReadWriter24 unmodified lines67 // Signals that the program should quit68 type quitMsg struct{}69 70 // NewProgram creates a new Program67-func NewProgram(model Model, update Update, view View, subs []Sub) *Program {71+func NewProgram(init Init, update Update, view View, subs []Sub) *Program {72 return &Program{69- model: model,73+ init: init,74 update: update,75 view: view,76 subscriptions: subs,77 }1 unmodified line79 80 // Start initializes the program81 func (p *Program) Start() error {82 var (79- model = p.model83+ model Model84 cmd Cmd85 cmds = make(chan Cmd)86 msgs = make(chan Msg)87 done = make(chan struct{})5 unmodified lines93 }94 95 p.rw = tty96 tty.SetRaw()97+ hideCursor()98 defer func() {99 showCursor()100 tty.Restore()101 }()102 103+ // Initialize program104+ model, _ = p.init()105+ if cmd != nil {106+ go func() {107+ cmds <- cmd108+ }()109+ }110+111 // Render initial view99- hideCursor()112 p.render(model)113 114 // Subscribe to user input. We could move this out of here and offer it115 // as a subscription, but it blocks nicely and seems to be a common enough10 unmodified lines126 if len(p.subscriptions) > 0 {127 for _, sub := range p.subscriptions {128 go func(s Sub) {129 for {118- msgs <- s(p.model)130+ msgs <- s(model)131 }132 }(sub)133 }134 }24 unmodified lines159 return nil160 }161 162 model, cmd = p.update(msg, model)163+ log.Println(model)164 cmds <- cmd // process command (if any)165 p.render(model)153- p.model = model166 }167 }168 }169 28 unmodified lines