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
|
package todolist
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"text/tabwriter"
"github.com/fatih/color"
)
type Formatter struct {
GroupedTodos *GroupedTodos
Writer *tabwriter.Writer
}
func NewFormatter(todos *GroupedTodos) *Formatter {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 0, '\t', 0)
formatter := &Formatter{GroupedTodos: todos, Writer: w}
return formatter
}
func (f *Formatter) Print() {
yellow := color.New(color.FgYellow).SprintFunc()
cyan := color.New(color.FgCyan).SprintFunc()
for key, todos := range f.GroupedTodos.Groups {
fmt.Fprintf(f.Writer, "\n \t%s\n", cyan(key))
for _, todo := range todos {
fmt.Fprintf(f.Writer, " \t%s\t%s\t%s\t\n",
yellow(strconv.Itoa(todo.Id)),
f.formatCompleted(todo.Completed),
f.formatSubject(todo.Subject))
}
}
f.Writer.Flush()
}
func (f *Formatter) formatSubject(subject string) string {
red := color.New(color.FgRed).SprintFunc()
magenta := color.New(color.FgMagenta).SprintFunc()
splitted := strings.Split(subject, " ")
projectRegex, _ := regexp.Compile(`\+\w+`)
contextRegex, _ := regexp.Compile(`\@\w+`)
coloredWords := []string{}
for _, word := range splitted {
if projectRegex.MatchString(word) {
coloredWords = append(coloredWords, magenta(word))
} else if contextRegex.MatchString(word) {
coloredWords = append(coloredWords, red(word))
} else {
coloredWords = append(coloredWords, word)
}
}
return strings.Join(coloredWords, " ")
}
func (f *Formatter) formatCompleted(completed bool) string {
if completed {
return "[x]"
} else {
return "[ ]"
}
}
|