Skip to content
Myr

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 file

Take it

1package 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//12//	func (m model) Init() tea.Cmd {13//	    return tea.Batch(someCommand, someOtherCommand)14//	}15func Batch(cmds ...Cmd) Cmd {16	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 {37	var validCmds []Cmd38	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 {51			return T(validCmds)52		}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//87//	func (m model) Init() (Model, Cmd) {88//	    // Start ticking.89//	    return m, tickEvery()90//	}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//141//	func (m model) Init() (Model, Cmd) {142//	    // Start ticking.143//	    return m, doTick()144//	}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{}167168// 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{}175}