aboutsummaryrefslogtreecommitdiffstats
path: root/todolist/formatter.go
diff options
context:
space:
mode:
authorGrant Ammons <gammons@gmail.com>2016-04-24 19:58:53 -0400
committerGrant Ammons <gammons@gmail.com>2016-04-24 19:58:53 -0400
commitdde4f5330c2126af0e3ec17e4098653c6f551426 (patch)
tree6e3fa6b61f2e5b2d19463c7467808e44e87b290b /todolist/formatter.go
parent51770b80db0fe299c69bef5060662178f67eb714 (diff)
Bring in the app and router
Diffstat (limited to 'todolist/formatter.go')
-rw-r--r--todolist/formatter.go67
1 files changed, 67 insertions, 0 deletions
diff --git a/todolist/formatter.go b/todolist/formatter.go
new file mode 100644
index 0000000..b2b9816
--- /dev/null
+++ b/todolist/formatter.go
@@ -0,0 +1,67 @@
+package todolist
+
+import (
+ "fmt"
+ "os"
+ "regexp"
+ "strconv"
+ "strings"
+ "text/tabwriter"
+
+ "github.com/fatih/color"
+)
+
+type Formatter struct {
+ Todos []Todo
+ Writer *tabwriter.Writer
+}
+
+func NewFormatter(todos []Todo) *Formatter {
+ w := new(tabwriter.Writer)
+ w.Init(os.Stdout, 0, 8, 0, '\t', 0)
+ formatter := &Formatter{Todos: todos, Writer: w}
+ return formatter
+}
+
+func (f *Formatter) Print() {
+ for _, todo := range f.Todos {
+ yellow := color.New(color.FgYellow).SprintFunc()
+
+ 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 "[ ]"
+ }
+}