diff options
| -rw-r--r-- | todolist/app.go | 13 | ||||
| -rw-r--r-- | todolist/memory_printer.go | 9 | ||||
| -rw-r--r-- | todolist/printer.go | 5 | ||||
| -rw-r--r-- | todolist/screen_printer.go (renamed from todolist/formatter.go) | 43 | ||||
| -rw-r--r-- | todolist/util.go | 20 |
5 files changed, 53 insertions, 37 deletions
diff --git a/todolist/app.go b/todolist/app.go index 24e2a61..5703ace 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}, true) 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/memory_printer.go b/todolist/memory_printer.go new file mode 100644 index 0000000..d9c504f --- /dev/null +++ b/todolist/memory_printer.go @@ -0,0 +1,9 @@ +package todolist + +type MemoryPrinter struct { + Groups *GroupedTodos +} + +func (m *MemoryPrinter) Print(groupedTodos *GroupedTodos, printNotes bool) { + m.Groups = groupedTodos +} 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) +} |
