-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkama.go
121 lines (100 loc) · 2.63 KB
/
kama.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
package main
import (
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/gen2brain/beeep"
"gopkg.in/ini.v1"
)
type Verb string
const (
Break Verb = "break"
LongBreak Verb = "longbreak"
Work Verb = "work"
)
const (
// how frequently the display should be drawn
timeTick = time.Second
)
var (
startTime = time.Now()
// CLI flags
totalTime = flag.Duration("t", time.Duration(0), "timer length")
quiet = flag.Bool("q", false, "disable notification")
message = flag.String("m", "timer finished!", "notification message")
size = flag.Int("s", 15, "width of the progress indicator")
// flags to force verb/mode
isBreak = flag.Bool("b", false, "forces a break timer")
isLongBreak = flag.Bool("l", false, "forces a long break timer")
isWork = flag.Bool("w", true, "forces a work timer")
breakSeconds = 5 * time.Minute
longBreakSeconds = 15 * time.Minute
workSeconds = 25 * time.Minute
)
func main() {
flag.Parse()
userHome, _ := os.UserHomeDir()
cfg, err := ini.Load(userHome + "/.kamarc")
// retrieve time duration configs
if err == nil {
breakSeconds = time.Duration(cfg.Section("").Key("break_time").MustInt()) * time.Second
longBreakSeconds = time.Duration(cfg.Section("").Key("long_break_time").MustInt()) * time.Second
workSeconds = time.Duration(cfg.Section("").Key("work_time").MustInt()) * time.Second
*size = cfg.Section("").Key("width").MustInt(15)
}
verb := Work
switch {
case *isBreak:
verb = Break
case *isLongBreak:
verb = LongBreak
case *isWork:
verb = Work
}
if *totalTime == time.Duration(0) {
switch verb {
case Break:
*totalTime = time.Duration(breakSeconds)
case LongBreak:
*totalTime = time.Duration(longBreakSeconds)
case Work:
*totalTime = time.Duration(workSeconds)
}
}
fmt.Printf("%s\n", strings.ToUpper(string(verb)))
printDisplay(*totalTime)
ticker := time.NewTicker(timeTick)
done := make(chan bool)
go func() {
for {
select {
case <-done:
return
case <-ticker.C:
printDisplay(*totalTime)
}
}
}()
time.Sleep(*totalTime)
ticker.Stop()
done <- true
fmt.Println()
if !*quiet {
// TODO add image icon?
beeep.Notify("kama", *message, "")
hour, minute, _ := time.Now().Clock()
fmt.Printf("finished at %d:%d\n", hour%12, minute)
}
}
func printDisplay(totalTime time.Duration) {
// TODO timer state should be a struct
currentTime := time.Since(startTime).Truncate(time.Second)
percent := int(100 * currentTime / totalTime)
display := ""
display += ProgressBar(percent, *size)
display += fmt.Sprintf(" %d%% : ", percent)
display += TimeDisplay(currentTime, totalTime)
fmt.Printf("%s\r", display)
}