aboutsummaryrefslogtreecommitdiffstats
path: root/todolist
diff options
context:
space:
mode:
Diffstat (limited to 'todolist')
-rw-r--r--todolist/app.go14
-rw-r--r--todolist/parser.go25
-rw-r--r--todolist/parser_test.go70
3 files changed, 102 insertions, 7 deletions
diff --git a/todolist/app.go b/todolist/app.go
index 880390e..56787d3 100644
--- a/todolist/app.go
+++ b/todolist/app.go
@@ -5,7 +5,6 @@ import (
"regexp"
"strconv"
"strings"
- "time"
)
type App struct {
@@ -92,16 +91,18 @@ func (a *App) UnarchiveTodo(input string) {
fmt.Println("Todo unarchived.")
}
-func (a *App) EditTodoDue(input string) {
+func (a *App) EditTodo(input string) {
a.Load()
id, todo := a.getId(input)
if id == -1 {
return
}
parser := &Parser{}
- todo.Due = parser.Due(input, time.Now())
- a.Save()
- fmt.Println("Todo due date updated.")
+
+ if parser.ParseEditTodo(todo, input) {
+ a.Save()
+ fmt.Println("Todo updated.")
+ }
}
func (a *App) ExpandTodo(input string) {
@@ -181,11 +182,14 @@ func (a *App) getId(input string) (int, *Todo) {
if todo == nil {
fmt.Println("No such id.")
return -1, nil
+
}
return id, todo
+
} else {
fmt.Println("Invalid id.")
return -1, nil
+
}
}
diff --git a/todolist/parser.go b/todolist/parser.go
index 1f070c3..256e782 100644
--- a/todolist/parser.go
+++ b/todolist/parser.go
@@ -28,12 +28,33 @@ func (p *Parser) ParseNewTodo(input string) *Todo {
return todo
}
+func (p *Parser) ParseEditTodo(todo *Todo, input string) bool {
+ r := regexp.MustCompile(`(\w+)\s+(\d+)(\s+(.*))?`)
+ matches := r.FindStringSubmatch(input)
+ if len(matches) < 3 {
+ fmt.Println("Could not match command or id")
+ return false
+ }
+
+ subjectOnly := matches[3]
+
+ if p.Subject(subjectOnly) != "" {
+ todo.Subject = p.Subject(subjectOnly)
+ todo.Projects = p.Projects(subjectOnly)
+ todo.Contexts = p.Contexts(subjectOnly)
+ }
+ if p.hasDue(subjectOnly) {
+ todo.Due = p.Due(subjectOnly, time.Now())
+ }
+ return true
+}
+
func (p *Parser) Subject(input string) string {
if strings.Contains(input, " due") {
index := strings.LastIndex(input, " due")
- return input[0:index]
+ return strings.TrimSpace(input[0:index])
} else {
- return input
+ return strings.TrimSpace(input)
}
}
diff --git a/todolist/parser_test.go b/todolist/parser_test.go
index 8cd6b53..f5cc185 100644
--- a/todolist/parser_test.go
+++ b/todolist/parser_test.go
@@ -169,3 +169,73 @@ func TestDueIntelligentlyChoosesCorrectYear(t *testing.T) {
assert.Equal("2017-01-10", parser.parseArbitraryDate("jan 10", septemberTime))
assert.Equal("2017-01-10", parser.parseArbitraryDate("jan 10", decemberTime))
}
+
+func TestParseEditTodoJustDate(t *testing.T) {
+ assert := assert.New(t)
+ parser := &Parser{}
+ todo := NewTodo()
+ tomorrow := time.Now().AddDate(0, 0, 1).Format("2006-01-02")
+
+ parser.ParseEditTodo(todo, "e 24 due tom")
+
+ assert.Equal(todo.Due, tomorrow)
+}
+
+func TestParseEditTodoJustDateDoesNotEditExistingSubject(t *testing.T) {
+ assert := assert.New(t)
+ parser := &Parser{}
+ todo := NewTodo()
+ todo.Subject = "pick up the trash"
+ tomorrow := time.Now().AddDate(0, 0, 1).Format("2006-01-02")
+
+ parser.ParseEditTodo(todo, "e 24 due tom")
+
+ assert.Equal(todo.Due, tomorrow)
+ assert.Equal(todo.Subject, "pick up the trash")
+}
+
+func TestParseEditTodoJustSubject(t *testing.T) {
+ assert := assert.New(t)
+ parser := &Parser{}
+ todo := &Todo{Subject: "pick up the trash", Due: "2016-11-25"}
+
+ parser.ParseEditTodo(todo, "e 24 changed the todo")
+
+ assert.Equal(todo.Due, "2016-11-25")
+ assert.Equal(todo.Subject, "changed the todo")
+}
+
+func TestParseEditTodoSubjectUpdatesProjectsAndContexts(t *testing.T) {
+ assert := assert.New(t)
+ parser := &Parser{}
+ todo := &Todo{
+ Subject: "pick up the +trash with @dad",
+ Due: "2016-11-25",
+ Projects: []string{"trash"},
+ Contexts: []string{"dad"},
+ }
+
+ parser.ParseEditTodo(todo, "e 24 get the +garbage with @mom")
+
+ assert.Equal(todo.Due, "2016-11-25")
+ assert.Equal(todo.Subject, "get the +garbage with @mom")
+ assert.Equal(todo.Projects, []string{"garbage"})
+ assert.Equal(todo.Contexts, []string{"mom"})
+}
+
+func TestParseEditTodoWithSubjectAndDue(t *testing.T) {
+ assert := assert.New(t)
+ parser := &Parser{}
+ todo := &Todo{
+ Subject: "pick up the +trash with @dad",
+ Due: "2016-11-25",
+ Projects: []string{"trash"},
+ Contexts: []string{"dad"},
+ }
+ tomorrow := time.Now().AddDate(0, 0, 1).Format("2006-01-02")
+
+ parser.ParseEditTodo(todo, "e 24 get the +garbage with @mom due tom")
+
+ assert.Equal(todo.Due, tomorrow)
+ assert.Equal(todo.Subject, "get the +garbage with @mom")
+}