-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
195 lines (166 loc) · 4.08 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
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
)
type Todo struct {
Text string `json:"text"`
Done bool `json:"done"`
Urgency int `json:"urgency"`
}
type UrgencyLevel struct {
Name string
Value int
Color string
}
var urgencyLevels = []UrgencyLevel{
{Name: "Critical", Value: 1, Color: "\033[35m"}, // Magenta
{Name: "High", Value: 2, Color: "\033[31m"}, // Red
{Name: "Medium", Value: 3, Color: "\033[33m"}, // Yellow
{Name: "Low", Value: 4, Color: "\033[36m"}, // Cyan
{Name: "Very Low", Value: 5, Color: "\033[32m"}, // Green
}
var todoDir string
var todoFile string
func init() {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Println("Error getting home directory:", err)
os.Exit(1)
}
todoDir = filepath.Join(homeDir, ".todo")
todoFile = filepath.Join(todoDir, "todos.json")
if err := os.MkdirAll(todoDir, 0755); err != nil {
fmt.Println("Error creating directory:", err)
os.Exit(1)
}
}
func selectUrgency() int {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Println("\nSelect Urgency Level")
for i, level := range urgencyLevels {
fmt.Printf("%d %s%s\033[0m\n", i+1, level.Color, level.Name)
}
fmt.Print("\nEnter urgency (1-5): ")
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
urgency, err := strconv.Atoi(input)
if err != nil || urgency < 1 || urgency > 5 {
fmt.Println("Error: Please enter a number between 1 and 5")
continue
}
return urgency
}
}
func saveTodos(todos []Todo) error {
file, err := os.Create(todoFile)
if err != nil {
return err
}
defer file.Close()
return json.NewEncoder(file).Encode(todos)
}
func loadTodos() []Todo {
var todos []Todo
file, err := os.Open(todoFile)
if err != nil {
return []Todo{}
}
defer file.Close()
json.NewDecoder(file).Decode(&todos)
return todos
}
func getColorByUrgency(urgency int) string {
for _, level := range urgencyLevels {
if level.Value == urgency {
return level.Color
}
}
return "\033[0m" // Reset
}
func displayTodos(todos []Todo, showAll bool) {
reset := "\033[0m"
for i, todo := range todos {
if !todo.Done || showAll {
color := getColorByUrgency(todo.Urgency)
doneStr := " "
if todo.Done {
doneStr = "✓"
}
fmt.Printf("%d. [%s] %s%s%s\n", i+1, doneStr, color, todo.Text, reset)
}
}
}
func main() {
todos := loadTodos()
reader := bufio.NewReader(os.Stdin)
// Get the base command name from the executable path
command := filepath.Base(os.Args[0])
// Get all arguments after the command
args := os.Args[1:]
// Special handling for 'td -a' case
if command == "td" && len(args) > 0 && args[0] == "-a" {
command = "-a"
args = args[1:]
}
switch command {
case "td", "main":
displayTodos(todos, false)
case "-a":
displayTodos(todos, true)
case "tda":
fmt.Print("Enter todo text: ")
text, _ := reader.ReadString('\n')
text = strings.TrimSpace(text)
urgency := selectUrgency()
todos = append(todos, Todo{
Text: text,
Done: false,
Urgency: urgency,
})
if err := saveTodos(todos); err != nil {
fmt.Println("Error saving todo:", err)
}
case "tdd":
if len(args) < 1 {
fmt.Println("Please provide todo index")
return
}
index, err := strconv.Atoi(args[0])
if err != nil || index < 1 || index > len(todos) {
fmt.Println("Invalid index")
return
}
todos[index-1].Done = true
if err := saveTodos(todos); err != nil {
fmt.Println("Error saving todo:", err)
}
case "tdr":
if len(args) < 1 {
fmt.Println("Please provide todo index")
return
}
index, err := strconv.Atoi(args[0])
if err != nil || index < 1 || index > len(todos) {
fmt.Println("Invalid index")
return
}
todos = append(todos[:index-1], todos[index:]...)
if err := saveTodos(todos); err != nil {
fmt.Println("Error saving todo:", err)
}
default:
fmt.Println("Usage:")
fmt.Println(" td - List active todos")
fmt.Println(" td -a - List all todos")
fmt.Println(" tda - Add new todo")
fmt.Println(" tdd N - Mark todo N as done")
fmt.Println(" tdr N - Remove todo N")
}
}