-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
81 lines (63 loc) · 1.7 KB
/
session.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
package main
import (
"time"
"github.com/0xAX/notificator"
"github.com/getlantern/systray"
"github.com/lobre/goodhosts"
)
type session struct {
config *Config
notify *notificator.Notificator
}
func (session *session) start() chan struct{} {
complete := make(chan struct{})
// TODO: add quit channel
go func(chan struct{}) {
for repeat := 1; repeat <= session.config.Pomodoro.Repeats; repeat++ {
lockTimer := time.NewTimer(time.Duration(session.config.Pomodoro.Work) * time.Minute)
// TODO: use icons instead of text
systray.SetTitle("I'm working hard")
session.notify.Push("Work hard", "", "", notificator.UR_NORMAL)
session.work()
<-lockTimer.C
restTimer := time.NewTimer(time.Duration(session.config.Pomodoro.Rest) * time.Minute)
systray.SetTitle("I'm resting")
session.notify.Push("Take a rest", "", "", notificator.UR_NORMAL)
session.rest()
<-restTimer.C
}
complete <- struct{}{}
}(complete)
return complete
}
func (session *session) rest() {
hosts, err := goodhosts.NewHosts()
if err != nil {
// TODO: may be it`s better just notify and stop
panic(err)
}
for _, bad := range session.config.Domains.Bad {
if hosts.Has(session.config.WorkIp, bad) {
hosts.Remove(session.config.WorkIp, bad)
hosts.Remove(session.config.WorkIp, "www."+bad)
}
}
if err := hosts.Flush(); err != nil {
panic(err)
}
}
func (session *session) work() {
hosts, err := goodhosts.NewHosts()
if err != nil {
panic(err)
}
for _, bad := range session.config.Domains.Bad {
if !hosts.Has(session.config.WorkIp, bad) {
hosts.Add(session.config.WorkIp, bad)
hosts.Add(session.config.WorkIp, "www."+bad)
}
}
if err := hosts.Flush(); err != nil {
panic(err)
}
}