-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
format.go
155 lines (140 loc) · 3.82 KB
/
format.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 main
import (
"fmt"
"strings"
"time"
"github.com/fatih/color"
todoist "github.com/sachaos/todoist/lib"
"github.com/urfave/cli/v2"
)
func ColorList() []color.Attribute {
return []color.Attribute{
color.FgHiRed,
color.FgHiGreen,
color.FgHiYellow,
color.FgHiBlue,
color.FgHiMagenta,
color.FgHiCyan,
}
}
func GenerateColorHash(ids []string, colorList []color.Attribute) map[string]color.Attribute {
colorHash := map[string]color.Attribute{}
colorNum := 0
for _, id := range ids {
var colorAttribute color.Attribute
value, ok := colorHash[id]
if ok {
colorAttribute = value
} else {
colorAttribute = colorList[colorNum]
colorHash[id] = colorAttribute
colorNum = colorNum + 1
if colorNum == len(colorList) {
colorNum = 0
}
}
}
return colorHash
}
func IdFormat(carrier todoist.IDCarrier) string {
return color.BlueString(carrier.GetID())
}
func ContentPrefix(store *todoist.Store, item *todoist.Item, depth int, c *cli.Context) (prefix string) {
if c.Bool("indent") {
prefix = prefix + strings.Repeat(" ", depth)
}
if c.Bool("namespace") {
parents := todoist.SearchItemParents(store, item)
for _, parent := range parents {
prefix = prefix + parent.Content + ":"
}
}
return
}
func ContentFormat(item todoist.ContentCarrier) string {
if todoist.HasURL(item) {
return color.New(color.Underline).SprintFunc()(todoist.GetContentTitle(item))
}
return todoist.GetContentTitle(item)
}
func PriorityFormat(priority int) string {
priorityColor := color.New(color.Bold)
var p int
switch priority {
case 1:
p = 4
priorityColor.Add(color.FgBlue).Add(color.BgBlack)
case 2:
p = 3
priorityColor.Add(color.FgHiYellow).Add(color.BgBlack)
case 3:
p = 2
priorityColor.Add(color.FgHiRed).Add(color.BgBlack)
case 4:
p = 1
priorityColor.Add(color.FgWhite).Add(color.BgRed)
}
return priorityColor.SprintFunc()(fmt.Sprintf("p%d", p))
}
func ProjectFormat(id string, store *todoist.Store, projectColorHash map[string]color.Attribute, c *cli.Context) string {
var prefix string
var namePrefix string
project := store.FindProject(id)
if project == nil {
// Accept unknown project ID
return color.New(color.FgCyan).SprintFunc()("Unknown")
}
projectName := project.Name
if c.Bool("project-namespace") {
parentProjects := todoist.SearchProjectParents(store, project)
for _, project := range parentProjects {
namePrefix = namePrefix + project.Name + ":"
}
}
return prefix + color.New(projectColorHash[project.GetID()]).SprintFunc()("#"+namePrefix+projectName)
}
func SectionFormat(id string, store *todoist.Store, c *cli.Context) string {
prefix := ""
sectionName := ""
section := store.FindSection(id)
if section != nil {
prefix = "/"
sectionName = section.Name
}
return prefix + sectionName
}
func dueDateString(dueDate time.Time, allDay bool) string {
if (dueDate == time.Time{}) {
return ""
}
dueDate = dueDate.Local()
if !allDay {
return dueDate.Format(ShortDateTimeFormat)
}
return dueDate.Format(ShortDateFormat)
}
func DueDateFormat(dueDate time.Time, allDay bool) string {
dueDateString := dueDateString(dueDate, allDay)
duration := time.Since(dueDate)
dueDateColor := color.New(color.Bold)
if duration > 0 {
dueDateColor.Add(color.FgWhite).Add(color.BgRed)
} else if duration > -12*time.Hour {
dueDateColor.Add(color.FgHiRed).Add(color.BgBlack)
} else if duration > -24*time.Hour {
dueDateColor.Add(color.FgHiYellow).Add(color.BgBlack)
} else {
dueDateColor.Add(color.FgHiBlue).Add(color.BgBlack)
}
return dueDateColor.SprintFunc()(dueDateString)
}
func completedDateString(completedDate time.Time) string {
if (completedDate == time.Time{}) {
return ""
}
completedDate = completedDate.Local()
return completedDate.Format(ShortDateTimeFormat)
}
func CompletedDateFormat(completedDate time.Time) string {
return completedDateString(completedDate)
}