/
gomato.go
349 lines (318 loc) · 7.19 KB
/
gomato.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"sync"
"time"
"github.com/0xAX/notificator"
"github.com/gin-gonic/gin"
"github.com/nsf/termbox-go"
"github.com/sethgrid/multibar"
"github.com/tkancf/curse"
"github.com/urfave/cli"
)
type Task struct {
Name string `json:"name"`
State string `json:"state"`
Date string `json:"date"`
Time string `json:"time"`
Elapsed int `json:"Elapsed"`
}
type Tasks []Task
var data Task
type File struct {
TemplateDir string
Json string
}
var files File
var notify *notificator.Notificator
func main() {
files.Json = getJsonFile()
files.TemplateDir = getTemplateDir()
notify = notificator.New(notificator.Options{
DefaultIcon: "icon/default.png",
AppName: "gomato",
})
err := termbox.Init()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
commands()
}
func commands() {
// init cli app
app := cli.NewApp()
app.Name = "gomato"
app.Usage = "Pomodoro Timer in Console written in golang."
app.Version = "0.1.0"
app.Authors = []cli.Author{
cli.Author{
Name: "tkan",
Email: "18424095+tkancf@users.noreply.github.com",
},
}
app.Commands = []cli.Command{
{
Name: "timer",
Aliases: []string{"t"},
Usage: "Start pomodoro timer\n\tYou can set time [task] [short break] [long break]\n\t(default time is 25 5 15)",
Action: timerAction,
},
{
Name: "server",
Aliases: []string{"s"},
Usage: "Start server to show tasks \n\thttp://localhost:3000/",
Action: serverAction,
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "List tasks",
Action: listAction,
},
}
app.After = func(c *cli.Context) error {
return nil
}
app.Run(os.Args)
}
func getTemplateDir() string {
gopath := os.Getenv("GOPATH")
tmp := filepath.Join(gopath, "src", "github.com", "tkancf", "gomato", "templates")
return tmp
}
func getJsonFile() string {
gopath := os.Getenv("GOPATH")
json := filepath.Join(gopath, "src", "github.com", "tkancf", "gomato", "data", "data.json")
return json
}
func timerAction(c *cli.Context) {
if !fileExists(files.Json) {
ioutil.WriteFile(files.Json, []byte(""), os.ModePerm)
}
fmt.Println("'q' Key to Quit")
cu, _ := curse.New()
c0 := "25"
c1 := "5"
c2 := "15"
data.Name = "unknown"
if c.NArg() == 4 {
data.Name = c.Args().Get(0)
c0 = c.Args().Get(1)
c1 = c.Args().Get(2)
c2 = c.Args().Get(3)
} else if c.NArg() == 1 {
data.Name = c.Args().Get(0)
}
t0, _ := strconv.Atoi(c0)
t1, _ := strconv.Atoi(c1)
t2, _ := strconv.Atoi(c2)
for {
for i := 1; i < 4; i++ {
data.State = "task"
timer(t0, getTaskString(data.State, t0, i))
cu.MoveUp(1)
cu.EraseCurrentLine()
data.Elapsed = t0
notify.Push("gomato", data.Name+" task is finished!", "/home/user/icon.png", notificator.UR_CRITICAL)
checkContinue()
data.State = "break"
timer(t1, getTaskString(data.State, t1, i))
cu.MoveUp(1)
cu.EraseCurrentLine()
data.Elapsed = t1
notify.Push("gomato", "break time is finished", "/home/user/icon.png", notificator.UR_CRITICAL)
checkContinue()
}
data.State = "task"
timer(t0, getTaskString(data.State, t0, 4))
cu.MoveUp(1)
cu.EraseCurrentLine()
data.Elapsed = t0
notify.Push("gomato", data.Name+" task is finished!", "/home/user/icon.png", notificator.UR_CRITICAL)
checkContinue()
data.State = "lbreak"
timer(t2, getTaskString(data.State, t2, 0))
data.Elapsed = t2
notify.Push("gomato", "long break time is finished", "/home/user/icon.png", notificator.UR_CRITICAL)
checkContinue()
}
}
// t = 経過時間, s = 左に表示するステータス
func timer(t int, s string) {
t = t * 60
data.Time = getTime()
data.Date = getDate()
progressBars, _ := multibar.New()
wg := &sync.WaitGroup{}
wg.Add(1)
barProgress := progressBars.MakeBar(t, s)
go handleKeyEvent()
go progressBars.Listen()
go func() {
for i := 0; i <= t; i++ {
data.Elapsed = i
barProgress(i)
time.Sleep(time.Second * 1)
}
wg.Done()
}()
wg.Wait()
}
func getTime() string {
t := time.Now()
const layout = "15:04"
ts := t.Format(layout)
return ts
}
func getDate() string {
t := time.Now()
const layout = "2006-01-02"
ts := t.Format(layout)
return ts
}
// タイマー終了後の継続確認
func checkContinue() {
fmt.Printf("'ENTER' to Continue")
saveData()
switch ev := termbox.PollEvent(); ev.Type {
}
}
func getTaskString(s string, t int, i int) string {
ts := strconv.Itoa(t)
is := strconv.Itoa(i)
str := ""
if s == "task" {
data.State = s
str = data.Name + "[task time]" + "(" + is + "/4)<" + ts + "Minutes>"
} else if s == "break" {
data.State = s
str = "[break time]" + "(" + is + "/4)<" + ts + "Minutes>"
} else if s == "lbreak" {
data.State = s
str = "[long break time]<" + ts + "Minutes>"
} else {
return str
}
return str
}
func handleKeyEvent() {
loop:
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
if ev.Key == termbox.KeyEsc {
os.Exit(1)
} else if ev.Ch == 'q' {
saveFile(saveData())
break loop
}
}
}
os.Exit(1)
}
func handleKeyEventNoSave() {
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
if ev.Key == termbox.KeyEsc {
os.Exit(1)
} else if ev.Ch == 'q' {
os.Exit(1)
}
}
}
}
func saveData() Tasks {
var s Tasks
if len(s) == 0 {
s = getJson(files.Json)
}
s = append(s, Task{Name: data.Name, State: data.State, Date: data.Date, Time: data.Time, Elapsed: data.Elapsed})
return s
}
func saveFile(s Tasks) {
writeJson(files.Json, &s)
}
func fileExists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func getJson(p string) Tasks {
var t Tasks
raw, err := ioutil.ReadFile(p)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
json.Unmarshal(raw, &t)
return t
}
func writeJson(p string, t *Tasks) {
json, err := json.Marshal(t)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
ioutil.WriteFile(p, json, os.ModePerm)
}
func serverAction(c *cli.Context) {
go handleKeyEventNoSave()
keys, values := getTaskTimeArray(files.Json)
fmt.Println("Server start\nAccess to http://localhost:3000/\n'q' Key to Quit")
router := gin.Default()
gin.SetMode(gin.ReleaseMode)
router.LoadHTMLGlob(filepath.Join(files.TemplateDir, "*"))
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"keys": keys,
"values": values,
})
})
router.Run(":3000")
}
func getTaskTimeArray(path string) ([]string, []int) {
jsonData := getJson(path)
m := map[string]int{}
keys := []string{}
values := []int{}
for i := 0; i < len(jsonData); i++ {
_, ok := m[jsonData[i].Name]
if ok == false {
m[jsonData[i].Name] = jsonData[i].Elapsed
} else {
m[jsonData[i].Name] = m[jsonData[i].Name] + jsonData[i].Elapsed
}
}
for k, v := range m {
keys = append(keys, k)
values = append(values, v)
}
return keys, values
}
func listAction(c *cli.Context) {
jsonData := getJson(files.Json)
s := []string{}
for i := 0; i < len(jsonData); i++ {
if !contains(s, jsonData[i].Name) {
s = append(s, jsonData[i].Name)
}
}
for i := 0; i < len(s); i++ {
fmt.Println(s[i])
}
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}