-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
251 lines (207 loc) · 5.78 KB
/
cmd.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
package main
import (
"bufio"
"github.com/cranej/ticktock/server"
"github.com/cranej/ticktock/store"
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
)
var Cli struct {
Db string `required:"" env:"TICKTOCK_DB" type:"path" help:"Path of the db file"`
Start StartCmd `cmd:"" help:"Start a ticktock"`
Finish FinishCmd `cmd:"" help:"Finish the ongoing ticktock"`
Titles TitlesCmd `cmd:"" help:"Print recent finished titles"`
Ongoing OngoingCmd `cmd:"" help:"Show currently ongoing ticktock"`
Last LastCmd `cmd:"" help:"Show last finished ticktock details of title"`
Report ReportCmd `cmd:"" help:"Show time usage report"`
Server ServerCmd `cmd:"" help:"Start a server"`
}
type StartCmd struct {
Wait bool `short:"w" help:"If set, wait for notes input until Ctrl-D, then finish the ticktock"`
Title string `arg:"" optional:"" name:"title" help:"The title of the ticktock. Choose interactively if not given"`
Notes string `help:"Notes of the ticktock"`
}
func (c *StartCmd) Run(ss store.Store) error {
title, err := chooseTitleAsNeed(c.Title, ss)
if err != nil {
return err
}
if err := ss.StartTitle(title, c.Notes); err != nil {
return err
}
fmt.Printf("(Started: %s)\n", c.Title)
if c.Wait {
fmt.Println("Waiting for notes input, Ctrl-D ends the input and finish the ticktock:")
notes, err := readToEOF()
if err != nil {
return fmt.Errorf("failed to read notes: %w, ticktock not finished", err)
}
r, err := ss.Finish(notes)
if err != nil {
return err
}
fmt.Printf("(Finished: %s)\n", r)
return nil
} else {
return nil
}
}
type FinishCmd struct {
Notes []string `help:"Notes to appends, each input as a line. If a single '-' is specified, read from stdin."`
}
func (c *FinishCmd) Run(ss store.Store) error {
var notes string
var err error
if len(c.Notes) == 1 && c.Notes[0] == "-" {
notes, err = readToEOF()
if err != nil {
return err
}
} else if len(c.Notes) > 1 {
notes = strings.Join(c.Notes, "\n")
}
r, err := ss.Finish(notes)
if err != nil {
return err
}
if len(r) != 0 {
fmt.Printf("(Finished: %s)\n", r)
} else {
fmt.Println("(NothingToFinish)")
}
return nil
}
type TitlesCmd struct {
Limit uint8 `short:"n" default:"5" help:"Number of titles to display, default 5"`
Index bool `short:"i" help:"If set, prefix titles with index starts from 1"`
}
func (c *TitlesCmd) Run(ss store.Store) error {
limit := uint8(5)
if c.Limit > 0 {
limit = c.Limit
}
titles, err := ss.RecentTitles(limit)
if err != nil {
return err
}
var f func(int, string)
if c.Index {
f = func(i int, title string) { fmt.Printf("%d: %s\n", i+1, title) }
} else {
f = func(i int, title string) { fmt.Println(title) }
}
for i, t := range titles {
f(i, t)
}
return nil
}
type OngoingCmd struct {
}
func (c *OngoingCmd) Run(ss store.Store) error {
entry, err := ss.Ongoing()
if err != nil {
return err
}
if entry == nil {
fmt.Println("No ongoing entry.")
return nil
}
duration := time.Since(entry.Start)
fmt.Printf("%s\n%.0f minutes ago\n", entry.Title, duration.Minutes())
return nil
}
type LastCmd struct {
Title string `arg:"" optional:"" help:"Title of the ticktock. Choose interactively if not given"`
}
func (c *LastCmd) Run(ss store.Store) error {
title, err := chooseTitleAsNeed(c.Title, ss)
if err != nil {
return err
}
last, err := ss.LastFinished(title)
if err != nil {
return err
}
fmt.Println(last.Format())
return nil
}
type ReportCmd struct {
Type string `default:"summary" enum:"summary,detail,dist,efforts" help:"Type of the report to show, valid values are: summary, detail, dist (distribution), and efforts"`
From uint16 `short:"f" default:"0" help:"Show report of ticktocks from '@today - From'. For example, '--from 1' shows report from yesterday 00:00:00"`
To uint16 `short:"t" default:"0" help:"Show report to @today - To. For example, '--to 1' shows report to yesterday 23:59:59"`
Title []string `help:"filter by titles"`
}
func (c *ReportCmd) Run(ss store.Store) error {
now := time.Now()
queryStart := time.Date(now.Year(), now.Month(), now.Day()-int(c.From), 0, 0, 0, 0, time.Local).UTC()
queryEnd := time.Date(now.Year(), now.Month(), now.Day()-int(c.To), 23, 59, 59, 0, time.Local).UTC()
entries, err := ss.Finished(queryStart, queryEnd, c.Title)
if err != nil {
return err
}
view, err := store.View(entries, c.Type)
if err != nil {
return err
}
fmt.Println(view)
return nil
}
type ServerCmd struct {
Addr string `arg:"" help:"Address to which the server listens"`
}
func (c *ServerCmd) Run(ss store.Store) error {
env := server.Env{Store: ss}
return env.Run(c.Addr)
}
var errCannotReadIndex error = errors.New("cannot read index")
var errInvalidIndex error = errors.New("invalid index")
var errNothingToChoose error = errors.New("candidates is empty")
const DEFAULT_LIMIT uint8 = 5
func chooseTitleAsNeed(title string, ss store.Store) (string, error) {
if title != "" {
return title, nil
}
titles, err := ss.RecentTitles(DEFAULT_LIMIT)
if err != nil {
return "", nil
}
return chooseString(titles)
}
func readToEOF() (string, error) {
var b strings.Builder
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
fmt.Fprintln(&b, scanner.Text())
}
return b.String(), scanner.Err()
}
func chooseString(candidates []string) (string, error) {
if len(candidates) == 0 {
return "", errNothingToChoose
}
for i, s := range candidates {
fmt.Printf("%d: %s\n", i+1, s)
}
fmt.Print("Choose index (default 1): ")
scanner := bufio.NewScanner(os.Stdin)
if !scanner.Scan() {
return "", errCannotReadIndex
}
var i int
var err error
if scanner.Text() == "" {
i = 1
} else {
if i, err = strconv.Atoi(scanner.Text()); err != nil {
return "", err
}
if i > len(candidates) || i < 1 {
return "", errInvalidIndex
}
}
return candidates[i-1], nil
}