diff options
| -rw-r--r-- | todolist/app.go | 2 | ||||
| -rw-r--r-- | todolist/filter.go | 53 | ||||
| -rw-r--r-- | todolist/grouper.go | 25 | ||||
| -rw-r--r-- | todolist/util.go | 29 |
4 files changed, 91 insertions, 18 deletions
diff --git a/todolist/app.go b/todolist/app.go index 2af3361..af1b367 100644 --- a/todolist/app.go +++ b/todolist/app.go @@ -109,7 +109,7 @@ func (a *App) getGroups(input string, todos []Todo) *GroupedTodos { if contextRegex.MatchString(input) { grouped = grouper.GroupByContext(todos) } else if projectRegex.MatchString(input) { - grouped = grouper.GroupByContext(todos) + grouped = grouper.GroupByProject(todos) } else { grouped = grouper.GroupByNothing(todos) } diff --git a/todolist/filter.go b/todolist/filter.go index eb4dbd7..39e8fbf 100644 --- a/todolist/filter.go +++ b/todolist/filter.go @@ -12,9 +12,28 @@ func NewFilter(todos []Todo) *TodoFilter { func (f *TodoFilter) Filter(input string) []Todo { f.Todos = f.filterArchived(input) + + if f.isFilteringByProjects(input) { + f.Todos = f.filterProjects(input) + } + + if f.isFilteringByContexts(input) { + f.Todos = f.filterContexts(input) + } + return f.Todos } +func (t *TodoFilter) isFilteringByProjects(input string) bool { + parser := &Parser{} + return len(parser.Projects(input)) > 0 +} + +func (t *TodoFilter) isFilteringByContexts(input string) bool { + parser := &Parser{} + return len(parser.Contexts(input)) > 0 +} + func (f *TodoFilter) filterArchived(input string) []Todo { r, _ := regexp.Compile(`l archived$`) if r.MatchString(input) { @@ -24,6 +43,40 @@ func (f *TodoFilter) filterArchived(input string) []Todo { } } +func (f *TodoFilter) filterProjects(input string) []Todo { + parser := &Parser{} + projects := parser.Projects(input) + var ret []Todo + + for _, todo := range f.Todos { + for _, todoProject := range todo.Projects { + for _, project := range projects { + if project == todoProject { + ret = AddTodoIfNotThere(ret, todo) + } + } + } + } + return ret +} + +func (f *TodoFilter) filterContexts(input string) []Todo { + parser := &Parser{} + contexts := parser.Contexts(input) + var ret []Todo + + for _, todo := range f.Todos { + for _, todoContext := range todo.Contexts { + for _, context := range contexts { + if context == todoContext { + ret = AddTodoIfNotThere(ret, todo) + } + } + } + } + return ret +} + func (f *TodoFilter) getArchived() []Todo { var ret []Todo for _, todo := range f.Todos { diff --git a/todolist/grouper.go b/todolist/grouper.go index 25547bf..2d13391 100644 --- a/todolist/grouper.go +++ b/todolist/grouper.go @@ -12,13 +12,16 @@ func (g *Grouper) GroupByContext(todos []Todo) *GroupedTodos { allContexts := []string{} for _, todo := range todos { - allContexts = addIfNotThere(allContexts, todo.Contexts) + allContexts = AddIfNotThere(allContexts, todo.Contexts) } for _, todo := range todos { for _, context := range todo.Contexts { groups[context] = append(groups[context], todo) } + if len(todo.Contexts) == 0 { + groups["No contexts"] = append(groups["No contexts"], todo) + } } return &GroupedTodos{Groups: groups} @@ -30,13 +33,16 @@ func (g *Grouper) GroupByProject(todos []Todo) *GroupedTodos { allProjects := []string{} for _, todo := range todos { - allProjects = addIfNotThere(allProjects, todo.Projects) + allProjects = AddIfNotThere(allProjects, todo.Projects) } for _, todo := range todos { for _, project := range todo.Projects { groups[project] = append(groups[project], todo) } + if len(todo.Projects) == 0 { + groups["No projects"] = append(groups["No projects"], todo) + } } return &GroupedTodos{Groups: groups} } @@ -46,18 +52,3 @@ func (g *Grouper) GroupByNothing(todos []Todo) *GroupedTodos { groups["all"] = todos return &GroupedTodos{Groups: groups} } - -func addIfNotThere(arr []string, items []string) []string { - for _, item := range items { - there := false - for _, arrItem := range arr { - if item == arrItem { - there = true - } - } - if !there { - arr = append(arr, item) - } - } - return arr -} diff --git a/todolist/util.go b/todolist/util.go new file mode 100644 index 0000000..4f5371d --- /dev/null +++ b/todolist/util.go @@ -0,0 +1,29 @@ +package todolist + +func AddIfNotThere(arr []string, items []string) []string { + for _, item := range items { + there := false + for _, arrItem := range arr { + if item == arrItem { + there = true + } + } + if !there { + arr = append(arr, item) + } + } + return arr +} + +func AddTodoIfNotThere(arr []Todo, item Todo) []Todo { + there := false + for _, arrItem := range arr { + if item.Id == arrItem.Id { + there = true + } + } + if !there { + arr = append(arr, item) + } + return arr +} |
