mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
chore: reorganize command helpers
This commit is contained in:
42
commands.go
42
commands.go
@@ -1,12 +1,48 @@
|
||||
package tea
|
||||
|
||||
// Convenience commands. Not part of the Bubble Tea core, but potentially
|
||||
// handy.
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Batch performs a bunch of commands concurrently with no ordering guarantees
|
||||
// about the results. Use a Batch to return several commands.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func (m model) Init() Cmd {
|
||||
// return tea.Batch(someCommand, someOtherCommand)
|
||||
// }
|
||||
func Batch(cmds ...Cmd) Cmd {
|
||||
var validCmds []Cmd
|
||||
for _, c := range cmds {
|
||||
if c == nil {
|
||||
continue
|
||||
}
|
||||
validCmds = append(validCmds, c)
|
||||
}
|
||||
if len(validCmds) == 0 {
|
||||
return nil
|
||||
}
|
||||
return func() Msg {
|
||||
return batchMsg(validCmds)
|
||||
}
|
||||
}
|
||||
|
||||
// batchMsg is the internal message used to perform a bunch of commands. You
|
||||
// can send a batchMsg with Batch.
|
||||
type batchMsg []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)
|
||||
}
|
||||
}
|
||||
|
||||
// sequenceMsg is used interally to run the the given commands in order.
|
||||
type sequenceMsg []Cmd
|
||||
|
||||
// Every is a command that ticks in sync with the system clock. So, if you
|
||||
// wanted to tick with the system clock every second, minute or hour you
|
||||
// could use this. It's also handy for having different things tick in sync.
|
||||
|
||||
Reference in New Issue
Block a user