-
Notifications
You must be signed in to change notification settings - Fork 1
/
Todo.go
135 lines (130 loc) · 3.28 KB
/
Todo.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
)
type Todo struct {
list []*Task
input string
}
func NewTodo() Todo {
return Todo{
list: []*Task{},
input: "",
}
}
func (todo *Todo) NewTask(name string) *Task {
task := NewTask(name)
todo.list = append(todo.list, task)
return task
}
func (todo *Todo) InsertTask(index int, name string) {
task := NewTask(name)
if len(todo.list) == index { // nil or empty slice or after last element
todo.list = append(todo.list, task)
}
todo.list = append(todo.list[:index+1], todo.list[index:]...) // index < len(a)
todo.list[index] = task
}
func (todo *Todo) RemoveTask(index int) {
if index >= len(todo.list) {
return
}
if len(todo.list) < 1 {
todo.list = []*Task{}
return
}
todo.list = append(todo.list[:index], todo.list[index+1:]...)
}
func (todo *Todo) Save() {
data, err := json.Marshal(todo.list)
if err != nil {
fmt.Println(err)
}
err = ioutil.WriteFile("data.json", data, 0644)
if err != nil {
fmt.Println(err)
}
}
func (todo *Todo) Render() {
todo.input = CLIInput()
data, _ := ioutil.ReadFile("data.json")
json.Unmarshal(data, &todo.list)
ResetStyle()
switch todo.input {
case "help":
PrintBold(Yellow("- new: "))
PrintUnderline("add a new task into the list\n")
PrintBold(Yellow("- del: "))
PrintUnderline("delete the task by put in the position\n")
PrintBold(Yellow("- show: "))
PrintUnderline("display all the task\n")
PrintBold(Yellow("- do: "))
PrintUnderline("making the undone task into done task or making the done task into undone task\n")
PrintBold(Yellow("- quit: "))
PrintUnderline("quit the program\n")
case "new":
todo.input = GetInput("task name: ")
todo.NewTask(todo.input)
fmt.Print(CHECKMARK)
msg := fmt.Sprintf(" sucess to add new '%v' task from the list\n", todo.input)
PrintSucess(msg)
ResetStyle()
case "del":
todo.input = GetInput("position of the task: ")
position, _ := strconv.Atoi(todo.input)
if len(todo.list) > position {
msg := fmt.Sprintf("%v sucess to delete '%v' task from the list\n", CHECKMARK, todo.list[position].GetName())
todo.RemoveTask(position)
PrintSucess(msg)
ResetStyle()
} else {
fmt.Print(CROSSMARK)
msg := fmt.Sprintf(" cannot delete the task,because the task doesn't exist\n")
PrintWarning(msg)
}
case "show":
fmt.Println("--- Tasks ---")
if len(todo.list) < 1 {
fmt.Println("nothing here")
}
for i, v := range todo.list {
symbol := func() string {
if v.IsDone() {
return CHECKMARK
}
return CROSSMARK
}()
msg := fmt.Sprintf("%v %v. %v\n", symbol, i, v.GetName())
PrintSucess(msg)
}
fmt.Println("-------------")
case "do":
todo.input = GetInput("position of the task: ")
position, _ := strconv.Atoi(todo.input)
task := todo.list[position]
if task.IsDone() {
task.ReDo()
fmt.Print(CHECKMARK)
msg := fmt.Sprintf(" sucess to change the '%v' task into undone\n", task.GetName())
PrintSucess(msg)
} else {
task.Done()
fmt.Print(CHECKMARK)
msg := fmt.Sprintf(" sucess to change the '%v' task into done\n", task.GetName())
PrintSucess(msg)
}
ResetStyle()
case "quit":
fmt.Print(CHECKMARK)
PrintSucess(" quitted sucessfully\n")
return
default:
fmt.Print(CROSSMARK)
PrintWarning(" invalid command\n")
}
todo.Save()
todo.Render()
}