From c12bbb27b6268d2f3eedc56234a46e54d010e3f2 Mon Sep 17 00:00:00 2001 From: Michael Strùˆder Date: Sat, 29 Apr 2017 16:34:04 +0200 Subject: add command line interface for edit-subject --- todo.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'todo.go') diff --git a/todo.go b/todo.go index f41410d..d091cd3 100644 --- a/todo.go +++ b/todo.go @@ -140,8 +140,10 @@ func routeInput(command string, input string) { app.UnarchiveTodo(input) case "ac": app.ArchiveCompleted() - case "e", "edit": + case "e", "edit", "ed", "edit-date": app.EditTodoDue(input) + case "es", "edit-subject": + app.EditTodoSubject(input) case "ex", "expand": app.ExpandTodo(input) case "gc": -- cgit v1.3 From 1056fecdccbb734dc732a2f795f2591d3c63f951 Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Fri, 5 May 2017 16:51:03 -0400 Subject: WIP on a unified edit This allows a unified edit functionality that supports the following: **`t e 25 due tom`** * Edits the due date * Leaves subject, projects and contexts unchanged **`t e 25 take out the trash with @bob` * Updates the subject, projects and contexts * Leaves the due date unchanged **`t e 25 take out the trash with @bob due tom` * Updates the subject, projects and contexts * Updates the due date **What's missing** * Tests that exercise this functionality --- todo.go | 6 ++---- todolist/app.go | 53 +++++++++++++++++++++-------------------------------- todolist/parser.go | 30 ++++++++++++------------------ 3 files changed, 35 insertions(+), 54 deletions(-) (limited to 'todo.go') diff --git a/todo.go b/todo.go index d091cd3..130fd55 100644 --- a/todo.go +++ b/todo.go @@ -140,10 +140,8 @@ func routeInput(command string, input string) { app.UnarchiveTodo(input) case "ac": app.ArchiveCompleted() - case "e", "edit", "ed", "edit-date": - app.EditTodoDue(input) - case "es", "edit-subject": - app.EditTodoSubject(input) + case "e", "edit": + app.EditTodo(input) case "ex", "expand": app.ExpandTodo(input) case "gc": diff --git a/todolist/app.go b/todolist/app.go index 12cc39d..9fefd8e 100644 --- a/todolist/app.go +++ b/todolist/app.go @@ -3,8 +3,8 @@ package todolist import ( "fmt" "regexp" + "strconv" "strings" - "time" ) type App struct { @@ -91,38 +91,18 @@ func (a *App) UnarchiveTodo(input string) { fmt.Println("Todo unarchived.") } -func (a *App) EditTodoSubject(input string) { +func (a *App) EditTodo(input string) { a.Load() - - p := Parser{input} - _, id, subject := p.Parse() + id, _ := a.getId(input) if id == -1 { return } + parser := &Parser{} - _, todo := a.getId(input) - if todo == nil { - return - } - - todo.Subject = subject - todo.Projects = p.Projects(subject) - todo.Contexts = p.Contexts(subject) - - a.Save() - fmt.Println("Todo subject updated.") -} - -func (a *App) EditTodoDue(input string) { - a.Load() - id, todo := a.getId(input) - if id == -1 { - return + if (parser.ParseEditTodo(a.TodoList.FindById(id), input)) { + a.Save() + fmt.Println("Todo updated.") } - parser := &Parser{} - todo.Due = parser.Due(input, time.Now()) - a.Save() - fmt.Println("Todo due date updated.") } func (a *App) ExpandTodo(input string) { @@ -195,13 +175,22 @@ func (a *App) UnprioritizeTodo(input string) { } func (a *App) getId(input string) (int, *Todo) { - _, id, _ := Parser{input}.Parse() - todo := a.TodoList.FindById(id) - if todo == nil { - fmt.Println("No such id.") + re, _ := regexp.Compile("\\d+") + if re.MatchString(input) { + id, _ := strconv.Atoi(re.FindString(input)) + todo := a.TodoList.FindById(id) + if todo == nil { + fmt.Println("No such id.") + return -1, nil + + } + return id, todo + + } else { + fmt.Println("Invalid id.") return -1, nil + } - return id, todo } func (a *App) getGroups(input string, todos []*Todo) *GroupedTodos { diff --git a/todolist/parser.go b/todolist/parser.go index 3598e12..91f9b6e 100644 --- a/todolist/parser.go +++ b/todolist/parser.go @@ -9,9 +9,7 @@ import ( "time" ) -type Parser struct { - input string -} +type Parser struct {} func (p *Parser) ParseNewTodo(input string) *Todo { r, _ := regexp.Compile(`^(add|a)(\\ |) `) @@ -30,29 +28,25 @@ func (p *Parser) ParseNewTodo(input string) *Todo { return todo } -// Parse accepts user input and splits it into subcommand, the todo id to -// work on and the subject for the subcommand function. -func (p Parser) Parse() (subcommand string, id int, subject string) { +func (p *Parser) ParseEditTodo(todo *Todo, input string) bool { r := regexp.MustCompile(`(\w+)\s+(\d+)(\s+(.*))?`) - matches := r.FindStringSubmatch(p.input) + matches := r.FindStringSubmatch(input) if len(matches) < 3 { fmt.Println("Could match command or id") - return "", -1, "" + return false } - subcommand = matches[1] + subjectOnly := matches[3] - // because of the regexp match, this can never fail - id, err := strconv.Atoi(matches[2]) - if err != nil { - panic(err) + if p.Subject(subjectOnly) != "" { + todo.Subject = p.Subject(subjectOnly) + todo.Projects = p.Projects(subjectOnly) + todo.Contexts = p.Contexts(subjectOnly) } - - if len(matches) == 5 { - subject = matches[4] + if p.hasDue(subjectOnly) { + todo.Due = p.Due(subjectOnly, time.Now()) } - - return + return true } func (p *Parser) Subject(input string) string { -- cgit v1.3