aboutsummaryrefslogtreecommitdiffstats
path: root/todolist/formatter.go
diff options
context:
space:
mode:
authorGrant Ammons <gammons@gmail.com>2016-04-30 08:45:49 -0400
committerGrant Ammons <gammons@gmail.com>2016-04-30 08:45:49 -0400
commitef9427646b9464d1f8c376f75741e737117a1a83 (patch)
treeba3912135e071cd2c7b77e746126cc5eae67ccfb /todolist/formatter.go
parent695c60e3f58d831e4b05aa35114adff7e1c46d23 (diff)
Handle parsing dates, and printing them
Diffstat (limited to 'todolist/formatter.go')
-rw-r--r--todolist/formatter.go61
1 files changed, 55 insertions, 6 deletions
diff --git a/todolist/formatter.go b/todolist/formatter.go
index 77006e0..60517f1 100644
--- a/todolist/formatter.go
+++ b/todolist/formatter.go
@@ -7,6 +7,7 @@ import (
"strconv"
"strings"
"text/tabwriter"
+ "time"
"github.com/fatih/color"
)
@@ -24,23 +25,71 @@ func NewFormatter(todos *GroupedTodos) *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.printTodo(todo)
}
}
f.Writer.Flush()
}
+func (f *Formatter) printTodo(todo Todo) {
+ yellow := color.New(color.FgYellow).SprintFunc()
+ fmt.Fprintf(f.Writer, " \t%s\t%s\t%s\t%s\t\n",
+ yellow(strconv.Itoa(todo.Id)),
+ f.formatCompleted(todo.Completed),
+ f.formatDue(todo.Due),
+ f.formatSubject(todo.Subject))
+}
+
+func (f *Formatter) formatDue(due string) string {
+ if due == "" {
+ return ""
+ }
+ dueTime, err := time.Parse("2006-01-02", due)
+
+ if err != nil {
+ panic(err)
+ }
+
+ blue := color.New(color.FgBlue).SprintFunc()
+ red := color.New(color.FgRed).SprintFunc()
+
+ if isToday(dueTime) {
+ return blue("today")
+ } else if isTomorrow(dueTime) {
+ return blue("tomorrow")
+ } else if isPastDue(dueTime) {
+ return red(dueTime.Format("Mon Jan 2"))
+ } else {
+ return blue(dueTime.Format("Mon Jan 2"))
+ }
+}
+
+func isToday(t time.Time) bool {
+ nowYear, nowMonth, nowDay := time.Now().Date()
+ timeYear, timeMonth, timeDay := t.Date()
+ return nowYear == timeYear &&
+ nowMonth == timeMonth &&
+ nowDay == timeDay
+}
+
+func isTomorrow(t time.Time) bool {
+ nowYear, nowMonth, nowDay := time.Now().AddDate(0, 0, 1).Date()
+ timeYear, timeMonth, timeDay := t.Date()
+ return nowYear == timeYear &&
+ nowMonth == timeMonth &&
+ nowDay == timeDay
+}
+
+func isPastDue(t time.Time) bool {
+ return time.Now().After(t)
+}
+
func (f *Formatter) formatSubject(subject string) string {
red := color.New(color.FgRed).SprintFunc()
magenta := color.New(color.FgMagenta).SprintFunc()