-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.go
70 lines (59 loc) · 1.29 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package bento
import "time"
type (
QuitMsg struct{}
BatchMsg []Cmd
WindowSizeMsg Size
// FocusMsg represents a terminal focus message.
// This occurs when the terminal gains focus.
FocusMsg struct{}
// BlurMsg represents a terminal blur message.
// This occurs when the terminal loses focus.
BlurMsg struct{}
)
type (
sequenceMsg []Cmd
)
// Quit is a special command that tells the Bento app to exit.
func Quit() Msg {
return QuitMsg{}
}
// 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)
}
}
// Batch performs a bunch of commands concurrently with no ordering guarantees
// about the results. Use a Batch to return several commands.
func Batch(cmds ...Cmd) Cmd {
validCmds := make([]Cmd, 0, len(cmds))
for _, c := range cmds {
if c == nil {
continue
}
validCmds = append(validCmds, c)
}
switch len(validCmds) {
case 0:
return nil
case 1:
return validCmds[0]
default:
return func() Msg {
return BatchMsg(validCmds)
}
}
}
func Tick(d time.Duration, fn func(time.Time) Msg) Cmd {
t := time.NewTimer(d)
return func() Msg {
ts := <-t.C
t.Stop()
for len(t.C) > 0 {
<-t.C
}
return fn(ts)
}
}