-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.go
347 lines (306 loc) · 8.42 KB
/
counter.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import ("os"
"fmt"
"flag"
"time"
"unsafe"
//"strconv"
"strings"
"syscall"
"os/exec"
"os/signal"
)
type winsize struct {
rows uint16
cols uint16
xpixels uint16
ypixels uint16
}
/** what is the size of the terminal
@param pointer to output stream
@return width, height
*/
func get_term_size(fd uintptr) (int, int) {
var sz winsize
_, _, _ = syscall.Syscall(syscall.SYS_IOCTL,
fd, uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&sz)))
return int(sz.cols), int(sz.rows)
}
/** ESC Commands */
const (
_ESC_SAVE_SCREEN = "?47h"
_ESC_RESTORE_SCREEN = "?47l"
_ESC_SAVE_CURSOR = "s"
_ESC_RESTORE_CURSOR = "u"
_ESC_BOLD_ON = "1m"
_ESC_BOLD_OFF = "0m"
_ESC_CURSOR_ON = "?25h"
_ESC_CURSOR_OFF = "?25l"
_ESC_CLEAR_SCREEN = "2J"
_ESC_CLEAR_LINE = "2K"
)
/** some common symbols */
const (
RuneSterling = '£'
RuneDArrow = '↓'
RuneLArrow = '←'
RuneRArrow = '→'
RuneUArrow = '↑'
RuneBullet = '·'
RuneBoard = '░'
RuneCkBoard = '▒'
RuneDegree = '°'
RuneDiamond = '◆'
RuneGEqual = '≥'
RunePi = 'π'
RuneHLine = '─'
RuneLantern = '§'
RunePlus = '┼'
RuneLEqual = '≤'
RuneLLCorner = '└'
RuneLRCorner = '┘'
RuneNEqual = '≠'
RunePlMinus = '±'
RuneS1 = '⎺'
RuneS3 = '⎻'
RuneS7 = '⎼'
RuneS9 = '⎽'
RuneBlock = '█'
RuneTTee = '┬'
RuneRTee = '┤'
RuneLTee = '├'
RuneBTee = '┴'
RuneULCorner = '┌'
RuneURCorner = '┐'
RuneVLine = '│' //'│'
RuneUVLine = '╷'
RuneDVLine = '╵'
)
/** numbers */
var letters = [][][]rune {
{/*0*/
{RuneULCorner, RuneHLine, RuneURCorner},
{RuneVLine, ' ', RuneVLine},
{RuneVLine, ' ', RuneVLine},
{RuneVLine, ' ', RuneVLine},
{RuneLLCorner, RuneHLine, RuneLRCorner} },
{/*1*/
{' ', RuneUVLine, ' '},
{' ', RuneVLine, ' '},
{' ', RuneVLine, ' '},
{' ', RuneVLine, ' '},
{' ', RuneDVLine, ' '} },
{/*2*/
{RuneHLine, RuneHLine, RuneURCorner},
{' ', ' ', RuneVLine},
{RuneULCorner, RuneHLine, RuneLRCorner},
{RuneVLine, ' ', ' '},
{RuneLLCorner, RuneHLine, RuneHLine} },
{/*3*/
{RuneHLine, RuneHLine, RuneURCorner},
{' ', ' ', RuneVLine},
{RuneHLine, RuneHLine, RuneRTee},
{' ', ' ', RuneVLine},
{RuneHLine, RuneHLine, RuneLRCorner} },
{/*4*/
{RuneTTee, ' ', RuneTTee},
{RuneVLine, ' ', RuneVLine},
{RuneLLCorner, RuneHLine, RuneRTee},
{' ', ' ', RuneVLine},
{' ', ' ', RuneBTee} },
{/*5*/
{RuneULCorner, RuneHLine, RuneHLine},
{RuneVLine, ' ', ' '},
{RuneLLCorner, RuneHLine, RuneURCorner},
{' ', ' ', RuneVLine},
{RuneHLine, RuneHLine, RuneLRCorner} },
{/*6*/
{RuneULCorner, RuneHLine, RuneHLine},
{RuneVLine, ' ', ' '},
{RuneLTee, RuneHLine, RuneURCorner},
{RuneVLine, ' ', RuneVLine},
{RuneLLCorner, RuneHLine, RuneLRCorner} },
{/*7*/
{RuneHLine, RuneHLine, RuneURCorner},
{' ', ' ', RuneVLine},
{' ', ' ', RuneVLine},
{' ', ' ', RuneVLine},
{' ', ' ', RuneBTee} },
{/*8*/
{RuneULCorner, RuneHLine, RuneURCorner},
{RuneVLine, ' ', RuneVLine},
{RuneLTee, RuneHLine, RuneRTee},
{RuneVLine, ' ', RuneVLine},
{RuneLLCorner, RuneHLine, RuneLRCorner} },
{/*9*/
{RuneULCorner, RuneHLine, RuneURCorner},
{RuneVLine, ' ', RuneVLine},
{RuneLLCorner, RuneHLine, RuneRTee},
{' ', ' ', RuneVLine},
{' ', ' ', RuneBTee} },
{/*blank*/
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '} } }
/**
print out a large number at a location
@param num 0-11, 11 is a blank
@param y row to print on
@param x col to print on
*/
func PrintNumber(color, num, y, x int) {
if (0<=num && 0<=y && 0<=x) {
for i:= range letters[num] {
for j:= range letters[num][i] {
text := ColorText(string(letters[num][i][j]), color)
PrintStrOnErrAt(text, y+i, x+j+1)
}
}
}
}
/**
print out an esc control
@param esc control code to print out
*/
func PrintCtrOnErr(esc string) {
fmt.Fprintf(os.Stderr, "\033[%s", esc)
}
func PrintCtrOnErrAt(esc string, y, x int) {
fmt.Fprintf(os.Stderr, "\033[%d;%dH\033[%s", y, x, esc)
}
/**
print a string on the error console at a specific location on the screen
@param msg text to print out
@param y row to print on
@param x col to print one
*/
func PrintStrOnErrAt(msg string, y, x int) {
fmt.Fprintf(os.Stderr, "\033[%d;%dH%s", y, x, msg)
}
/** print a string in a color */
func ColorText(text string, color int) string {
encoded := fmt.Sprintf("\033[0;%dm%s\033[0m", color, text)
return encoded
}
func Red(text string) string {
return ColorText(text, 31)
}
func Green(text string) string {
return ColorText(text, 32)
}
func Blue(text string) string {
return ColorText(text, 34)
}
/** Save the screen setup at the start of the app */
func ScrSave() {
PrintCtrOnErr(_ESC_SAVE_SCREEN)
PrintCtrOnErr(_ESC_SAVE_CURSOR)
PrintCtrOnErr(_ESC_CURSOR_OFF)
PrintCtrOnErr(_ESC_CLEAR_SCREEN)
}
/** Restore the screen setup from SrcSave() */
func ScrRestore() {
PrintCtrOnErr(_ESC_CURSOR_ON)
PrintCtrOnErr(_ESC_RESTORE_CURSOR)
PrintCtrOnErr(_ESC_RESTORE_SCREEN)
}
/**
Print the time
@param direction word to show direction
@param i number to print out
*/
func PrintTime(direction string, i int) {
var x, y = get_term_size(uintptr(syscall.Stdin))
PrintStrOnErrAt("counting " + direction, y/5, x/5)
//PrintCtrOnErrAt(ESC_CLEAR_LINE, y/2-2, x/2-6)
//PrintCtrOnErrAt(ESC_CLEAR_LINE, y/2-2, x/2-2)
//PrintCtrOnErrAt(ESC_CLEAR_LINE, y/2-2, x/2+2)
//PrintCtrOnErrAt(ESC_CLEAR_LINE, y/2-2, x/2+6)
PrintCtrOnErr(_ESC_BOLD_ON)
fmt.Fprintf(os.Stderr, "\033[%d;%dH", (y/2), (x/2))
color := 37
if direction == "down" {
if i < 5 {
color = 31 //red
} else if i < 10 {
color = 33 //yellow
} else {
color = 37 //white
}
}
PrintNumber(color, (i/1000)%10, y/2-2, x/2-6)
PrintNumber(color, (i/100)%10, y/2-2, x/2-2)
PrintNumber(color, (i/10)%10, y/2-2, x/2+2)
PrintNumber(color, (i/1)%10, y/2-2, x/2+6)
PrintCtrOnErr(_ESC_BOLD_OFF)
PrintStrOnErrAt("", (y), (x-1))
}
/**
get the current time as a timestamp
@return timestamp
*/
func makeTimestamp() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
/** sleep for a second */
func WaitSecond() {time.Sleep(1*1000 * time.Millisecond)}
func DoCommand(command string) {
s := strings.Split(command, " ")
cmd := exec.Command(s[0], s[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {fmt.Printf("Error: %s\n", err)}
}
/**
register an event for ctrl-c
*/
func panic_on_interrupt(c chan os.Signal) {
sig := <-c
if sig != nil {
ScrRestore()
os.Exit(1)
}
}
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go panic_on_interrupt(c)
var up = flag.Int("up", -1, "Count up in seconds")
var down = flag.Int("down", -1, "count down in seconds")
var done = flag.String("done", "Done", "text to output after timers.")
var cmd = flag.String("command", "", "command to execute after timers")
var help = flag.Bool("help", false, "Print this help message")
flag.Parse()
if 0==flag.NFlag() || *help {
flag.PrintDefaults()
}
ScrSave()
if 0<*up {
var start = makeTimestamp();
for i:=0; i<*up; i++ {
var now = makeTimestamp();
PrintTime("up", int(now-start)/1000)
WaitSecond()
}
}
if 0<*down {
var start = makeTimestamp();
for i:=*down; 0<=i; i-- {
var now = makeTimestamp();
PrintTime("down", *down - int(now-start)/1000)
WaitSecond()
}
}
//fmt.Fprintf(os.Stderr, "\r")
//post timer tasks
ScrRestore()
if *done!="" {
fmt.Printf("%s\n", *done)
}
if *cmd!="" {
DoCommand(*cmd)
}
}