From 08deecf1954f6cf748812057ba5cdd9a0425bfc9 Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Tue, 3 Oct 2017 07:20:02 -0400 Subject: 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. --- todolist/app.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'todolist/app.go') 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) { -- cgit v1.3 From 6193b27cd7c71bcff5b5dd0cafb4f04172a73206 Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Tue, 3 Oct 2017 07:25:44 -0400 Subject: false to true --- todolist/app.go | 2 +- todolist/memory_printer.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'todolist/app.go') diff --git a/todolist/app.go b/todolist/app.go index ad02569..5703ace 100644 --- a/todolist/app.go +++ b/todolist/app.go @@ -184,7 +184,7 @@ func (a *App) HandleNotes(input string) { } else if parser.ParseShowNote(todo, input) { groups := map[string][]*Todo{} groups[""] = append(groups[""], todo) - a.Printer.Print(&GroupedTodos{Groups: groups}, false) + a.Printer.Print(&GroupedTodos{Groups: groups}, true) return } a.Save() diff --git a/todolist/memory_printer.go b/todolist/memory_printer.go index 62eaa83..d9c504f 100644 --- a/todolist/memory_printer.go +++ b/todolist/memory_printer.go @@ -5,5 +5,5 @@ type MemoryPrinter struct { } func (m *MemoryPrinter) Print(groupedTodos *GroupedTodos, printNotes bool) { - f.Groups = groupedTodos + m.Groups = groupedTodos } -- cgit v1.3