-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.go
112 lines (98 loc) · 2.82 KB
/
task.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
package main
import (
"fmt"
"strings"
"time"
"github.com/mgutz/ansi"
)
// Task represents what we have to do
type Task struct {
ID int `json:"id"` // Id of the task
Created int64 `json:"created"` // timestamp when it has been created
Text string `json:"text"` // Text description of the Task
Status string `json:"status"` // Status of the task
Tags []string `json:"tags"` // Tags of the task
}
// TaskByTime sort by timestamp
type TaskByTime []Task
func (t TaskByTime) Len() int { return len(t) }
func (t TaskByTime) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t TaskByTime) Less(i, j int) bool { return t[i].Created < t[j].Created }
// AnsiString provide string with ansi color escapes
func (t Task) AnsiString() string {
date := time.Unix(t.Created/1000, 0)
var ansiStatus string
if t.Status == "pending" || t.Status == "open" {
ansiStatus = ansi.Color(t.Status, "94")
} else if t.Status == "done" {
ansiStatus = ansi.Color(t.Status, "90")
}
return fmt.Sprintf("%d %s %s\n (%s) %s", t.ID, ansiStatus, ansi.Color(t.Text, "80"), ansi.Color(strings.Join(t.Tags, ", "), "90"), date.Format("02-01-2006"))
}
// JSONDb is a Task database in json
type JSONDb struct {
Tags []string // List of existing tags that can be used for a task
Tasks []Task // List of tasks
IDGen int // used to generate a new id
}
// GenerateID generate a new id incrementing IDGen
func (db *JSONDb) GenerateID() int {
db.IDGen++
return db.IDGen
}
func findByID(tasks []Task, id int) *Task {
for i, task := range tasks {
if task.ID == id {
return &tasks[i]
}
}
return nil
}
// FilterByTags return tasks list that have one the tags
func FilterByTags(tasks []Task, tags []string) []Task {
var result []Task
for _, task := range tasks {
if tags[0] == "" || containsOne(tags, task.Tags) {
result = append(result, task)
}
}
return result
}
// FilterByStatus return tasks list that have one of the status
func FilterByStatus(tasks []Task, status []string) []Task {
var result []Task
for _, task := range tasks {
if status[0] == "" || contains(status, task.Status) {
result = append(result, task)
}
}
return result
}
// FilterByText return tasks list that have contains text
func FilterByText(tasks []Task, text string) []Task {
var result []Task
for _, task := range tasks {
if strings.Index(strings.ToLower(task.Text), strings.ToLower(text)) != -1 {
result = append(result, task)
}
}
return result
}
func containsOne(strs1 []string, strs2 []string) bool {
for _, str1 := range strs1 {
for _, str2 := range strs2 {
if strings.ToLower(str1) == strings.ToLower(str2) {
return true
}
}
}
return false
}
func contains(strs []string, s string) bool {
for _, str := range strs {
if strings.ToLower(str) == strings.ToLower(s) {
return true
}
}
return false
}