-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.go
More file actions
116 lines (96 loc) · 2.1 KB
/
Copy pathtask.go
File metadata and controls
116 lines (96 loc) · 2.1 KB
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
package main
import (
"errors"
"os"
"strconv"
"time"
"github.com/aquasecurity/table"
"github.com/fatih/color"
)
type Task struct {
Title string
Completed bool
CreatedAt time.Time
CompletedAt *time.Time
}
type Tasks []Task
func (t *Tasks) Add(title string) {
task := Task{
Title: title,
Completed: false,
CreatedAt: time.Now(),
}
*t = append(*t, task)
}
func (t *Tasks) ValidateIndex(index int) error {
if index < 0 || index >= len(*t) {
return errors.New("invalid index")
}
return nil
}
func (t *Tasks) Delete(index int) error {
if err := t.ValidateIndex(index); err != nil {
return err
}
*t = append((*t)[:index], (*t)[index+1:]...)
return nil
}
// Delete All Task
func (t *Tasks) DeleteAll() error {
*t = nil
return nil
}
func (t *Tasks) Toggle(index int) error {
task := *t
if err := task.ValidateIndex(index); err != nil {
return err
}
isCompleted := task[index].Completed
if !isCompleted {
completionTime := time.Now()
task[index].CompletedAt = &completionTime
}
task[index].Completed = !isCompleted
return nil
}
func (t *Tasks) Edit(index int, title string) error {
task := *t
if err := task.ValidateIndex(index); err != nil {
return err
}
task[index].Title = title
return nil
}
func (t *Tasks) Print() {
table := table.New(os.Stdout)
table.SetRowLines(false)
table.SetHeaders("#", "Task", "Completed", "Created At", "Completed At")
for index, todo := range *t {
completed := "No"
completedAt := ""
if todo.Completed {
completed = "Yes"
if todo.CompletedAt != nil {
completedAt = todo.CompletedAt.Format(time.RFC1123)
}
}
if todo.Completed {
table.AddRow(
color.GreenString(strconv.Itoa(index)),
color.GreenString(todo.Title),
color.GreenString(completed),
color.GreenString(todo.CreatedAt.Format(time.RFC1123)),
color.GreenString(completedAt),
)
} else {
table.AddRow(
color.RedString(strconv.Itoa(index)),
color.RedString(todo.Title),
color.RedString(completed),
color.RedString(todo.CreatedAt.Format(time.RFC1123)),
color.RedString(completedAt),
)
}
}
table.Render()
}