forked from antonmedv/countdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
192 lines (160 loc) · 3.14 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/nsf/termbox-go"
)
const (
usage = `
countdown [-up] [-say] <duration>
Usage
countdown 25s
countdown 14:15
countdown 02:15PM
Flags
`
tick = time.Second
)
var (
timer *time.Timer
ticker *time.Ticker
queues chan termbox.Event
startDone bool
startX, startY int
)
func main() {
countUp := flag.Bool("up", false, "count up from zero")
sayTheTime := flag.Bool("say", false, "announce the time left")
flag.Parse()
args := flag.Args()
if len(args) != 1 {
stderr(usage)
flag.PrintDefaults()
os.Exit(2)
}
timeLeft, err := parseTime(args[0])
if err != nil {
timeLeft, err = time.ParseDuration(args[0])
if err != nil {
stderr("error: invalid duration or time: %v\n", args[0])
os.Exit(2)
}
}
err = termbox.Init()
if err != nil {
panic(err)
}
queues = make(chan termbox.Event)
go func() {
for {
queues <- termbox.PollEvent()
}
}()
countdown(timeLeft, *countUp, *sayTheTime)
}
func start(d time.Duration) {
timer = time.NewTimer(d)
ticker = time.NewTicker(tick)
}
func stop() {
timer.Stop()
ticker.Stop()
}
func countdown(timeLeft time.Duration, countUp bool, sayTheTime bool) {
var exitCode int
start(timeLeft)
if countUp {
timeLeft = 0
}
draw(timeLeft)
if sayTheTime {
go say(timeLeft)
}
loop:
for {
select {
case ev := <-queues:
if ev.Type == termbox.EventKey && (ev.Key == termbox.KeyEsc || ev.Key == termbox.KeyCtrlC) {
exitCode = 1
break loop
}
if ev.Ch == 'p' || ev.Ch == 'P' {
stop()
}
if ev.Ch == 'c' || ev.Ch == 'C' {
start(timeLeft)
}
case <-ticker.C:
if countUp {
timeLeft += tick
} else {
timeLeft -= tick
}
draw(timeLeft)
if sayTheTime {
go say(timeLeft)
}
case <-timer.C:
break loop
}
}
termbox.Close()
if exitCode != 0 {
os.Exit(exitCode)
}
}
func draw(d time.Duration) {
w, h := termbox.Size()
clear()
str := format(d)
text := toText(str)
if !startDone {
startDone = true
startX, startY = w/2-text.width()/2, h/2-text.height()/2
}
x, y := startX, startY
for _, s := range text {
echo(s, x, y)
x += s.width()
}
flush()
}
func format(d time.Duration) string {
d = d.Round(time.Second)
h := d / time.Hour
d -= h * time.Hour
m := d / time.Minute
d -= m * time.Minute
s := d / time.Second
if h < 1 {
return fmt.Sprintf("%02d:%02d", m, s)
}
return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
}
func say(d time.Duration) {
if d.Seconds() <= 10 {
cmd := exec.Command("say", fmt.Sprintf("%v", d.Seconds()))
_ = cmd.Run()
}
}
func parseTime(date string) (time.Duration, error) {
targetTime, err := time.Parse(time.Kitchen, strings.ToUpper(date))
if err != nil {
targetTime, err = time.Parse("15:04", date)
if err != nil {
return time.Duration(0), err
}
}
now := time.Now()
originTime := time.Date(0, time.January, 1, now.Hour(), now.Minute(), now.Second(), 0, time.UTC)
// The time of day has already passed, so target tomorrow.
if targetTime.Before(originTime) {
targetTime = targetTime.AddDate(0, 0, 1)
}
duration := targetTime.Sub(originTime)
return duration, err
}