go-todotxt/todo.go

282 lines
8.2 KiB
Go

package todotxt
import (
"fmt"
"regexp"
"sort"
"strings"
"time"
)
var (
// DateLayout is used for formatting time.Time into todo.txt date format and vice-versa.
DateLayout = "2006-01-02"
priorityRx = regexp.MustCompile(`^(x|x \d{4}-\d{2}-\d{2}|)\s*\(([A-Z])\)\s+`) // Match priority: '(A) ...' or 'x (A) ...' or 'x 2012-12-12 (A) ...'
// Match created date: '(A) 2012-12-12 ...' or 'x 2012-12-12 (A) 2012-12-12 ...' or 'x (A) 2012-12-12 ...'or 'x 2012-12-12 2012-12-12 ...' or '2012-12-12 ...'
createdDateRx = regexp.MustCompile(`^(\([A-Z]\)|x \d{4}-\d{2}-\d{2} \([A-Z]\)|x \([A-Z]\)|x \d{4}-\d{2}-\d{2}|)\s*(\d{4}-\d{2}-\d{2})\s+`)
completedRx = regexp.MustCompile(`^x\s+`) // Match completed: 'x ...'
completedDateRx = regexp.MustCompile(`^x\s*(\d{4}-\d{2}-\d{2})\s+`) // Match completed date: 'x 2012-12-12 ...'
addonTagRx = regexp.MustCompile(`(^|\s+)([\w-]+):(\S+)`) // Match additional tags date: '... due:2012-12-12 ...'
contextRx = regexp.MustCompile(`(^|\s+)@(\S+)`) // Match contexts: '@Context ...' or '... @Context ...'
projectRx = regexp.MustCompile(`(^|\s+)\+(\S+)`) // Match projects: '+Project...' or '... +Project ...')
)
// Todo represents a todo.txt task entry.
type Todo struct {
Id int // Internal todo id.
Original string // Original raw todo text.
Todo string // Todo part of todo text.
Priority string
Projects []string
Contexts []string
AdditionalTags map[string]string // Addon tags will be available here.
CreatedDate time.Time
DueDate time.Time
CompletedDate time.Time
Completed bool
}
// String returns a complete todo string in todo.txt format.
//
// Contexts, Projects and additional tags are alphabetically sorted,
// and appendend at the end in the following order:
// Contexts, Projects, Tags
//
// For example:
//
// "(A) 2013-07-23 Call Dad @Home @Phone +Family due:2013-07-31 customTag1:Important!"
func (todo Todo) String() string {
var text string
if todo.Completed {
text += "x "
if todo.HasCompletedDate() {
text += fmt.Sprintf("%s ", todo.CompletedDate.Format(DateLayout))
}
}
if todo.HasPriority() {
text += fmt.Sprintf("(%s) ", todo.Priority)
}
if todo.HasCreatedDate() {
text += fmt.Sprintf("%s ", todo.CreatedDate.Format(DateLayout))
}
text += todo.Todo
if len(todo.Contexts) > 0 {
sort.Strings(todo.Contexts)
for _, context := range todo.Contexts {
text += fmt.Sprintf(" @%s", context)
}
}
if len(todo.Projects) > 0 {
sort.Strings(todo.Projects)
for _, project := range todo.Projects {
text += fmt.Sprintf(" +%s", project)
}
}
if len(todo.AdditionalTags) > 0 {
// Sort map alphabetically by keys
keys := make([]string, 0, len(todo.AdditionalTags))
for key := range todo.AdditionalTags {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
text += fmt.Sprintf(" %s:%s", key, todo.AdditionalTags[key])
}
}
if todo.HasDueDate() {
text += fmt.Sprintf(" due:%s", todo.DueDate.Format(DateLayout))
}
return text
}
// NewTodo creates a new empty Todo with default values. (CreatedDate is set to Now())
func NewTodo() Todo {
todo := Todo{}
todo.CreatedDate = time.Now()
return todo
}
// ParseTodo parses the input text string into a Todo struct.
func ParseTodo(text string) (*Todo, error) {
var err error
todo := Todo{}
todo.Original = strings.Trim(text, "\t\n\r ")
todo.Todo = todo.Original
// Check for completed
if completedRx.MatchString(todo.Original) {
todo.Completed = true
// Check for completed date
if completedDateRx.MatchString(todo.Original) {
if date, err := time.Parse(DateLayout, completedDateRx.FindStringSubmatch(todo.Original)[1]); err == nil {
todo.CompletedDate = date
} else {
return nil, err
}
}
// Remove from Todo text
todo.Todo = completedDateRx.ReplaceAllString(todo.Todo, "") // Strip CompletedDate first, otherwise it wouldn't match anymore (^x date...)
todo.Todo = completedRx.ReplaceAllString(todo.Todo, "") // Strip 'x '
}
// Check for priority
if priorityRx.MatchString(todo.Original) {
todo.Priority = priorityRx.FindStringSubmatch(todo.Original)[2]
todo.Todo = priorityRx.ReplaceAllString(todo.Todo, "") // Remove from Todo text
}
// Check for created date
if createdDateRx.MatchString(todo.Original) {
if date, err := time.Parse(DateLayout, createdDateRx.FindStringSubmatch(todo.Original)[2]); err == nil {
todo.CreatedDate = date
todo.Todo = createdDateRx.ReplaceAllString(todo.Todo, "") // Remove from Todo text
} else {
return nil, err
}
}
// function for collecting projects/contexts as slices from text
getSlice := func(rx *regexp.Regexp) []string {
matches := rx.FindAllStringSubmatch(todo.Original, -1)
slice := make([]string, 0, len(matches))
seen := make(map[string]bool, len(matches))
for _, match := range matches {
word := strings.Trim(match[2], "\t\n\r ")
if _, found := seen[word]; !found {
slice = append(slice, word)
seen[word] = true
}
}
sort.Strings(slice)
return slice
}
// Check for contexts
if contextRx.MatchString(todo.Original) {
todo.Contexts = getSlice(contextRx)
todo.Todo = contextRx.ReplaceAllString(todo.Todo, "") // Remove from Todo text
}
// Check for projects
if projectRx.MatchString(todo.Original) {
todo.Projects = getSlice(projectRx)
todo.Todo = projectRx.ReplaceAllString(todo.Todo, "") // Remove from Todo text
}
// Check for additional tags
if addonTagRx.MatchString(todo.Original) {
matches := addonTagRx.FindAllStringSubmatch(todo.Original, -1)
tags := make(map[string]string, len(matches))
for _, match := range matches {
key, value := match[2], match[3]
if key == "due" { // due date is a known addon tag, it has its own struct field
if date, err := time.Parse(DateLayout, value); err == nil {
todo.DueDate = date
} else {
return nil, err
}
} else if key != "" && value != "" {
tags[key] = value
}
}
todo.AdditionalTags = tags
todo.Todo = addonTagRx.ReplaceAllString(todo.Todo, "") // Remove from Todo text
}
// Trim any remaining whitespaces from Todo text
todo.Todo = strings.Trim(todo.Todo, "\t\n\r\f ")
return &todo, err
}
// HasPriority returns true if the task has a priority.
func (todo *Todo) HasPriority() bool {
return todo.Priority != ""
}
// HasCreatedDate returns true if the task has a created date.
func (todo *Todo) HasCreatedDate() bool {
return !todo.CreatedDate.IsZero()
}
// HasDueDate returns true if the task has a due date.
func (todo *Todo) HasDueDate() bool {
return !todo.DueDate.IsZero()
}
// HasCompletedDate returns true if the task has a completed date.
func (todo *Todo) HasCompletedDate() bool {
return !todo.CompletedDate.IsZero() && todo.Completed
}
// Complete sets Task.Completed to 'true' if the task was not already completed.
// Also sets Task.CompletedDate to time.Now()
func (todo *Todo) Complete() {
if !todo.Completed {
todo.Completed = true
todo.CompletedDate = time.Now()
}
}
// Reopen sets Task.Completed to 'false' if the task was completed.
// Also resets Task.CompletedDate.
func (todo *Todo) Reopen() {
if todo.Completed {
todo.Completed = false
todo.CompletedDate = time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC) // time.IsZero() value
}
}
// IsOverdue returns true if due date is in the past.
//
// This function does not take the Completed flag into consideration.
// You should check Task.Completed first if needed.
func (todo *Todo) IsOverdue() bool {
if todo.HasDueDate() {
return todo.DueDate.Before(time.Now())
}
return false
}
// Due returns the duration passed since due date, or until due date from now.
// Check with IsOverdue() if the task is overdue or not.
//
// Just as with IsOverdue(), this function does also not take the Completed flag into consideration.
// You should check Task.Completed first if needed.
func (todo *Todo) Due() time.Duration {
if todo.IsOverdue() {
return time.Now().Sub(todo.DueDate)
}
return todo.DueDate.Sub(time.Now())
}
func (todo *Todo) HasContext(ctx string) bool {
for _, c := range todo.Contexts {
if c == ctx {
return true
}
}
return false
}
func (todo *Todo) HasProject(proj string) bool {
for _, p := range todo.Projects {
if p == proj {
return true
}
}
return false
}