From 0d1eecc168321dbfd56d1fec55c7d6df5eee2c29 Mon Sep 17 00:00:00 2001 From: Quey-Liang Kao Date: Sat, 21 Jan 2017 04:33:46 +0800 Subject: New Feature: Expanding existing todos --- todolist/app.go | 28 ++++++++++++++++++++++++++++ todolist/parser.go | 11 +++++++++++ 2 files changed, 39 insertions(+) (limited to 'todolist') 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 : , , ...\"") + 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) -- cgit v1.3 From 206a28076422758102fb7df322b218dfe4248deb Mon Sep 17 00:00:00 2001 From: Quey-Liang Kao Date: Sat, 21 Jan 2017 12:43:43 +0800 Subject: Fix a bug and add some tests --- todolist/app.go | 2 +- todolist/parser_test.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) (limited to 'todolist') diff --git a/todolist/app.go b/todolist/app.go index e67605f..5f26d45 100644 --- a/todolist/app.go +++ b/todolist/app.go @@ -121,7 +121,7 @@ func (a *App) ExpandTodo(input string) { newTodos := strings.Split(input[todos+1:], ",") for _, todo := range newTodos { - args := []string{"add +", commonProject, " ", todo} + args := []string{"add ", commonProject, " ", todo} a.AddTodo(strings.Join(args, "")) } diff --git a/todolist/parser_test.go b/todolist/parser_test.go index 83c0602..15307cc 100644 --- a/todolist/parser_test.go +++ b/todolist/parser_test.go @@ -26,6 +26,19 @@ func TestParseSubjectWithDue(t *testing.T) { } } +func TestParseExpandProjects(t *testing.T) { + assert := assert.New(t) + parser := &Parser{} + correctFormat := parser.ExpandProject("ex 113 +meeting: figures, slides, coffee, suger") + assert.Equal("+meeting", correctFormat) + wrongFormat1 := parser.ExpandProject("ex 114 +meeting figures, slides, coffee, suger") + assert.Equal("", wrongFormat1) + wrongFormat2 := parser.ExpandProject("ex 115 meeting: figures, slides, coffee, suger") + assert.Equal("", wrongFormat2) + wrongFormat3 := parser.ExpandProject("ex 116 meeting figures, slides, coffee, suger") + assert.Equal("", wrongFormat3) +} + func TestParseProjects(t *testing.T) { parser := &Parser{} todo := parser.ParseNewTodo("do this thing +proj1 +proj2 due tomorrow") -- cgit v1.3