-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.go
132 lines (115 loc) · 2.67 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
package todocli
import (
"encoding/json"
"errors"
"fmt"
"os"
"time"
"github.com/alexeyco/simpletable"
)
type Item struct {
Task string `json:"task"`
Result bool `json:"result"`
Created time.Time `json:"created"`
Completed *time.Time `json:"completed,omitempty"`
}
type Todos []Item
func (t *Todos) Add(task string) {
todo := Item{
Task: task,
Result: false,
Created: time.Now(),
Completed: nil,
}
*t = append(*t, todo)
}
func (t *Todos) Complete(index int) error {
if err := t.validateIndex(index); err != nil {
return err
}
now := time.Now()
(*t)[index-1].Completed = &now
(*t)[index-1].Result = true
return nil
}
func (t *Todos) Delete(index int) error {
if err := t.validateIndex(index); err != nil {
return err
}
*t = append((*t)[:index-1], (*t)[index:]...)
return nil
}
func (t *Todos) Load(filename string) error {
file, err := os.ReadFile(filename)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}
if len(file) == 0 {
return nil
}
return json.Unmarshal(file, t)
}
func (t *Todos) Store(filename string) error {
data, err := json.Marshal(t)
if err != nil {
return err
}
return os.WriteFile(filename, data, 0644)
}
func (t *Todos) validateIndex(index int) error {
if index <= 0 || index > len(*t) {
return errors.New("invalid index")
}
return nil
}
func (t *Todos) Print() {
table := simpletable.New()
table.Header = &simpletable.Header{
Cells: []*simpletable.Cell{
{Align: simpletable.AlignCenter, Text: "#"},
{Align: simpletable.AlignCenter, Text: "TASK"},
{Align: simpletable.AlignCenter, Text: "RESULT"},
{Align: simpletable.AlignCenter, Text: "CREATED_AT"},
{Align: simpletable.AlignCenter, Text: "COMPLETED_AT"},
},
}
var cells [][]*simpletable.Cell
for idx, item := range *t {
idx++
task := blue(item.Task)
result := red("no")
completedAt := ""
if item.Result {
task = green(fmt.Sprintf("\u2705 %s", item.Task))
result = green("yes")
if item.Completed != nil {
completedAt = item.Completed.Format(time.RFC822)
}
}
cells = append(cells, []*simpletable.Cell{
{Text: fmt.Sprintf("%d", idx)},
{Text: task},
{Text: result},
{Text: item.Created.Format(time.RFC822)},
{Text: completedAt},
})
}
table.Body = &simpletable.Body{Cells: cells}
table.Footer = &simpletable.Footer{Cells: []*simpletable.Cell{
{Align: simpletable.AlignCenter, Span: 5, Text: red(fmt.Sprintf("You have %d pending Tasks", t.CountPending()))},
}}
table.SetStyle(simpletable.StyleUnicode)
table.Println()
}
func (t *Todos) CountPending() int {
total := 0
for _, item := range *t {
if !item.Result {
total++
}
}
return total
}