diff options
| author | Quey-Liang Kao <s101062801@m101.nthu.edu.tw> | 2017-01-21 04:33:46 +0800 |
|---|---|---|
| committer | Quey-Liang Kao <s101062801@m101.nthu.edu.tw> | 2017-01-21 05:08:37 +0800 |
| commit | 0d1eecc168321dbfd56d1fec55c7d6df5eee2c29 (patch) | |
| tree | 98bf878ca64afd082788ad98370fedf34bfa7645 | |
| parent | 994709884e05d803e34578d74893e5e1c20da417 (diff) | |
New Feature: Expanding existing todos
| -rw-r--r-- | todo.go | 6 | ||||
| -rw-r--r-- | todolist/app.go | 28 | ||||
| -rw-r--r-- | todolist/parser.go | 11 |
3 files changed, 45 insertions, 0 deletions
@@ -97,6 +97,10 @@ func usage() { yellow.Println("\ttodo e 33 due none") fmt.Println("\tEdits the todo with 33 and removes the due date\n") + blueBold.Println("\nExpanding existing todos") + yellow.Println("\ttodo ex 39 +final: read physics due mon, do literature report due fri") + fmt.Println("\tRemoves the todo with id 39, and adds following two todos\n") + blueBold.Println("\nDeleting") yellow.Println("\ttodo d 33") fmt.Println("\tDeletes a todo with id 33\n") @@ -125,6 +129,8 @@ func routeInput(command string, input string) { app.ArchiveCompleted() case "e", "edit": app.EditTodoDue(input) + case "ex", "expand": + app.ExpandTodo(input) case "init": app.InitializeRepo() case "web": diff --git a/todolist/app.go b/todolist/app.go index 58b4801..e67605f 100644 --- a/todolist/app.go +++ b/todolist/app.go @@ -4,6 +4,7 @@ import ( "fmt" "regexp" "strconv" + "strings" "time" ) @@ -102,6 +103,33 @@ func (a *App) EditTodoDue(input string) { fmt.Println("Todo due date updated.") } +func (a *App) ExpandTodo(input string) { + a.Load() + id, _ := a.getId(input) + parser := &Parser{} + if id == -1 { + return + } + + commonProject := parser.ExpandProject(input) + todos := strings.LastIndex(input, ":") + if commonProject == "" || len(input) <= todos+1 || todos == -1 { + fmt.Println("I'm expecting a format like \"todolist ex <project>: <todo1>, <todo2>, ...\"") + return + } + + newTodos := strings.Split(input[todos+1:], ",") + + for _, todo := range newTodos { + args := []string{"add +", commonProject, " ", todo} + a.AddTodo(strings.Join(args, "")) + } + + a.TodoList.Delete(id) + a.Save() + fmt.Println("Todo expanded.") +} + func (a *App) ArchiveCompleted() { a.Load() for _, todo := range a.TodoList.Todos() { diff --git a/todolist/parser.go b/todolist/parser.go index 62d864d..3ad3aae 100644 --- a/todolist/parser.go +++ b/todolist/parser.go @@ -39,6 +39,17 @@ func (p *Parser) Subject(input string) string { } } +func (p *Parser) ExpandProject(input string) string { + r, _ := regexp.Compile(`(ex|expand) +\d+ +\+\w+[^:]`) + newProject := r.FindString(input) + if len(newProject) == 0 { + return "" + } + + project := strings.Split(newProject, " ") + return project[len(project)-1] +} + func (p *Parser) Projects(input string) []string { r, _ := regexp.Compile(`\+\w+`) return p.matchWords(input, r) |
