Skip to content

Commit

Permalink
chore: reorganize command helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Oct 4, 2022
1 parent e4ca150 commit 8c295a2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 42 deletions.
42 changes: 39 additions & 3 deletions commands.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
39 changes: 0 additions & 39 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,34 +117,6 @@ type Program struct {
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.
func Quit() Msg {
return quitMsg{}
Expand Down Expand Up @@ -575,14 +547,3 @@ func (p *Program) Printf(template string, args ...interface{}) {
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)
}
}

0 comments on commit 8c295a2

Please sign in to comment.