aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorQuey-Liang Kao <s101062801@m101.nthu.edu.tw>2017-08-14 16:34:33 +0800
committerQuey-Liang Kao <s101062801@m101.nthu.edu.tw>2017-08-14 16:34:33 +0800
commit7c24f39a0fc8bb07dc9db4f5fecd3468705b1e37 (patch)
treefef48b2359c4a432cf8bf28c5caa16188dbb53e8
parentc4d6dbdb100bb66db9b184c653617c47a63e91ca (diff)
Implement the "en" feature: Editting notes
-rw-r--r--todo.go4
-rw-r--r--todolist/parser.go30
-rw-r--r--todolist/parser_test.go7
3 files changed, 38 insertions, 3 deletions
diff --git a/todo.go b/todo.go
index 9619bdb..cda6cc3 100644
--- a/todo.go
+++ b/todo.go
@@ -129,6 +129,8 @@ func usage() {
fmt.Println("\tLists notes of the todo with id 12\n")
yellow.Println("\ttodo dn 12 3")
fmt.Println("\tDeletes the 3rd note of the todo with id 12\n")
+ yellow.Println("\ttodo en 12 3 check http://that.web.site")
+ fmt.Println("\tEditing the 3rd note of the todo with id 12 to \"http://that.web.site\" \n")
blueBold.Println("\nGarbage Collection")
yellow.Println("\ttodo gc")
@@ -161,7 +163,7 @@ func routeInput(command string, input string) {
app.EditTodo(input)
case "ex", "expand":
app.ExpandTodo(input)
- case "an", "ln", "dn":
+ case "an", "ln", "dn", "en":
app.ManipulateNotes(input)
case "gc":
app.GarbageCollect()
diff --git a/todolist/parser.go b/todolist/parser.go
index 3883454..42af133 100644
--- a/todolist/parser.go
+++ b/todolist/parser.go
@@ -99,9 +99,8 @@ func (p *Parser) ParseNotes(todo *Todo, input string) string {
return "list"
case "dn":
- rmid, err := strconv.Atoi(matches[2])
+ rmid, err := p.getNoteID(matches[2])
if err != nil {
- fmt.Println("wrong note id")
return ""
}
@@ -113,12 +112,39 @@ func (p *Parser) ParseNotes(todo *Todo, input string) string {
}
fmt.Println("Could not found note id")
return ""
+
+ case "en":
+ r1, _ := regexp.Compile(`(\d)+\s+(.*)?`)
+ tail := r1.FindStringSubmatch(matches[2])
+ edid, err := p.getNoteID(tail[1])
+ if err != nil {
+ return ""
+ }
+
+ for id, _ := range todo.Notes {
+ if id == edid {
+ todo.Notes[id] = tail[2]
+ return "edit"
+ }
+ }
+
+ fmt.Println("Could not found note id")
+ return ""
}
fmt.Println("Could not match command or id")
return ""
}
+func (p *Parser) getNoteID(input string) (int, error) {
+ ret, err := strconv.Atoi(input)
+ if err != nil {
+ fmt.Println("wrong note id")
+ return -1, err
+ }
+ return ret, nil
+}
+
func (p *Parser) hasDue(input string) bool {
r1, _ := regexp.Compile(`due \w+$`)
r2, _ := regexp.Compile(`due \w+ \d+$`)
diff --git a/todolist/parser_test.go b/todolist/parser_test.go
index e5c7007..2c4fd25 100644
--- a/todolist/parser_test.go
+++ b/todolist/parser_test.go
@@ -85,6 +85,13 @@ func TestParseNotes(t *testing.T) {
t.Error("Expected note 1 to be 'www.google.com' but got", todo.Notes[0])
}
+ if parser.ParseNotes(todo, "en 1 0 www.duckduckgo.com") != "edit" {
+ t.Error("Expected Notes to be editted")
+ }
+ if todo.Notes[0] != "www.duckduckgo.com" {
+ t.Error("Expected note 1 to be 'www.duckduckgo.com' but got", todo.Notes[0])
+ }
+
if parser.ParseNotes(todo, "dn 1 0") != "delete" {
t.Error("Expected Notes to be deleted")
}