mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
feat: add Sequence for running commands in order. Closes #413.
This deprecates Sequentially.
This commit is contained in:
@@ -119,6 +119,8 @@ func Tick(d time.Duration, fn func(time.Time) Msg) Cmd {
|
|||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// cmd := Sequentially(saveStateCmd, Quit)
|
// cmd := Sequentially(saveStateCmd, Quit)
|
||||||
|
//
|
||||||
|
// Deprecated: use Sequence instead.
|
||||||
func Sequentially(cmds ...Cmd) Cmd {
|
func Sequentially(cmds ...Cmd) Cmd {
|
||||||
return func() Msg {
|
return func() Msg {
|
||||||
for _, cmd := range cmds {
|
for _, cmd := range cmds {
|
||||||
|
|||||||
40
examples/sequence/main.go
Normal file
40
examples/sequence/main.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// A simple example illustrating how to run a series of commands in order.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
)
|
||||||
|
|
||||||
|
type model struct{}
|
||||||
|
|
||||||
|
func (m model) Init() tea.Cmd {
|
||||||
|
return tea.Sequence(
|
||||||
|
tea.Println("A"),
|
||||||
|
tea.Println("B"),
|
||||||
|
tea.Println("C"),
|
||||||
|
tea.Quit,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
|
switch msg.(type) {
|
||||||
|
case tea.KeyMsg:
|
||||||
|
return m, tea.Quit
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m model) View() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := tea.NewProgram(model{}).Start(); err != nil {
|
||||||
|
fmt.Println("Uh oh:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
22
tea.go
22
tea.go
@@ -532,6 +532,17 @@ func (p *Program) StartReturningModel() (Model, error) {
|
|||||||
case execMsg:
|
case execMsg:
|
||||||
// NB: this blocks.
|
// NB: this blocks.
|
||||||
p.exec(msg.cmd, msg.fn)
|
p.exec(msg.cmd, msg.fn)
|
||||||
|
|
||||||
|
case sequenceMsg:
|
||||||
|
go func() {
|
||||||
|
// Execute commands one at a time, in order.
|
||||||
|
for _, cmd := range msg {
|
||||||
|
select {
|
||||||
|
case p.msgs <- cmd():
|
||||||
|
case <-p.ctx.Done():
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process internal messages for the renderer.
|
// Process internal messages for the renderer.
|
||||||
@@ -736,3 +747,14 @@ func (p *Program) Printf(template string, args ...interface{}) {
|
|||||||
messageBody: fmt.Sprintf(template, args...),
|
messageBody: fmt.Sprintf(template, args...),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sequenceMsg is used interally to run the the given commands in order.
|
||||||
|
type sequenceMsg []Cmd
|
||||||
|
|
||||||
|
// Sequence runs the given commands one at a time, in order. Contrast this with
|
||||||
|
// Batch, which runs commands concurrently.
|
||||||
|
func Sequence(cmds ...Cmd) Cmd {
|
||||||
|
return func() Msg {
|
||||||
|
return sequenceMsg(cmds)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user