aboutsummaryrefslogtreecommitdiffstats
path: root/todolist/app.go
blob: 6122b6a32fb932e5ca156026c1aeb1b5581a4479 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package todolist

import "regexp"

type App struct {
	TodoStore Store
}

func NewApp() *App {
	app := &App{TodoStore: NewFileStore()}
	app.TodoStore.Load()
	return app
}

func (a *App) ListTodos(input string) {

	grouped := a.getGroups(input)

	formatter := NewFormatter(grouped)
	formatter.Print()
}

func (a *App) getGroups(input string) *GroupedTodos {
	grouper := &Grouper{}
	contextRegex, _ := regexp.Compile(`by c.*$`)
	projectRegex, _ := regexp.Compile(`by p.*$`)

	var grouped *GroupedTodos

	if contextRegex.MatchString(input) {
		grouped = grouper.GroupByContext(a.TodoStore.Todos())
	} else if projectRegex.MatchString(input) {
		grouped = grouper.GroupByContext(a.TodoStore.Todos())
	} else {
		grouped = grouper.GroupByNothing(a.TodoStore.Todos())
	}
	return grouped
}