commands.go
This file
Path
commands.go
175 lines · 4.8 KiB · Go
Last changed by
docs: fix command and environment godoc examples (#1833)Hsnbefore adoption33f6a40
History of this fileTake it
The walk stopped after 200 commits: lines older than 3dfbcd9 point at it.
feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 1–11why, one step back1package tea23import (4 "time"5)67// Batch performs a bunch of commands concurrently with no ordering guarantees8// about the results. Use a Batch to return several commands.9//10// Example:11//docs: fix command and environment godoc examples (#1833)Hsnbefore adoption33f6a40
lines 12–14why, one step back12// func (m model) Init() tea.Cmd {13// return tea.Batch(someCommand, someOtherCommand)14// }feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
line 15why, one step back15func Batch(cmds ...Cmd) Cmd {fix: compact sequences like batches (#958)Joe Henkebefore adoptionc76509a
lines 16–36why, one step back16 return compactCmds[BatchMsg](cmds)17}1819// BatchMsg is a message used to perform a bunch of commands concurrently with20// no ordering guarantees. You can send a BatchMsg with Batch.21type BatchMsg []Cmd2223// Sequence runs the given commands one at a time, in order. Contrast this with24// Batch, which runs commands concurrently.25func Sequence(cmds ...Cmd) Cmd {26 return compactCmds[sequenceMsg](cmds)27}2829// sequenceMsg is used internally to run the given commands in order.30type sequenceMsg []Cmd3132// compactCmds ignores any nil commands in cmds, and returns the most direct33// command possible. That is, considering the non-nil commands, if there are34// none it returns nil, if there is exactly one it returns that command35// directly, else it returns the non-nil commands as type T.36func compactCmds[T ~[]Cmd](cmds []Cmd) Cmd {Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 37why, one step back37 var validCmds []Cmdfeat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 38–50why, one step back38 for _, c := range cmds {39 if c == nil {40 continue41 }42 validCmds = append(validCmds, c)43 }44 switch len(validCmds) {45 case 0:46 return nil47 case 1:48 return validCmds[0]49 default:50 return func() Msg {fix: compact sequences like batches (#958)Joe Henkebefore adoptionc76509a
line 51why, one step back51 return T(validCmds)feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 52–86why, one step back52 }53 }54}5556// Every is a command that ticks in sync with the system clock. So, if you57// wanted to tick with the system clock every second, minute or hour you58// could use this. It's also handy for having different things tick in sync.59//60// Because we're ticking with the system clock the tick will likely not run for61// the entire specified duration. For example, if we're ticking for one minute62// and the clock is at 12:34:20 then the next tick will happen at 12:35:00, 4063// seconds later.64//65// To produce the command, pass a duration and a function which returns66// a message containing the time at which the tick occurred.67//68// type TickMsg time.Time69//70// cmd := Every(time.Second, func(t time.Time) Msg {71// return TickMsg(t)72// })73//74// Beginners' note: Every sends a single message and won't automatically75// dispatch messages at an interval. To do that, you'll want to return another76// Every command after receiving your tick message. For example:77//78// type TickMsg time.Time79//80// // Send a message every second.81// func tickEvery() Cmd {82// return Every(time.Second, func(t time.Time) Msg {83// return TickMsg(t)84// })85// }86//Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 87why, one step back87// func (m model) Init() (Model, Cmd) {feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
line 88why, one step back88// // Start ticking.Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 89why, one step back89// return m, tickEvery()feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 90–140why, one step back90// }91//92// func (m model) Update(msg Msg) (Model, Cmd) {93// switch msg.(type) {94// case TickMsg:95// // Return your Every command again to loop.96// return m, tickEvery()97// }98// return m, nil99// }100//101// Every is analogous to Tick in the Elm Architecture.102func Every(duration time.Duration, fn func(time.Time) Msg) Cmd {103 n := time.Now()104 d := n.Truncate(duration).Add(duration).Sub(n)105 t := time.NewTimer(d)106 return func() Msg {107 ts := <-t.C108 t.Stop()109 for len(t.C) > 0 {110 <-t.C111 }112 return fn(ts)113 }114}115116// Tick produces a command at an interval independent of the system clock at117// the given duration. That is, the timer begins precisely when invoked,118// and runs for its entire duration.119//120// To produce the command, pass a duration and a function which returns121// a message containing the time at which the tick occurred.122//123// type TickMsg time.Time124//125// cmd := Tick(time.Second, func(t time.Time) Msg {126// return TickMsg(t)127// })128//129// Beginners' note: Tick sends a single message and won't automatically130// dispatch messages at an interval. To do that, you'll want to return another131// Tick command after receiving your tick message. For example:132//133// type TickMsg time.Time134//135// func doTick() Cmd {136// return Tick(time.Second, func(t time.Time) Msg {137// return TickMsg(t)138// })139// }140//Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 141why, one step back141// func (m model) Init() (Model, Cmd) {feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
line 142why, one step back142// // Start ticking.Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 143why, one step back143// return m, doTick()feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 144–167why, one step back144// }145//146// func (m model) Update(msg Msg) (Model, Cmd) {147// switch msg.(type) {148// case TickMsg:149// // Return your Tick command again to loop.150// return m, doTick()151// }152// return m, nil153// }154func Tick(d time.Duration, fn func(time.Time) Msg) Cmd {155 t := time.NewTimer(d)156 return func() Msg {157 ts := <-t.C158 t.Stop()159 for len(t.C) > 0 {160 <-t.C161 }162 return fn(ts)163 }164}165166type windowSizeMsg struct{}167Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
lines 168–174why, one step back168// RequestWindowSize is a command that queries the terminal for its current169// size. It delivers the results to Update via a [WindowSizeMsg]. Keep in mind170// that WindowSizeMsgs will automatically be delivered to Update when the171// [Program] starts and when the window dimensions change so in many cases you172// will not need to explicitly invoke this command.173func RequestWindowSize() Msg {174 return windowSizeMsg{}feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
line 175why, one step back175}