Skip to content
Myr

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

7 files+44 added−25 removed

modified:examples/fullscreen/main.go+5 added-1 removed
15 unmodified lines
1617func 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}2526func initialize() (tea.Model, tea.Cmd) {27 return model(5), nil28}2930func update(message tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {31 m, _ := mdl.(model)3233 switch msg := message.(type) {
31 unmodified lines
deleted:examples/input/go.mod-5 removed
1module input-example23go 1.1345require github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 // indirect
deleted:examples/input/go.sum-6 removed
1github.com/charmbracelet/tea v0.0.0-20200118154546-df52853f9d94 h1:m2xhUqOw6OcefbPBR9Il0J0n0gB1663NoKU+vvkiLdU=2github.com/charmbracelet/tea v0.0.0-20200118154546-df52853f9d94/go.mod h1:lijy1lXOKNwMjBu/jTT/DvR8yE9PhtX2olGFsCz9/Vk=3github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 h1:YQvJgppGVexnzIJ+KJlK9lBYA3+zXfdqZO/5Ngedtb0=4github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9/go.mod h1:z8JWtuxM0oA+dZfi7BkgBW2YGbyOTbWAixFs46W3SK4=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=
modified:examples/input/main.go+8 added-4 removed
20 unmodified lines
21func main() {22 tea.UseSysLog("tea")2324 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, giving
12 unmodified lines
42 log.Fatal(err)43 }44}4546func initialize() (tea.Model, tea.Cmd) {47 return Model{48 Input: input.DefaultModel(),49 Error: nil,50 }, nil51}5253func 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 lines
modified:examples/simple/main.go+5 added-1 removed
19 unmodified lines
20type TickMsg struct{}2122func 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}2930func initialize() (tea.Model, tea.Cmd) {31 return Model(5), nil32}3334// 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 lines
modified:examples/views/main.go+7 added-1 removed
26 unmodified lines
27type frameMsg struct{}2829func 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 line
37 fmt.Println("could not start program:", err)38 }39}4041// INIT4243func initialize() (tea.Model, tea.Cmd) {44 return Model{0, false, 10, 0, 0, false}, nil45}4647// SUBSCRIPTIONS4849func tick(model tea.Model) tea.Msg {50 time.Sleep(time.Second)
166 unmodified lines
modified:tea.go+19 added-7 removed
20 unmodified lines
2122// Sub is an event subscription. If it returns nil it's considered a no-op.23type Sub func(Model) Msg2425// Init is the first function that will be called. It returns your initial26// model and runs an optional command27type Init func() (Model, Cmd)2829// Update is called when a message is received. It may update the model and/or30// send a command.31type Update func(Msg, Model) (Model, Cmd)32
1 unmodified line
34type View func(Model) string3536// Program is a terminal user interface37type Program struct {34 model Model38 init Init39 update Update40 view View41 subscriptions []Sub42 rw io.ReadWriter
24 unmodified lines
67// Signals that the program should quit68type quitMsg struct{}6970// NewProgram creates a new Program67func NewProgram(model Model, update Update, view View, subs []Sub) *Program {71func 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 line
7980// Start initializes the program81func (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 lines
93 }9495 p.rw = tty96 tty.SetRaw()97 hideCursor()98 defer func() {99 showCursor()100 tty.Restore()101 }()102103 // Initialize program104 model, _ = p.init()105 if cmd != nil {106 go func() {107 cmds <- cmd108 }()109 }110111 // Render initial view99 hideCursor()112 p.render(model)113114 // 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 enough
10 unmodified lines
126 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 lines
159 return nil160 }161162 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