From 44dd550fdd7e71c1f0edfe3f77a8b9fafab967e8 Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Sun, 24 Apr 2016 08:22:24 -0400 Subject: Beginning on working on due date stuff --- parser.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- parser_test.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- todo.go | 4 +++- 3 files changed, 107 insertions(+), 5 deletions(-) diff --git a/parser.go b/parser.go index bb54b97..a9a2d24 100644 --- a/parser.go +++ b/parser.go @@ -4,6 +4,9 @@ import ( "fmt" "regexp" "strings" + "time" + + "github.com/jinzhu/now" ) type Parser struct{} @@ -12,6 +15,10 @@ func (p *Parser) Parse(input string) *Todo { todo := NewTodo() todo.Subject = p.Subject(input) todo.Projects = p.Projects(input) + todo.Contexts = p.Contexts(input) + if p.hasDue(input) { + todo.Due = p.Due(input) + } return todo } @@ -25,15 +32,58 @@ func (p *Parser) Subject(input string) string { } func (p *Parser) Projects(input string) []string { - r, err := regexp.Compile(`\+\w+`) + r, _ := regexp.Compile(`\+\w+`) + return p.matchWords(input, r) +} + +func (p *Parser) Contexts(input string) []string { + r, err := regexp.Compile(`\@\w+`) if err != nil { - fmt.Printf("There was a problem with the regexp", err) + fmt.Println("regex error", err) + } + return p.matchWords(input, r) +} + +func (p *Parser) hasDue(input string) bool { + r, _ := regexp.Compile(`due \w+$`) + return r.MatchString(input) +} + +func (p *Parser) Due(input string) time.Time { + r, _ := regexp.Compile(`due .*$`) + + res := r.FindString(input) + res = res[4:len(res)] + switch { + case res == "today": + return now.BeginningOfDay() + case res == "tomorrow" || res == "tom": + return now.BeginningOfDay().AddDate(0, 0, 1) + case res == "monday" || res == "mon": + n := now.BeginningOfDay() + return now.New(n).Monday().AddDate(0, 0, 7) + case res == "tuesday" || res == "tue": + n := now.BeginningOfDay() + return now.New(n).Tuesday().AddDate(0, 0, 7) + case res == "wednesday" || res == "wed": + n := now.BeginningOfDay() + return now.New(n).Wednesday().AddDate(0, 0, 7) + case res == "wednesday" || res == "wed": + n := now.BeginningOfDay() + return now.New(n).Wednesday().AddDate(0, 0, 7) + case res == "next week": + n := now.BeginningOfDay() + return now.New(n).Monday().AddDate(0, 0, 7) } + //return now.Parse(input) + return time.Now() +} + +func (p *Parser) matchWords(input string, r *regexp.Regexp) []string { results := r.FindAllString(input, -1) ret := []string{} for _, val := range results { - fmt.Println("Result is ", val) ret = append(ret, val[1:len(val)]) } return ret diff --git a/parser_test.go b/parser_test.go index 9e1c9cf..980aad2 100644 --- a/parser_test.go +++ b/parser_test.go @@ -1,6 +1,12 @@ package todolist -import "testing" +import ( + "fmt" + "testing" + "time" + + "github.com/jinzhu/now" +) func TestParseSubject(t *testing.T) { parser := &Parser{} @@ -31,3 +37,47 @@ func TestParseProjects(t *testing.T) { t.Error("todo.Projects[1] should equal 'proj2' but got", todo.Projects[1]) } } + +func TestParseContexts(t *testing.T) { + parser := &Parser{} + todo := parser.Parse("do this thing with @bob and @mary due tomorrow") + if len(todo.Contexts) != 2 { + t.Error("Expected Projects length to be 2") + } + if todo.Contexts[0] != "bob" { + t.Error("todo.Contexts[0] should equal 'mary' but got", todo.Contexts[0]) + } + if todo.Contexts[1] != "mary" { + t.Error("todo.Contexts[1] should equal 'mary' but got", todo.Contexts[1]) + } +} + +func TestDueToday(t *testing.T) { + parser := &Parser{} + todo := parser.Parse("do this thing with @bob and @mary due today") + if todo.Due != now.BeginningOfDay() { + fmt.Println("Date is different", todo.Due, time.Now()) + } +} + +func TestDueTomorrow(t *testing.T) { + parser := &Parser{} + todo := parser.Parse("do this thing with @bob and @mary due tomorrow") + if todo.Due != now.BeginningOfDay().AddDate(0, 0, 1) { + fmt.Println("Date is different", todo.Due, time.Now()) + } +} + +//func TestDueNextWeek(t *testing.T) { +// parser := &Parser{} +// +// fmt.Println("about to parse") +// todo := parser.Parse("do this thing with @bob and @mary due next week") +// fmt.Println(todo.Due) +//} + +func TestDueMonday(t *testing.T) { + parser := &Parser{} + todo := parser.Parse("do this thing with @bob and @mary due mon") + fmt.Println(todo.Due) +} diff --git a/todo.go b/todo.go index 739863c..f918b1e 100644 --- a/todo.go +++ b/todo.go @@ -1,11 +1,13 @@ package todolist +import "time" + type Todo struct { Id string Subject string Projects []string Contexts []string - Due string + Due time.Time Completed bool Archived bool } -- cgit v1.3