aboutsummaryrefslogtreecommitdiffstats
path: root/todolist
diff options
context:
space:
mode:
Diffstat (limited to 'todolist')
-rw-r--r--todolist/app.go72
-rw-r--r--todolist/app_test.go18
-rw-r--r--todolist/todo_list.go110
3 files changed, 142 insertions, 58 deletions
diff --git a/todolist/app.go b/todolist/app.go
index 367cc2f..5045fdf 100644
--- a/todolist/app.go
+++ b/todolist/app.go
@@ -38,55 +38,55 @@ func (a *App) AddTodo(input string) {
func (a *App) DeleteTodo(input string) {
a.Load()
- id, _ := a.getId(input)
- if id == -1 {
+ ids := a.getIds(input)
+ if len(ids) == 0 {
return
}
- a.TodoList.Delete(id)
+ a.TodoList.Delete(ids...)
a.Save()
fmt.Println("Todo deleted.")
}
func (a *App) CompleteTodo(input string) {
a.Load()
- id, _ := a.getId(input)
- if id == -1 {
+ ids := a.getIds(input)
+ if len(ids) == 0 {
return
}
- a.TodoList.Complete(id)
+ a.TodoList.Complete(ids...)
a.Save()
fmt.Println("Todo completed.")
}
func (a *App) UncompleteTodo(input string) {
a.Load()
- id, _ := a.getId(input)
- if id == -1 {
+ ids := a.getIds(input)
+ if len(ids) == 0 {
return
}
- a.TodoList.Uncomplete(id)
+ a.TodoList.Uncomplete(ids...)
a.Save()
fmt.Println("Todo uncompleted.")
}
func (a *App) ArchiveTodo(input string) {
a.Load()
- id, _ := a.getId(input)
- if id == -1 {
+ ids := a.getIds(input)
+ if len(ids) == 0 {
return
}
- a.TodoList.Archive(id)
+ a.TodoList.Archive(ids...)
a.Save()
fmt.Println("Todo archived.")
}
func (a *App) UnarchiveTodo(input string) {
a.Load()
- id, _ := a.getId(input)
- if id == -1 {
+ ids := a.getIds(input)
+ if len(ids) == 0 {
return
}
- a.TodoList.Unarchive(id)
+ a.TodoList.Unarchive(ids...)
a.Save()
fmt.Println("Todo unarchived.")
}
@@ -154,22 +154,22 @@ func (a *App) ListTodos(input string) {
func (a *App) PrioritizeTodo(input string) {
a.Load()
- id, _ := a.getId(input)
- if id == -1 {
+ ids := a.getIds(input)
+ if len(ids) == 0 {
return
}
- a.TodoList.Prioritize(id)
+ a.TodoList.Prioritize(ids...)
a.Save()
fmt.Println("Todo prioritized.")
}
func (a *App) UnprioritizeTodo(input string) {
a.Load()
- id, _ := a.getId(input)
- if id == -1 {
+ ids := a.getIds(input)
+ if len(ids) == 0 {
return
}
- a.TodoList.Unprioritize(id)
+ a.TodoList.Unprioritize(ids...)
a.Save()
fmt.Println("Todo un-prioritized.")
}
@@ -193,6 +193,36 @@ func (a *App) getId(input string) (int, *Todo) {
}
}
+func (a *App) getIds(input string) []int {
+ var ids []int
+ idGroups := strings.Split(input, ",")
+
+ singleNumberRE, _ := regexp.Compile("\\d+")
+ rangeNumberRE, _ := regexp.Compile("(\\d+)-(\\d+)")
+
+ for _, idGroup := range idGroups {
+ if matches := rangeNumberRE.FindStringSubmatch(idGroup); len(matches) > 0 {
+ lowerID, _ := strconv.Atoi(matches[1])
+ upperID, _ := strconv.Atoi(matches[2])
+ if lowerID >= upperID {
+ fmt.Printf("Invalid id group: %s.\n", idGroup)
+ break
+ }
+ for id := lowerID; id <= upperID; id++ {
+ ids = append(ids, id)
+ }
+
+ } else if singleNumberRE.MatchString(idGroup) {
+ id, _ := strconv.Atoi(singleNumberRE.FindString(idGroup))
+ ids = append(ids, id)
+
+ } else {
+ fmt.Printf("Invalid id: %s.\n", idGroup)
+ }
+ }
+ return ids
+}
+
func (a *App) getGroups(input string, todos []*Todo) *GroupedTodos {
grouper := &Grouper{}
contextRegex, _ := regexp.Compile(`by c.*$`)
diff --git a/todolist/app_test.go b/todolist/app_test.go
index f29a1b6..72a0292 100644
--- a/todolist/app_test.go
+++ b/todolist/app_test.go
@@ -108,3 +108,21 @@ func TestListbyContext(t *testing.T) {
}
assert.Equal(true, hasACompletedTodo)
}
+
+func TestGetIds(t *testing.T) {
+ assert := assert.New(t)
+ app := &App{TodoList: &TodoList{}, TodoStore: &MemoryStore{}}
+ // no valid id here
+ assert.Equal(0, len(app.getIds("p")))
+ // one valid value here
+ assert.Equal([]int{6}, app.getIds("6"))
+ // lots of single post numbers
+ assert.Equal([]int{6, 10, 8, 4}, app.getIds("6,10,8,4"))
+ // a correct range
+ assert.Equal([]int{6, 7, 8}, app.getIds("6-8"))
+ // some incorrect ranges
+ assert.Equal(0, len(app.getIds("6-6")))
+ assert.Equal(0, len(app.getIds("8-6")))
+ // some compsite ranges
+ assert.Equal([]int{5, 6, 7, 8, 10, 11, 9}, app.getIds("5,6-8,10-11,9"))
+}
diff --git a/todolist/todo_list.go b/todolist/todo_list.go
index 4623a22..dc3add4 100644
--- a/todolist/todo_list.go
+++ b/todolist/todo_list.go
@@ -15,57 +15,93 @@ func (t *TodoList) Add(todo *Todo) {
t.Data = append(t.Data, todo)
}
-func (t *TodoList) Delete(id int) {
- i := -1
- for index, todo := range t.Data {
- if todo.Id == id {
- i = index
+func (t *TodoList) Delete(ids ...int) {
+ for _, id := range ids {
+ todo := t.FindById(id)
+ if todo == nil {
+ break
+ }
+ i := -1
+ for index, todo := range t.Data {
+ if todo.Id == id {
+ i = index
+ }
}
- }
- t.Data = append(t.Data[:i], t.Data[i+1:]...)
+ t.Data = append(t.Data[:i], t.Data[i+1:]...)
+ }
}
-func (t *TodoList) Complete(id int) {
- todo := t.FindById(id)
- todo.Complete()
- t.Delete(id)
- t.Data = append(t.Data, todo)
+func (t *TodoList) Complete(ids ...int) {
+ for _, id := range ids {
+ todo := t.FindById(id)
+ if todo == nil {
+ continue
+ }
+ todo.Complete()
+ t.Delete(id)
+ t.Data = append(t.Data, todo)
+ }
}
-func (t *TodoList) Uncomplete(id int) {
- todo := t.FindById(id)
- todo.Uncomplete()
- t.Delete(id)
- t.Data = append(t.Data, todo)
+func (t *TodoList) Uncomplete(ids ...int) {
+ for _, id := range ids {
+ todo := t.FindById(id)
+ if todo == nil {
+ continue
+ }
+ todo.Uncomplete()
+ t.Delete(id)
+ t.Data = append(t.Data, todo)
+ }
}
-func (t *TodoList) Archive(id int) {
- todo := t.FindById(id)
- todo.Archive()
- t.Delete(id)
- t.Data = append(t.Data, todo)
+func (t *TodoList) Archive(ids ...int) {
+ for _, id := range ids {
+ todo := t.FindById(id)
+ if todo == nil {
+ continue
+ }
+ todo.Archive()
+ t.Delete(id)
+ t.Data = append(t.Data, todo)
+ }
}
-func (t *TodoList) Unarchive(id int) {
- todo := t.FindById(id)
- todo.Unarchive()
- t.Delete(id)
- t.Data = append(t.Data, todo)
+func (t *TodoList) Unarchive(ids ...int) {
+ for _, id := range ids {
+ todo := t.FindById(id)
+ if todo == nil {
+ continue
+ }
+ todo.Unarchive()
+ t.Delete(id)
+ t.Data = append(t.Data, todo)
+ }
}
-func (t *TodoList) Prioritize(id int) {
- todo := t.FindById(id)
- todo.Prioritize()
- t.Delete(id)
- t.Data = append(t.Data, todo)
+func (t *TodoList) Prioritize(ids ...int) {
+ for _, id := range ids {
+ todo := t.FindById(id)
+ if todo == nil {
+ continue
+ }
+ todo.Prioritize()
+ t.Delete(id)
+ t.Data = append(t.Data, todo)
+ }
}
-func (t *TodoList) Unprioritize(id int) {
- todo := t.FindById(id)
- todo.Unprioritize()
- t.Delete(id)
- t.Data = append(t.Data, todo)
+func (t *TodoList) Unprioritize(ids ...int) {
+ for _, id := range ids {
+ todo := t.FindById(id)
+ if todo == nil {
+ continue
+ }
+ todo.Unprioritize()
+ t.Delete(id)
+ t.Data = append(t.Data, todo)
+ }
}
func (t *TodoList) IndexOf(todoToFind *Todo) int {