aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--todolist/app.go17
-rw-r--r--todolist/parser.go17
2 files changed, 21 insertions, 13 deletions
diff --git a/todolist/app.go b/todolist/app.go
index 880390e..24ebe4d 100644
--- a/todolist/app.go
+++ b/todolist/app.go
@@ -3,7 +3,6 @@ package todolist
import (
"fmt"
"regexp"
- "strconv"
"strings"
"time"
)
@@ -174,19 +173,13 @@ func (a *App) UnprioritizeTodo(input string) {
}
func (a *App) getId(input string) (int, *Todo) {
- 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.")
+ _, id, _ := Parser{input}.Parse()
+ todo := a.TodoList.FindById(id)
+ if todo == nil {
+ fmt.Println("No such 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 1f070c3..d55ab3b 100644
--- a/todolist/parser.go
+++ b/todolist/parser.go
@@ -9,7 +9,9 @@ import (
"time"
)
-type Parser struct{}
+type Parser struct {
+ input string
+}
func (p *Parser) ParseNewTodo(input string) *Todo {
r, _ := regexp.Compile(`^(add|a)(\\ |) `)
@@ -28,6 +30,19 @@ func (p *Parser) ParseNewTodo(input string) *Todo {
return todo
}
+func (p Parser) Parse() (string, int, string) {
+ input := p.input
+ r := regexp.MustCompile(`(\w+) (\d+) (.*)`)
+ matches := r.FindStringSubmatch(input)
+ id, err := strconv.Atoi(matches[2])
+ if err != nil {
+ fmt.Println("Invalid id.")
+ id = -1
+ }
+
+ return matches[1], id, matches[3]
+}
+
func (p *Parser) Subject(input string) string {
if strings.Contains(input, " due") {
index := strings.LastIndex(input, " due")