/
main.go
114 lines (98 loc) · 1.9 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
package main
import (
"bufio"
"os"
"os/signal"
"syscall"
"time"
"github.com/gosuri/uiprogress"
)
const (
pomodoroTime = 25 * time.Second
breakTime = 5 * time.Second
)
// TODO: progress bar per pomodoro
// TODO: msg per pomodoro
// TODO: remove break pomodoros
type Pomodoro struct {
bar *uiprogress.Bar
running bool
done chan struct{}
}
func NewPomodoro() *Pomodoro {
pomodoro := Pomodoro{
done: make(chan struct{}, 0),
}
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
for {
select {
case <-pomodoro.done:
case <-sig:
if pomodoro.running {
pomodoro.Stop()
} else {
os.Exit(0)
}
}
}
}()
uiprogress.Start()
return &pomodoro
}
func (p *Pomodoro) Run(duration int) bool {
tick := time.NewTicker(time.Second / 10)
p.running = true
p.bar = uiprogress.AddBar(10 * duration).AppendCompleted().PrependElapsed()
/*.PrependFunc(func(b *uiprogress.Bar) string {
return p.msg + ": "
})*/
defer func() {
tick.Stop()
p.running = false
p.done = make(chan struct{}, 0)
}()
for {
select {
case <-tick.C:
p.bar.Set(p.bar.Current() + 1)
if p.bar.Current() >= 10*duration {
p.running = false
close(p.done)
return true
}
case <-p.done:
return false
}
}
}
func (p *Pomodoro) Stop() bool {
if !p.running {
return false
}
close(p.done)
return true
}
func main() {
print("\033[H\033[2J")
stdin := bufio.NewReader(os.Stdin)
pomodoro := NewPomodoro()
for {
// Pomodoro.
if !pomodoro.Run(25 * 60) {
print(" Hit enter to start pomodoro..")
stdin.ReadString('\n')
print("\033[H\033[2J")
continue
}
print("\a Hit enter to start break..")
stdin.ReadString('\n')
print("\033[H\033[2J")
// Break.
pomodoro.Run(5 * 60)
print("\a Hit enter to start pomodoro..")
stdin.ReadString('\n')
print("\033[H\033[2J")
}
}