From a716a04c831e4fb23cdd558756d8f393e4ef8d73 Mon Sep 17 00:00:00 2001 From: Quey-Liang Kao Date: Tue, 29 Aug 2017 17:00:18 +0800 Subject: Refactor previous commits Rewrite the `ManipulateNotes` function into `HandleNotes`, which routes the note sub-commands to calls of newly-written `Parse*Note` functions. On successful parse, these functions return true, and false otherwise. Related tests are also refined. --- todolist/parser.go | 95 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 43 deletions(-) (limited to 'todolist/parser.go') diff --git a/todolist/parser.go b/todolist/parser.go index 8f5e180..6ad8007 100644 --- a/todolist/parser.go +++ b/todolist/parser.go @@ -83,57 +83,66 @@ func (p *Parser) Contexts(input string) []string { return p.matchWords(input, r) } -func (p *Parser) ParseNotes(todo *Todo, input string) string { - r, _ := regexp.Compile(`(\w+) \d+\s*(.*)?`) +func (p *Parser) ParseAddNote(todo *Todo, input string) bool { + r, _ := regexp.Compile(`^an\s+\d+\s+(.*)`) matches := r.FindStringSubmatch(input) - switch matches[1] { - case "an": - todo.Notes = append(todo.Notes, matches[2]) - return "add" - - case "n": - groups := map[string][]*Todo{} - groups[""] = append(groups[""], todo) - formatter := NewFormatter(&GroupedTodos{Groups: groups}) - formatter.Print(true) - return "list" - - case "dn": - rmid, err := p.getNoteID(matches[2]) - if err != nil { - return "" - } + if len(matches) != 2 { + return false + } - for id, _ := range todo.Notes { - if id == rmid { - todo.Notes = append(todo.Notes[:rmid], todo.Notes[rmid+1:]...) - return "delete" - } - } - fmt.Println("Could not found note id") - return "" + todo.Notes = append(todo.Notes, matches[1]) + return true +} - case "en": - r1, _ := regexp.Compile(`(\d)+\s+(.*)?`) - tail := r1.FindStringSubmatch(matches[2]) - edid, err := p.getNoteID(tail[1]) - if err != nil { - return "" - } +func (p *Parser) ParseDeleteNote(todo *Todo, input string) bool { + r, _ := regexp.Compile(`^dn\s+\d+\s+(\d+)`) + matches := r.FindStringSubmatch(input) + if len(matches) != 2 { + return false + } + + rmid, err := p.getNoteID(matches[1]) + if err != nil { + return false + } - for id, _ := range todo.Notes { - if id == edid { - todo.Notes[id] = tail[2] - return "edit" - } + for id, _ := range todo.Notes { + if id == rmid { + todo.Notes = append(todo.Notes[:rmid], todo.Notes[rmid+1:]...) + return true } + } + return false +} - fmt.Println("Could not found note id") - return "" +func (p *Parser) ParseEditNote(todo *Todo, input string) bool { + r, _ := regexp.Compile(`^en\s+\d+\s+(\d+)\s+(.*)`) + matches := r.FindStringSubmatch(input) + if len(matches) != 3 { + return false + } + + edid, err := p.getNoteID(matches[1]) + if err != nil { + return false } - fmt.Println("Could not match command or id") - return "" + for id, _ := range todo.Notes { + if id == edid { + todo.Notes[id] = matches[2] + return true + } + } + return false +} + +func (p *Parser) ParseShowNote(todo *Todo, input string) bool { + r, _ := regexp.Compile(`^n\s+\d+`) + matches := r.FindStringSubmatch(input) + if len(matches) != 1 { + return false + } + return true } func (p *Parser) getNoteID(input string) (int, error) { -- cgit v1.3