-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
136 lines (119 loc) · 2.85 KB
/
cli.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
package app
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/c-bata/go-prompt"
)
func (a *App) executor(in string) {
cmd := strings.Split(in, " ")
switch cmd[0] {
case "start":
if len(cmd) > 3 {
fmt.Println("invalid number of arguments")
return
}
desc := ""
if len(cmd) == 3 {
desc = cmd[2]
}
err := a.d.Start(cmd[1], desc, time.Now().Unix())
if err != nil {
fmt.Println(err)
} else {
fmt.Println("task started successfully")
}
case "stop":
if len(cmd) != 2 {
fmt.Println("invalid number of arguments")
return
}
err := a.d.End(cmd[1], time.Now().Unix())
if err != nil {
fmt.Println(err)
} else {
fmt.Println("task stopped successfully")
}
case "add":
if len(cmd) == 3 {
cmd = append(cmd, "")
} else if len(cmd) != 4 {
fmt.Println("invalid number of arguments")
return
}
start, err := strconv.Atoi(cmd[1])
if err != nil {
fmt.Println("start must be an integer")
return
}
end, err := strconv.Atoi(cmd[2])
if err != nil {
fmt.Println("end must be an integer")
return
}
err = a.d.Start(cmd[0], cmd[3], int64(start))
if err != nil {
fmt.Printf("can't start task: %s\n", err.Error())
return
}
err = a.d.End(cmd[0], int64(end))
if err != nil {
fmt.Printf("can't start task: %s\n", err.Error())
return
}
case "delete":
if len(cmd) != 1 {
fmt.Println("invalid number of arguments")
return
}
err := a.d.Delete(cmd[0])
if err != nil {
fmt.Printf("can't delete record: %s\n", err.Error())
return
}
case "list":
if len(cmd) != 1 {
fmt.Println("invalid number of arguments")
return
}
records, err := a.d.List("")
if err != nil {
fmt.Println(err)
return
}
for _, r := range records {
endTime := time.Unix(r.End, 0).Format(time.RFC1123)
if r.End == 0 {
endTime = "in progress"
}
fmt.Printf("%s %s %s\n", r.Name, time.Unix(r.Start, 0).Format(time.RFC1123), endTime)
}
case "exit":
os.Exit(0)
default:
fmt.Println("Unknown command")
}
}
func (a *App) completer(in prompt.Document) []prompt.Suggest {
s := []prompt.Suggest{
{Text: "start", Description: "Start task with specified name and optional description"},
{Text: "stop", Description: "Stop task by name"},
{Text: "add", Description: "Add a new custom record with the given name, start, end (UNIX time) and optional description."},
{Text: "delete", Description: "Delete task by name"},
{Text: "list", Description: "List all tasks"},
{Text: "export", Description: "Export tasks to CSV file"},
{Text: "exit", Description: "Exit the program"},
}
cl := in.CurrentLine()
var f []string
f = strings.Fields(cl)
if strings.HasSuffix(cl, " ") {
f = append(f, " ")
}
if len(f) == 1 {
return prompt.FilterHasPrefix(s, in.GetWordBeforeCursor(), true)
}
return prompt.FilterHasPrefix([]prompt.Suggest{}, in.GetWordBeforeCursor(), true)
}