diff options
| author | Stuart Skelton <stuarts@broadbean.com> | 2017-08-06 21:40:19 +0100 |
|---|---|---|
| committer | Stuart Skelton <stuarts@broadbean.com> | 2017-08-12 17:58:52 +0100 |
| commit | 2976acd639b57853df315f80172b61ff75b9d7f3 (patch) | |
| tree | 8d2373fff1511f9eb213a30461aa19d34a3b608a /todolist/app.go | |
| parent | 9f62f82025e4dd7fa3a0dfc50bb94704209e608c (diff) | |
Allow for ranged ids in some commands
Diffstat (limited to 'todolist/app.go')
| -rw-r--r-- | todolist/app.go | 72 |
1 files changed, 51 insertions, 21 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.*$`) |
