Skip to content
Myr

feat: add Sequence for running commands in order. Closes #413.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This deprecates Sequentially.

authored by Christian Rocha · committed by Christian Rocha · parent 9fcebd1 · Browse files · 3 files+64 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+64 added−0 removed

  • other:+64 added−0 removed

3 files+64 added−0 removed

added:examples/sequence/main.go+40 added
1package main23// A simple example illustrating how to run a series of commands in order.45import (6 "fmt"7 "os"89 tea "github.com/charmbracelet/bubbletea"10)1112type model struct{}1314func (m model) Init() tea.Cmd {15 return tea.Sequence(16 tea.Println("A"),17 tea.Println("B"),18 tea.Println("C"),19 tea.Quit,20 )21}2223func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {24 switch msg.(type) {25 case tea.KeyMsg:26 return m, tea.Quit27 }28 return m, nil29}3031func (m model) View() string {32 return ""33}3435func main() {36 if err := tea.NewProgram(model{}).Start(); err != nil {37 fmt.Println("Uh oh:", err)38 os.Exit(1)39 }40}
modified:commands.go+2 added
117 unmodified lines
118// return nil119// }120//121// cmd := Sequentially(saveStateCmd, Quit)122//123// Deprecated: use Sequence instead.124func Sequentially(cmds ...Cmd) Cmd {125 return func() Msg {126 for _, cmd := range cmds {127 if cmd == nil {
9 unmodified lines
modified:tea.go+22 added
530 unmodified lines
531532 case execMsg:533 // NB: this blocks.534 p.exec(msg.cmd, msg.fn)535536 case sequenceMsg:537 go func() {538 // Execute commands one at a time, in order.539 for _, cmd := range msg {540 select {541 case p.msgs <- cmd():542 case <-p.ctx.Done():543 }544 }545 }()546 }547548 // Process internal messages for the renderer.549 if r, ok := p.renderer.(*standardRenderer); ok {
196 unmodified lines
746 p.msgs <- printLineMessage{747 messageBody: fmt.Sprintf(template, args...),748 }749}750751// sequenceMsg is used interally to run the the given commands in order.752type sequenceMsg []Cmd753754// Sequence runs the given commands one at a time, in order. Contrast this with755// Batch, which runs commands concurrently.756func Sequence(cmds ...Cmd) Cmd {757 return func() Msg {758 return sequenceMsg(cmds)759 }760}