-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasky.go
155 lines (120 loc) · 2.79 KB
/
tasky.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
package tasky
import (
"encoding/json"
"errors"
"fmt"
"os"
"time"
"github.com/alexeyco/simpletable"
)
type item struct {
Task string
Done bool
CreatedAt time.Time
CompletedAt time.Time
}
type Todos []item
func (t *Todos) Add(task string) {
todo := item{
Task: task,
Done: false,
CreatedAt: time.Now(),
CompletedAt: time.Time{},
}
*t = append(*t, todo)
}
func (t *Todos) Complete(index int) error {
if index <= 0 || index > len(*t) {
return errors.New("invalid index")
}
(*t)[index-1].CompletedAt = time.Now()
(*t)[index-1].Done = true
return nil
}
func (t *Todos) Edit(index int, newTask string) error {
if index <= 0 || index > len(*t) {
return errors.New("invalid index")
}
(*t)[index-1].Task = newTask
return nil
}
func (t *Todos) Delete(index int) error {
if index <= 0 || index > len(*t) {
return errors.New("invalid index")
}
*t = append((*t)[:index-1], (*t)[index:]...)
return nil
}
func (t *Todos) Load(filename string) error {
// file, err := os.ReadFile(filename)
homeDir, err := os.UserHomeDir()
homeDir = homeDir + "/" + filename
file, err := os.ReadFile(homeDir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}
if len(file) == 0 {
return nil
}
err = json.Unmarshal(file, t)
if err != nil {
return err
}
return nil
}
func (t *Todos) Store(filename string) error {
data, err := json.Marshal(t)
if err != nil {
return err
}
homeDir, err := os.UserHomeDir()
homeDir = homeDir + "/" + filename
return os.WriteFile(homeDir, data, 0644)
}
func (t *Todos) Print() {
table := simpletable.New()
table.Header = &simpletable.Header{
Cells: []*simpletable.Cell{
{Align: simpletable.AlignCenter, Text: "#"},
{Align: simpletable.AlignCenter, Text: "Tasks"},
{Align: simpletable.AlignCenter, Text: "State"},
{Align: simpletable.AlignRight, Text: "Created At"},
{Align: simpletable.AlignRight, Text: "Completed At"},
},
}
var cells [][]*simpletable.Cell
for index, item := range *t {
index++
task := blue(item.Task)
done := "❌"
if item.Done {
task = green(fmt.Sprintf("%s", item.Task))
done = green("✅")
}
cells = append(cells, []*simpletable.Cell{
{Text: fmt.Sprintf("%d", index)},
{Text: task},
{Text: done},
{Text: item.CreatedAt.Format(time.RFC822)},
{Text: item.CompletedAt.Format(time.RFC822)},
})
}
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.Done {
total++
}
}
return total
}