-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
364 lines (317 loc) · 8.54 KB
/
main.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package main
import (
"bufio"
"database/sql"
"flag"
"fmt"
"os"
"strconv"
"strings"
"github.com/fatih/color"
_ "github.com/mattn/go-sqlite3"
)
// Setup - Create a database if not exist (tick -s)
func setup() {
// Header
fmt.Print(`
_____ _____
/\ \ /\ \
/::\ \ /::\ \
\:::\ \ \:::\ \
\:::\ \ \:::\ \
\:::\ \ \:::\ \
\:::\ \ \:::\ \
/::::\ \ /::::\ \
/::::::\ \ /::::::\ \
/:::/\:::\ \ /:::/\:::\ \
/:::/ \:::\____\ /:::/ \:::\____\
/:::/ \::/ //:::/ \::/ /
/:::/ / \/____//:::/ / \/____/
/:::/ / /:::/ /
/:::/ / /:::/ /
\::/ / \::/ /
\/____/ \/____/
Welcome to Tick!
As name suggests this is a todo list that can be accessed on a terminal.
Type 'tick -h' for more info.`)
fmt.Print("\nDo you want to setup tick? (y/n): ")
reader := bufio.NewReader(os.Stdin)
consent, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
database, err := sql.Open("sqlite3", "./tick.db")
consent = strings.Replace(consent, "\n", "", -1)
if strings.Compare(consent, "y") == 0 {
color.Set(color.FgCyan, color.Bold)
if err != nil {
panic(err)
} else {
fmt.Print("Initilizing Database...")
}
stmt, stmterr := database.Prepare("CREATE TABLE IF NOT EXISTS AllTasks (ID INTEGER PRIMARY KEY, TASKS TEXT, DONE INTEGER DEFAULT 0, URGENCY INTEGER DEFAULT 0)") // 0 = not
if stmterr != nil {
panic(stmterr)
} else {
stmt.Exec()
}
fmt.Println("Done.\n")
color.Unset()
color.Set(color.FgGreen, color.Bold)
fmt.Println("Setup Complete...\nType 'tick -h' for help. \nWe hope Tick makes you more productive!")
color.Unset()
fmt.Println()
} else if strings.Compare(consent, "n") == 0 {
color.Red("Hope to see you soon!")
}
}
// Add Task (tick -a)
// Open a shell, that lets user to add tasks to db
// For Urgent - !urgent
// For Exit - !exit
func addTask() {
database, err := sql.Open("sqlite3", "./tick.db")
if err != nil {
panic(err)
}
reader := bufio.NewReader(os.Stdin)
red := color.New(color.FgRed).SprintFunc()
fmt.Printf("Welcome to Tick!\n================\nType %s at the start, for urgent tasks.\nType %s when you are done adding tasks.\nAdd Tasks,\n", red("!urgent"), red("!exit"))
i := 1
for {
fmt.Print(">")
task, _ := reader.ReadString('\n')
task = strings.Replace(task, "\n", "", -1)
if strings.Compare(task, "!exit") == 0 {
fmt.Println("Tasks added to list! \nExiting...")
break
} else {
if strings.HasPrefix(task, "!urgent ") {
task = strings.Trim(task, "!urgent ")
stmt, _ := database.Prepare("INSERT INTO AllTasks (TASKS, URGENCY) VALUES (?,?)")
stmt.Exec(task, 1)
} else {
stmt, _ := database.Prepare("INSERT INTO AllTasks (TASKS) VALUES (?)")
stmt.Exec(task)
}
}
i++
}
}
// Show Urgent Tasks
func showUrgent() {
database, err := sql.Open("sqlite3", "./tick.db")
if err != nil {
panic(err)
}
// Colors
heading := color.New(color.FgWhite, color.BgRed, color.Bold)
subHead := color.New(color.BgBlue, color.FgWhite, color.Bold)
urgentList := color.New(color.FgGreen, color.Bold)
heading.Print(" Welcome to Tick! ")
fmt.Println("\n------------------")
// Count Check
rows, err := database.Query("SELECT COUNT(*) as count FROM AllTasks WHERE URGENCY=1 and DONE=0")
if checkCount(rows) > 0 {
subHead.Print(" URGENT ")
fmt.Println("\n")
rows, err = database.Query("SELECT ID, TASKS FROM AllTasks WHERE URGENCY=1 and DONE=0")
// Data Retrival
var id int
var task string
for rows.Next() {
err = rows.Scan(&id, &task)
if err != nil {
panic(err)
}
urgentList.Printf("[ %d ] %s\n", id, task)
}
rows.Close()
} else {
subHead.Print(" No URGENT Tasks Present! ")
fmt.Println()
rows.Close()
}
}
func checkCount(rows *sql.Rows) (count int) {
for rows.Next() {
err := rows.Scan(&count)
if err != nil {
panic(err)
}
}
return count
}
func showTasks() {
// Urgent Tasks
showUrgent()
// Normal Tasks
database, err := sql.Open("sqlite3", "./tick.db")
if err != nil {
panic(err)
}
// Colors
subHead := color.New(color.BgBlue, color.FgWhite, color.Bold)
normalList := color.New(color.Bold)
fmt.Println("----------------------")
rows, err := database.Query("SELECT COUNT(*) as count FROM AllTasks where URGENCY=0 and DONE=0")
if checkCount(rows) > 0 {
subHead.Print(" NORMAL ")
fmt.Println("\n")
rows, err = database.Query("SELECT ID, TASKS FROM AllTasks WHERE URGENCY=0 and DONE=0")
// Data Retrival
var id int
var task string
for rows.Next() {
err = rows.Scan(&id, &task)
if err != nil {
panic(err)
}
normalList.Printf("[ %d ] %s\n", id, task)
}
rows.Close()
} else {
subHead.Print(" No Tasks Present! ")
fmt.Println()
rows.Close()
}
fmt.Println()
}
func showDeleted() {
database, err := sql.Open("sqlite3", "./tick.db")
if err != nil {
panic(err)
}
subHead := color.New(color.BgBlue, color.FgWhite, color.Bold)
deadList := color.New(color.FgRed, color.Bold)
heading := color.New(color.FgWhite, color.BgRed, color.Bold)
heading.Print(" Welcome to Tick! ")
fmt.Println("\n------------------")
rows, err := database.Query("SELECT COUNT(*) as count FROM AllTasks where DONE=1")
if checkCount(rows) > 0 {
subHead.Print(" COMPLETED TASKS ")
fmt.Println("\n")
rows, err = database.Query("SELECT ID, TASKS FROM AllTasks WHERE DONE=1")
// Data Retrival
var id int
var task string
for rows.Next() {
err = rows.Scan(&id, &task)
if err != nil {
panic(err)
}
deadList.Printf("[ %d ] %s\n", id, task)
}
rows.Close()
} else {
subHead.Print(" No Completed Tasks! ")
fmt.Println("\n")
os.Exit(0)
}
}
func delete() {
database, err := sql.Open("sqlite3", "./tick.db")
if err != nil {
panic(err)
}
subHead := color.New(color.BgRed, color.FgWhite, color.Bold)
rows, err := database.Query("SELECT COUNT(*) as count FROM AllTasks WHERE DONE=0")
if checkCount(rows) > 0 {
indices := flag.Args()
for i := 0; i < len(indices); i++ {
if _, err := strconv.Atoi(indices[i]); err == nil {
if stmt, err := database.Prepare("UPDATE AllTasks set DONE = 1 WHERE ID = ?"); err == nil {
stmt.Exec(indices[i])
}
}
}
subHead.Print(" Tasks Completed! ")
fmt.Println()
} else {
subHead.Print(" All tasks are completed! ")
fmt.Println()
}
fmt.Println()
}
func init() {
flag.Usage = func() {
h := `
Welcome to Tick!
A handy todo list, accessible from Terminal! (Terminal Todo)
Usage:
tick [OPTIONS] {-d|--delete} [args...]
Options:
Shows the complete to-do list
-s, --setup Setup Tick for use (initilize database)
-a, --add Interactive shell to add tasks.
> use !urgent at the start of the tasks to specify urgency
> use !exit at the start to exit the interactive shell
-d, --delete Deletes tasks based on tasks' indices
-u, --urgent Prints the urgent tasks only
--deleted Prints the deleted tasks only
--version Prints the version information
Example:
tick -d 1 2 5 // Deletes the tasks with index 1,2 and 5
`
fmt.Fprintf(os.Stderr, h)
}
}
func main() {
var (
setupFlag bool
addFlag bool
delFlag bool
urgentFlag bool
showDelFlag bool
versionFlag bool
)
flag.BoolVar(&setupFlag, "s", false, "")
flag.BoolVar(&setupFlag, "setup", false, "")
flag.BoolVar(&addFlag, "a", false, "")
flag.BoolVar(&addFlag, "add", false, "")
flag.BoolVar(&delFlag, "d", false, "")
flag.BoolVar(&delFlag, "delete", false, "")
flag.BoolVar(&urgentFlag, "u", false, "")
flag.BoolVar(&urgentFlag, "urgent", false, "")
flag.BoolVar(&showDelFlag, "deleted", false, "")
flag.BoolVar(&versionFlag, "v", false, "")
flag.BoolVar(&versionFlag, "version", false, "")
flag.Parse()
TickVersion := " v1.0.0"
if versionFlag {
subHead := color.New(color.BgRed, color.FgWhite, color.Bold)
subHead.Print(" Welcome to Tick", TickVersion, " ")
fmt.Println()
subHead = color.New(color.BgBlue, color.FgWhite, color.Bold)
subHead.Print(" Developed by @krAshwin ")
fmt.Println()
os.Exit(1)
}
if addFlag {
addTask()
os.Exit(1)
}
if setupFlag {
setup()
os.Exit(1)
}
if urgentFlag {
showUrgent()
os.Exit(1)
}
if showDelFlag {
showDeleted()
os.Exit(1)
}
if delFlag {
delete()
os.Exit(1)
}
if _, err := os.Stat("./tick.db"); os.IsNotExist(err) {
setup()
os.Exit(1)
} else {
showTasks()
os.Exit(1)
}
}