aboutsummaryrefslogtreecommitdiffstats
path: root/todolist
diff options
context:
space:
mode:
authorGrant Ammons <gammons@gmail.com>2017-10-03 07:20:02 -0400
committerGrant Ammons <gammons@gmail.com>2017-10-03 07:20:41 -0400
commit08deecf1954f6cf748812057ba5cdd9a0425bfc9 (patch)
tree722ccf48c7a204af8d508c2f2757f673f771915b /todolist
parent0840e7a147d8594acec63fd1ce8aba9e15472839 (diff)
Refactor the formatter to an interface and rename it
This change refactors the formatter to be an interface called Printer. `NewApp` will use the functionality of `ScreenPrinter`, which is what I refactored the old `Formatter` class into. There is a new class the implements the `Printer` interface called `MemoryPrinter`, which will simply store the `groups` as they were passed to it. This will allow tests in `app_test.go` to become much more powerful, since `App` no longer needs to be tightly coupled to printing to the screen.
Diffstat (limited to 'todolist')
-rw-r--r--todolist/app.go13
-rw-r--r--todolist/printer.go5
-rw-r--r--todolist/screen_printer.go (renamed from todolist/formatter.go)43
-rw-r--r--todolist/util.go20
4 files changed, 44 insertions, 37 deletions
diff --git a/todolist/app.go b/todolist/app.go
index 24e2a61..ad02569 100644
--- a/todolist/app.go
+++ b/todolist/app.go
@@ -9,11 +9,16 @@ import (
type App struct {
TodoStore Store
+ Printer Printer
TodoList *TodoList
}
func NewApp() *App {
- app := &App{TodoList: &TodoList{}, TodoStore: NewFileStore()}
+ app := &App{
+ TodoList: &TodoList{},
+ Printer: NewScreenPrinter(),
+ TodoStore: NewFileStore(),
+ }
return app
}
@@ -179,8 +184,7 @@ func (a *App) HandleNotes(input string) {
} else if parser.ParseShowNote(todo, input) {
groups := map[string][]*Todo{}
groups[""] = append(groups[""], todo)
- formatter := NewFormatter(&GroupedTodos{Groups: groups})
- formatter.Print(true)
+ a.Printer.Print(&GroupedTodos{Groups: groups}, false)
return
}
a.Save()
@@ -202,9 +206,8 @@ func (a *App) ListTodos(input string) {
filtered := NewFilter(a.TodoList.Todos()).Filter(input)
grouped := a.getGroups(input, filtered)
- formatter := NewFormatter(grouped)
re, _ := regexp.Compile(`^ln`)
- formatter.Print(re.MatchString(input))
+ a.Printer.Print(grouped, re.MatchString(input))
}
func (a *App) PrioritizeTodo(input string) {
diff --git a/todolist/printer.go b/todolist/printer.go
new file mode 100644
index 0000000..805cc6e
--- /dev/null
+++ b/todolist/printer.go
@@ -0,0 +1,5 @@
+package todolist
+
+type Printer interface {
+ Print(*GroupedTodos, bool)
+}
diff --git a/todolist/formatter.go b/todolist/screen_printer.go
index e7863db..eab0c39 100644
--- a/todolist/formatter.go
+++ b/todolist/screen_printer.go
@@ -13,30 +13,29 @@ import (
"github.com/fatih/color"
)
-type Formatter struct {
- GroupedTodos *GroupedTodos
- Writer *tabwriter.Writer
+type ScreenPrinter struct {
+ Writer *tabwriter.Writer
}
-func NewFormatter(todos *GroupedTodos) *Formatter {
+func NewScreenPrinter() *ScreenPrinter {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 0, '\t', 0)
- formatter := &Formatter{GroupedTodos: todos, Writer: w}
+ formatter := &ScreenPrinter{Writer: w}
return formatter
}
-func (f *Formatter) Print(printNotes bool) {
+func (f *ScreenPrinter) Print(groupedTodos *GroupedTodos, printNotes bool) {
cyan := color.New(color.FgCyan).SprintFunc()
var keys []string
- for key := range f.GroupedTodos.Groups {
+ for key := range groupedTodos.Groups {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
fmt.Fprintf(f.Writer, "\n %s\n", cyan(key))
- for _, todo := range f.GroupedTodos.Groups[key] {
+ for _, todo := range groupedTodos.Groups[key] {
f.printTodo(todo)
if printNotes {
for nid, note := range todo.Notes {
@@ -49,7 +48,7 @@ func (f *Formatter) Print(printNotes bool) {
f.Writer.Flush()
}
-func (f *Formatter) printTodo(todo *Todo) {
+func (f *ScreenPrinter) printTodo(todo *Todo) {
yellow := color.New(color.FgYellow)
if todo.IsPriority {
yellow.Add(color.Bold, color.Italic)
@@ -61,7 +60,7 @@ func (f *Formatter) printTodo(todo *Todo) {
f.formatSubject(todo.Subject, todo.IsPriority))
}
-func (f *Formatter) formatDue(due string, isPriority bool) string {
+func (f *ScreenPrinter) formatDue(due string, isPriority bool) string {
blue := color.New(color.FgBlue)
red := color.New(color.FgRed)
@@ -92,27 +91,7 @@ func (f *Formatter) formatDue(due string, isPriority bool) string {
}
}
-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, isPriority bool) string {
+func (f *ScreenPrinter) formatSubject(subject string, isPriority bool) string {
red := color.New(color.FgRed)
magenta := color.New(color.FgMagenta)
@@ -143,7 +122,7 @@ func (f *Formatter) formatSubject(subject string, isPriority bool) string {
}
-func (f *Formatter) formatCompleted(completed bool) string {
+func (f *ScreenPrinter) formatCompleted(completed bool) string {
if completed {
return "[x]"
} else {
diff --git a/todolist/util.go b/todolist/util.go
index ff3dbba..6fa93f3 100644
--- a/todolist/util.go
+++ b/todolist/util.go
@@ -59,3 +59,23 @@ func pluralize(count int, singular, plural string) string {
}
return singular
}
+
+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)
+}