-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (134 loc) · 4.07 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
package main
import (
"fmt"
sf "github.com/manyminds/gosfml"
"gopkg.in/alecthomas/kingpin.v1"
"os"
"time"
)
type mint int
type FullTime struct {
hours mint
minutes mint
seconds mint
}
func (i mint) String() string {
return fmt.Sprintf("%d", i)
}
func playSound(filepath string) {
data := MustAsset("resources/" + filepath)
buffer, err := sf.NewSoundBufferFromMemory(data)
if err != nil {
panic("soundbuffer not loaded")
}
sound := sf.NewSound(buffer)
sound.Play()
}
func sayTime(time string, lang string, female bool) {
var mf string = "m"
if female {
mf = "f"
}
playSound(lang + "/" + mf + "/" + time + ".wav")
}
func separateDuration(duration time.Duration) (int, int, int) {
hours := int(duration.Hours())
minutes := int(duration.Minutes()) - hours*60
seconds := int(duration.Seconds()) - hours*60*60 - minutes*60
return hours, minutes, seconds
}
func countDown(time FullTime) FullTime {
time.seconds -= 1
if time.seconds == -1 {
time.seconds = 59
time.minutes -= 1
if time.minutes == -1 {
time.minutes = 59
time.hours -= 1
}
}
return time
}
func tick(c chan FullTime, initialTime FullTime) {
var countingTime FullTime = initialTime
for {
c <- countingTime
if countingTime.hours == 0 && countingTime.minutes == 0 && countingTime.seconds == 0 {
break
} else {
countingTime = countDown(countingTime)
time.Sleep(time.Second)
}
}
}
func onTick(c chan FullTime, lang string, female bool) {
for {
var counter FullTime = <-c
fmt.Printf("%dh %sm %ss\n", counter.hours, counter.minutes, counter.seconds)
if counter.hours == 0 {
if counter.seconds == 0 {
// Final
if counter.minutes == 0 {
fmt.Println("FINISHED! Enter any key to exit.")
playSound("finale/reminder.wav")
break
} else if counter.minutes == 5 {
sayTime(counter.minutes.String()+"m", lang, female)
} else if counter.minutes == 3 {
sayTime(counter.minutes.String()+"m", lang, female)
} else if counter.minutes == 2 {
sayTime(counter.minutes.String()+"m", lang, female)
} else if counter.minutes == 1 {
sayTime(counter.minutes.String()+"m", lang, female)
}
}
if counter.minutes == 0 {
if counter.seconds == 30 {
sayTime(counter.seconds.String()+"s", lang, female)
} else if counter.seconds == 20 {
sayTime(counter.seconds.String()+"s", lang, female)
} else if counter.seconds == 10 {
sayTime(counter.seconds.String()+"s", lang, female)
} else if counter.seconds == 9 {
sayTime(counter.seconds.String()+"s", lang, female)
} else if counter.seconds == 8 {
sayTime(counter.seconds.String()+"s", lang, female)
} else if counter.seconds == 7 {
sayTime(counter.seconds.String()+"s", lang, female)
} else if counter.seconds == 6 {
sayTime(counter.seconds.String()+"s", lang, female)
} else if counter.seconds == 5 {
sayTime(counter.seconds.String()+"s", lang, female)
} else if counter.seconds == 4 {
sayTime(counter.seconds.String()+"s", lang, female)
} else if counter.seconds == 3 {
sayTime(counter.seconds.String()+"s", lang, female)
} else if counter.seconds == 2 {
sayTime(counter.seconds.String()+"s", lang, female)
} else if counter.seconds == 1 {
sayTime(counter.seconds.String()+"s", lang, female)
}
}
}
}
}
var (
app = kingpin.New("godown", "A talking countdown command-line application.")
duration = app.Arg("duration", "duration of the countdown (i.e. 1m30s)").Required().Duration()
female = app.Flag("female", "male or female voice").Bool()
language = app.Flag("lang", "english (en) or german (de) language").Default("en").Enum("en", "de")
)
func main() {
app.Version("1.0.1")
kingpin.MustParse(app.Parse(os.Args[1:]))
h, m, s := separateDuration(*duration)
var fulltime FullTime = FullTime{hours: mint(h), minutes: mint(m), seconds: mint(s)}
fmt.Println("Starting talking countdown with", *duration)
fmt.Println("Enter any key to abort")
fmt.Println()
var ticker chan FullTime = make(chan FullTime)
go tick(ticker, fulltime)
go onTick(ticker, *language, *female)
var input string
fmt.Scanln(&input)
}