1 | package main
|
2 |
|
3 | import (
|
4 | "encoding/json"
|
5 | "fmt"
|
6 | "io/ioutil"
|
7 | "os"
|
8 | "time"
|
9 |
|
10 | "github.com/0xAX/notificator"
|
11 | "github.com/fatih/color"
|
12 | )
|
13 |
|
14 | const (
|
15 | defaultDateTimeFmt = "2006-01-02 15:04"
|
16 | )
|
17 |
|
18 | type State int
|
19 |
|
20 | func (s State) String() string {
|
21 | switch s {
|
22 | case RUNNING:
|
23 | return "RUNNING"
|
24 | case BREAKING:
|
25 | return "BREAKING"
|
26 | case COMPLETE:
|
27 | return "COMPLETE"
|
28 | case PAUSED:
|
29 | return "PAUSED"
|
30 | }
|
31 | return ""
|
32 | }
|
33 |
|
34 | const (
|
35 | RUNNING State = iota + 1
|
36 | BREAKING
|
37 | COMPLETE
|
38 | PAUSED
|
39 | )
|
40 |
|
41 | // Wheel keeps track of an ASCII spinner
|
42 | type Wheel int
|
43 |
|
44 | func (w *Wheel) String() string {
|
45 | switch int(*w) {
|
46 | case 0:
|
47 | *w++
|
48 | return "|"
|
49 | case 1:
|
50 | *w++
|
51 | return "/"
|
52 | case 2:
|
53 | *w++
|
54 | return "-"
|
55 | case 3:
|
56 | *w = 0
|
57 | return "\\"
|
58 | }
|
59 | return ""
|
60 | }
|
61 |
|
62 | // Config represents user preferences
|
63 | type Config struct {
|
64 | Colors map[string]*color.Color
|
65 | DateTimeFmt string
|
66 | }
|
67 |
|
68 | var colorMap = map[string]*color.Color{
|
69 | "black": color.New(color.FgBlack),
|
70 | "hiblack": color.New(color.FgHiBlack),
|
71 | "blue": color.New(color.FgBlue),
|
72 | "hiblue": color.New(color.FgHiBlue),
|
73 | "cyan": color.New(color.FgCyan),
|
74 | "hicyan": color.New(color.FgHiCyan),
|
75 | "green": color.New(color.FgGreen),
|
76 | "higreen": color.New(color.FgHiGreen),
|
77 | "magenta": color.New(color.FgMagenta),
|
78 | "himagenta": color.New(color.FgHiMagenta),
|
79 | "red": color.New(color.FgRed),
|
80 | "hired": color.New(color.FgHiRed),
|
81 | "white": color.New(color.FgWhite),
|
82 | "hiwrite": color.New(color.FgHiWhite),
|
83 | "yellow": color.New(color.FgYellow),
|
84 | "hiyellow": color.New(color.FgHiYellow),
|
85 | }
|
86 |
|
87 | func (c *Config) UnmarshalJSON(raw []byte) error {
|
88 | config := &struct {
|
89 | Colors map[string]string `json:"colors"`
|
90 | DateTimeFmt string `json:"datetimefmt"`
|
91 | }{}
|
92 | err := json.Unmarshal(raw, config)
|
93 | if err != nil {
|
94 | return err
|
95 | }
|
96 | for key, name := range config.Colors {
|
97 | if color, ok := colorMap[name]; ok {
|
98 | c.Colors[key] = color
|
99 | } else {
|
100 | return fmt.Errorf("bad color choice: %s", name)
|
101 | }
|
102 | }
|
103 | if config.DateTimeFmt != "" {
|
104 | c.DateTimeFmt = config.DateTimeFmt
|
105 | } else {
|
106 | c.DateTimeFmt = defaultDateTimeFmt
|
107 | }
|
108 | return nil
|
109 | }
|
110 |
|
111 | func NewConfig(path string) (*Config, error) {
|
112 | raw, err := ioutil.ReadFile(path)
|
113 | if err != nil {
|
114 | // Create an empty config file
|
115 | // if it does not already exist.
|
116 | if os.IsNotExist(err) {
|
117 | raw, _ := json.Marshal(map[string]*color.Color{})
|
118 | ioutil.WriteFile(path, raw, 0644)
|
119 | return NewConfig(path)
|
120 | }
|
121 | return nil, err
|
122 | }
|
123 | config := &Config{
|
124 | Colors: map[string]*color.Color{},
|
125 | }
|
126 | err = json.Unmarshal(raw, config)
|
127 | if err != nil {
|
128 | return nil, err
|
129 | }
|
130 | return config, json.Unmarshal(raw, config)
|
131 | }
|
132 |
|
133 | // Task describes some activity
|
134 | type Task struct {
|
135 | ID int `json:"id"`
|
136 | Message string `json:"message"`
|
137 | // Array of completed pomodoros
|
138 | Pomodoros []*Pomodoro `json:"pomodoros"`
|
139 | // Free-form tags associated with this task
|
140 | Tags []string `json:"tags"`
|
141 | // Number of pomodoros for this task
|
142 | NPomodoros int `json:"n_pomodoros"`
|
143 | // Duration of each pomodoro
|
144 | Duration time.Duration `json:"duration"`
|
145 | }
|
146 |
|
147 | // ByID is a sortable array of tasks
|
148 | type ByID []*Task
|
149 |
|
150 | func (b ByID) Len() int { return len(b) }
|
151 | func (b ByID) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
|
152 | func (b ByID) Less(i, j int) bool { return b[i].ID < b[j].ID }
|
153 |
|
154 | // After returns tasks that were started after the
|
155 | // provided start time.
|
156 | func After(start time.Time, tasks []*Task) []*Task {
|
157 | filtered := []*Task{}
|
158 | for _, task := range tasks {
|
159 | if len(task.Pomodoros) > 0 {
|
160 | if start.Before(task.Pomodoros[0].Start) {
|
161 | filtered = append(filtered, task)
|
162 | }
|
163 | }
|
164 | }
|
165 | return filtered
|
166 | }
|
167 |
|
168 | // Pomodoro is a unit of time to spend working
|
169 | // on a single task.
|
170 | type Pomodoro struct {
|
171 | Start time.Time `json:"start"`
|
172 | End time.Time `json:"end"`
|
173 | }
|
174 |
|
175 | // Duration returns the runtime of the pomodoro
|
176 | func (p Pomodoro) Duration() time.Duration {
|
177 | return (p.End.Sub(p.Start))
|
178 | }
|
179 |
|
180 | // Status is used to communicate the state
|
181 | // of a running Pomodoro session
|
182 | type Status struct {
|
183 | State State `json:"state"`
|
184 | Remaining time.Duration `json:"remaining"`
|
185 | Count int `json:"count"`
|
186 | NPomodoros int `json:"n_pomodoros"`
|
187 | }
|
188 |
|
189 | // Notifier sends a system notification
|
190 | type Notifier interface {
|
191 | Notify(string, string) error
|
192 | }
|
193 |
|
194 | // NoopNotifier does nothing
|
195 | type NoopNotifier struct{}
|
196 |
|
197 | func (n NoopNotifier) Notify(string, string) error { return nil }
|
198 |
|
199 | // Xnotifier can push notifications to mac, linux and windows.
|
200 | type Xnotifier struct {
|
201 | *notificator.Notificator
|
202 | iconPath string
|
203 | }
|
204 |
|
205 | func NewXnotifier(iconPath string) Notifier {
|
206 | // Write the built-in tomato icon if it
|
207 | // doesn't already exist.
|
208 | _, err := os.Stat(iconPath)
|
209 | if os.IsNotExist(err) {
|
210 | raw := MustAsset("tomato-icon.png")
|
211 | _ = ioutil.WriteFile(iconPath, raw, 0644)
|
212 | }
|
213 | return Xnotifier{
|
214 | Notificator: notificator.New(notificator.Options{}),
|
215 | iconPath: iconPath,
|
216 | }
|
217 | }
|
218 |
|
219 | // Notify sends a notification to the OS.
|
220 | func (n Xnotifier) Notify(title, body string) error {
|
221 | return n.Push(title, body, n.iconPath, notificator.UR_NORMAL)
|
222 | }
|