chore: reorganize command helpers

This commit is contained in:
Christian Muehlhaeuser 2022-10-04 05:24:07 +02:00
parent e4ca150210
commit 8d6bdcf2fd
2 changed files with 39 additions and 42 deletions

View File

@ -1,12 +1,48 @@
package tea package tea
// Convenience commands. Not part of the Bubble Tea core, but potentially
// handy.
import ( import (
"time" "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 // 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 // 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. // could use this. It's also handy for having different things tick in sync.

39
tea.go
View File

@ -117,34 +117,6 @@ type Program struct {
windowsStdin *os.File //nolint:golint,structcheck,unused windowsStdin *os.File //nolint:golint,structcheck,unused
} }
// 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
// Quit is a special command that tells the Bubble Tea program to exit. // Quit is a special command that tells the Bubble Tea program to exit.
func Quit() Msg { func Quit() Msg {
return quitMsg{} return quitMsg{}
@ -575,14 +547,3 @@ 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)
}
}