forked from antonmedv/countdown
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
235 lines (184 loc) · 4.32 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"flag"
"fmt"
"os"
"strings"
"time"
"log"
"math/rand"
"github.com/nsf/termbox-go"
"gopkg.in/yaml.v2"
)
const (
usage = `
countdowndsm <pathtoconfig>
Usage
countdowndsm /path/to/config.yml
Flags
`
tick = time.Second
)
var (
timer *time.Timer
ticker *time.Ticker
queues chan termbox.Event
w, h int
inputStartTime time.Time
actTime string
actName string
)
type yamlData struct {
Acts []map[string]interface{} `yaml:'acts'`
Persons []string `yaml:'persons'`
Random bool `yaml:'random'`
}
func ReadConfig() yamlData {
var config yamlData
flag.Parse()
args := flag.Args()
if len(args) != 1 {
stderr(usage)
flag.PrintDefaults()
os.Exit(2)
}
// Open YAML file
file, err := os.Open(args[0])
if err != nil {
log.Println(err.Error())
}
defer file.Close()
// Decode YAML file to struct
if file != nil {
decoder := yaml.NewDecoder(file)
if err := decoder.Decode(&config); err != nil {
log.Println(err.Error())
}
}
return config
}
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
}
func main() {
config := ReadConfig()
persons := config.Persons
// Mix persons
if config.Random == true {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(persons), func(i, j int) { persons[i], persons[j] = persons[j], persons[i] })
}
for _, person := range persons {
for actIndex, act := range config.Acts {
actTime, _ := act["time"].(string)
actName, _ := act["name"].(string)
timeLeft, err := parseTime(actTime)
if err != nil {
timeLeft, err = time.ParseDuration(actTime)
if err != nil {
stderr("error: Time: invalid duration or time: acts[%v]\n", actIndex)
os.Exit(2)
}
}
// Clean terminal
err = termbox.Init()
if err != nil {
panic(err)
}
queues = make(chan termbox.Event)
go func() {
for {
queues <- termbox.PollEvent()
}
}()
countdown(timeLeft, actName, person)
}
}
}
func countdown(totalDuration time.Duration, actTitle string, personNote string) {
timeLeft := totalDuration
title := actTitle
note := personNote
var (
exitCode int
)
w, h = termbox.Size()
start(timeLeft)
draw(title, timeLeft, note, w, h)
loop:
for {
select {
case ev := <-queues:
// Ctrl+C/Esc
if ev.Key == termbox.KeyEsc || ev.Key == termbox.KeyCtrlC {
exitCode = 1
break loop
}
// Space/Enter
if ev.Key == termbox.KeySpace || ev.Key == termbox.KeyEnter {
break loop
}
case <-ticker.C:
timeLeft -= tick
draw(title, timeLeft, note, w, h)
case <-timer.C:
break loop
}
}
termbox.Close()
if exitCode != 0 {
os.Exit(exitCode)
}
return
}
func start(d time.Duration) {
timer = time.NewTimer(d)
ticker = time.NewTicker(tick)
}
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 draw(t string, d time.Duration, n string, w int, h int) {
clear()
str := format(d)
timerText := toText(str)
titleStatus := toTextSmall(strings.ToLower(t))
personNote := toTextSmall(strings.ToLower(n))
xTitle, yTitle, xTimer, yTimer, xNote, yNote := w/2-titleStatus.width()/2, h/2-timerText.height()/2-2-titleStatus.height(), w/2-timerText.width()/2, h/2-timerText.height()/2, w/2-personNote.width()/2, h/2+timerText.height()/2+2
for _, symbolTitle := range titleStatus {
echo_symbol(symbolTitle, xTitle, yTitle)
xTitle += symbolTitle.width()
}
for _, symbolTimer := range timerText {
echo_symbol(symbolTimer, xTimer, yTimer)
xTimer += symbolTimer.width()
}
for _, symbolNote := range personNote {
echo_symbol(symbolNote, xNote, yNote)
xNote += symbolNote.width()
}
flush()
}