-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
94 lines (81 loc) · 1.55 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
package main
import (
"fmt"
"os"
"strconv"
"time"
termbox "github.com/nsf/termbox-go"
)
var (
slotIntervalTime = map[string]int{
"easy": 200,
"normal": 100,
"hard": 50,
}
)
func main() {
args, err := ParseArgs()
if err != nil {
Err(err)
os.Exit(1)
}
interval := slotIntervalTime[args.Level]
style := styles[args.Style]
slot := NewSlot(0, interval)
if err := termbox.Init(); err != nil {
panic(err)
}
defer termbox.Close()
termbox.SetInputMode(termbox.InputEsc)
termbox.Flush()
go clock(slot, style)
waitKeyInput(slot)
termbox.Close()
changeMode(slot, args.Args)
}
func clock(s *Slot, st DrawStyle) {
sleepMilSec := time.Duration(s.IntervalTime()) * time.Millisecond
for !s.IsFinished() {
s.Switch()
DrawSlot(s, st)
time.Sleep(sleepMilSec)
}
}
func waitKeyInput(s *Slot) {
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyCtrlC, termbox.KeyCtrlD:
return
case termbox.KeyEnter:
s.Select()
}
switch ev.Ch {
case 'q':
return
}
}
if s.IsFinished() {
return
}
}
}
func changeMode(s *Slot, files []string) {
slots := s.Slots()
for _, file := range files {
perm := fmt.Sprintf("0%d%d%d", slots[0], slots[1], slots[2])
t := fmt.Sprintf("chmod %s %s", perm, file)
fmt.Println(t)
mode, err := strconv.ParseUint(perm, 8, 32)
if err != nil {
panic(err)
}
if err := os.Chmod(file, os.FileMode(mode)); err != nil {
panic(err)
}
}
if slots[0] == slots[1] && slots[1] == slots[2] {
fmt.Println("BINGO🎉")
}
}