diff options
| author | Grant Ammons <gammons@gmail.com> | 2017-03-07 08:50:00 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-03-07 08:50:00 -0500 |
| commit | b735a0cc90c6df666f59b7ee302dae27a6a7afeb (patch) | |
| tree | 8dc6d5376e1bf422a1aa693c27f505721741a1f0 | |
| parent | 34931b46ed9ebb06e9555d6e62c98c70dc460d59 (diff) | |
| parent | 5763376a7ef50a116f9585ffd280bbf8e37621d3 (diff) | |
Merge pull request #37 from gammons/garbage-collect
Add garbage collection feature
| -rw-r--r-- | todo.go | 6 | ||||
| -rw-r--r-- | todolist/app.go | 7 | ||||
| -rw-r--r-- | todolist/filter.go | 2 | ||||
| -rw-r--r-- | todolist/todo_list.go | 34 | ||||
| -rw-r--r-- | todolist/todo_list_test.go | 51 |
5 files changed, 97 insertions, 3 deletions
@@ -115,6 +115,10 @@ func usage() { fmt.Println("\tDeletes a todo with id 33\n") fmt.Println("Todolist was lovingly crafted by Grant Ammons (https://twitter.com/gammons).") fmt.Println("For full documentation, please visit http://todolist.site") + + blueBold.Println("\nGarbage Collection") + yellow.Println("\ttodo gc") + fmt.Println("\tDeletes all archived todos.\n") } func routeInput(command string, input string) { @@ -140,6 +144,8 @@ func routeInput(command string, input string) { app.EditTodoDue(input) case "ex", "expand": app.ExpandTodo(input) + case "gc": + app.GarbageCollect() case "p", "prioritize": app.PrioritizeTodo(input) case "up", "unprioritize": diff --git a/todolist/app.go b/todolist/app.go index 495d087..a9145a1 100644 --- a/todolist/app.go +++ b/todolist/app.go @@ -205,6 +205,13 @@ func (a *App) getGroups(input string, todos []*Todo) *GroupedTodos { return grouped } +func (a *App) GarbageCollect() { + a.Load() + a.TodoList.GarbageCollect() + a.Save() + fmt.Println("Garbage collection complete.") +} + func (a *App) Load() error { todos, err := a.TodoStore.Load() if err != nil { diff --git a/todolist/filter.go b/todolist/filter.go index b577e60..8c2a7ad 100644 --- a/todolist/filter.go +++ b/todolist/filter.go @@ -111,7 +111,7 @@ func (f *TodoFilter) getPrioritized() []*Todo { func (f *TodoFilter) getUnarchived() []*Todo { var ret []*Todo for _, todo := range f.Todos { - if todo.Archived == false { + if !todo.Archived { ret = append(ret, todo) } } diff --git a/todolist/todo_list.go b/todolist/todo_list.go index 71484c6..19fe0b7 100644 --- a/todolist/todo_list.go +++ b/todolist/todo_list.go @@ -92,14 +92,32 @@ func (t *TodoList) Todos() []*Todo { return t.Data } -func (t *TodoList) NextId() int { +func (t *TodoList) MaxId() int { maxId := 0 for _, todo := range t.Data { if todo.Id > maxId { maxId = todo.Id } } - return maxId + 1 + return maxId +} + +func (t *TodoList) NextId() int { + var found bool + maxID := t.MaxId() + for i := 1; i <= maxID; i++ { + found = false + for _, todo := range t.Data { + if todo.Id == i { + found = true + break + } + } + if !found { + return i + } + } + return maxID + 1 } func (t *TodoList) FindById(id int) *Todo { @@ -110,3 +128,15 @@ func (t *TodoList) FindById(id int) *Todo { } return nil } + +func (t *TodoList) GarbageCollect() { + var toDelete []*Todo + for _, todo := range t.Data { + if todo.Archived { + toDelete = append(toDelete, todo) + } + } + for _, todo := range toDelete { + t.Delete(todo.Id) + } +} diff --git a/todolist/todo_list_test.go b/todolist/todo_list_test.go index bd0f926..eefd99c 100644 --- a/todolist/todo_list_test.go +++ b/todolist/todo_list_test.go @@ -8,8 +8,42 @@ import ( func TestNextId(t *testing.T) { assert := assert.New(t) + todo := &Todo{Subject: "testing", Completed: false, Archived: false} list := &TodoList{} assert.Equal(1, list.NextId()) + list.Add(todo) + assert.Equal(2, list.NextId()) +} + +func TestNextIdWhenTodoDeleted(t *testing.T) { + assert := assert.New(t) + todo := &Todo{Subject: "testing", Completed: false, Archived: false} + todo2 := &Todo{Subject: "testing2", Completed: false, Archived: false} + todo3 := &Todo{Subject: "testing3", Completed: false, Archived: false} + list := &TodoList{} + + list.Add(todo) + list.Add(todo2) + list.Add(todo3) + + list.Delete(2) + assert.Equal(2, list.NextId()) + list.Add(todo2) + assert.Equal(4, list.NextId()) + list.Delete(1) + assert.Equal(1, list.NextId()) +} + +func TestMaxId(t *testing.T) { + assert := assert.New(t) + todo := &Todo{Subject: "testing", Completed: false, Archived: false} + todo2 := &Todo{Subject: "testing 2", Completed: false, Archived: false} + list := &TodoList{} + assert.Equal(0, list.MaxId()) + list.Add(todo) + assert.Equal(1, list.MaxId()) + list.Add(todo2) + assert.Equal(2, list.MaxId()) } func TestIndexOf(t *testing.T) { @@ -78,6 +112,23 @@ func TestUncomplete(t *testing.T) { assert.Equal(false, list.FindById(2).Completed) } +func TestGarbageCollect(t *testing.T) { + assert := assert.New(t) + list := &TodoList{} + todo := &Todo{Subject: "testing", Completed: false, Archived: true} + todo2 := &Todo{Subject: "testing2", Completed: false, Archived: false} + todo3 := &Todo{Subject: "testing3", Completed: false, Archived: true} + list.Add(todo) + list.Add(todo2) + list.Add(todo3) + + list.GarbageCollect() + + assert.Equal(len(list.Data), 1) + assert.Equal(1, list.NextId()) + assert.Equal(2, list.MaxId()) +} + func TestPrioritizeNotInTodosJson(t *testing.T) { assert := assert.New(t) store := &FileStore{FileLocation: "todos.json"} |
