2014-01-03 00:29:54 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2014-01-03 12:01:04 +00:00
|
|
|
// Package todotxt is a Go client library for Gina Trapani's todo.txt files.
|
|
|
|
// It allows for parsing and manipulating of task lists and tasks in the todo.txt format.
|
|
|
|
//
|
|
|
|
// Source code and project home: https://github.com/JamesClonk/go-todotxt
|
2014-01-03 00:29:54 +00:00
|
|
|
package todotxt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2014-01-03 12:01:04 +00:00
|
|
|
"fmt"
|
2014-01-03 00:29:54 +00:00
|
|
|
"os"
|
|
|
|
"regexp"
|
2014-01-03 13:21:27 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2014-01-03 00:29:54 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-01-03 12:01:04 +00:00
|
|
|
// Task represents a todo.txt task entry.
|
2014-01-03 00:29:54 +00:00
|
|
|
type Task struct {
|
2014-01-03 12:01:04 +00:00
|
|
|
Original string // Original raw task text
|
|
|
|
Todo string // Todo part of task text
|
2014-01-03 00:29:54 +00:00
|
|
|
Priority string
|
|
|
|
Projects []string
|
|
|
|
Contexts []string
|
|
|
|
CreatedDate time.Time
|
|
|
|
DueDate time.Time
|
|
|
|
CompletedDate time.Time
|
|
|
|
Completed bool
|
|
|
|
}
|
|
|
|
|
2014-01-03 12:01:04 +00:00
|
|
|
// TaskList represents a list of todo.txt task entries.
|
|
|
|
// It is usually loaded from a whole todo.txt file.
|
2014-01-03 00:29:54 +00:00
|
|
|
type TaskList []Task
|
|
|
|
|
|
|
|
var (
|
2014-01-03 12:01:04 +00:00
|
|
|
// Used for formatting time.Time into todo.txt date format.
|
|
|
|
DateLayout = "2006-01-02"
|
|
|
|
|
|
|
|
// unexported vars
|
2014-01-03 13:21:27 +00:00
|
|
|
priorityRx = regexp.MustCompile(`^\(([A-Z])\)\s+`) // Match priority: '(A) ...'
|
|
|
|
createdDateRx = regexp.MustCompile(`^(\([A-Z]\)|)\s*([\d]{4}-[\d]{2}-[\d]{2})\s+`) // Match date: '(A) 2012-12-12 ...' or '2012-12-12 ...'
|
|
|
|
dueDateRx = regexp.MustCompile(`\s+due:([\d]{4}-[\d]{2}-[\d]{2})`) // Match due date: '... due:2012-12-12 ...'
|
|
|
|
contextRx = regexp.MustCompile(`(^|\s+)@([[:word:]]+)`) // Match contexts: '@Context ...' or '... @Context ...'
|
|
|
|
projectRx = regexp.MustCompile(`(^|\s+)\+([[:word:]]+)`) // Match projects: '+Project...' or '... +Project ...'
|
2014-01-03 00:29:54 +00:00
|
|
|
)
|
|
|
|
|
2014-01-03 12:01:04 +00:00
|
|
|
// String returns a complete task string in todo.txt format.
|
|
|
|
//
|
|
|
|
// For example:
|
|
|
|
// "(A) 2013-07-23 Call Dad @Phone +Family due:2013-07-31"
|
2014-01-03 00:29:54 +00:00
|
|
|
func (task *Task) String() string {
|
2014-01-03 12:01:04 +00:00
|
|
|
var text string
|
2014-01-03 13:21:27 +00:00
|
|
|
|
2014-01-03 12:01:04 +00:00
|
|
|
if task.HasPriority() {
|
|
|
|
text += fmt.Sprintf("(%s) ", task.Priority)
|
|
|
|
}
|
2014-01-03 13:21:27 +00:00
|
|
|
|
2014-01-03 12:01:04 +00:00
|
|
|
if task.HasCreatedDate() {
|
|
|
|
text += fmt.Sprintf("%s ", task.CreatedDate.Format(DateLayout))
|
|
|
|
}
|
2014-01-03 13:21:27 +00:00
|
|
|
|
2014-01-03 12:01:04 +00:00
|
|
|
text += task.Todo
|
2014-01-03 13:21:27 +00:00
|
|
|
|
|
|
|
if len(task.Contexts) > 0 {
|
|
|
|
for _, context := range task.Contexts {
|
|
|
|
text += fmt.Sprintf(" @%s", context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(task.Projects) > 0 {
|
|
|
|
for _, project := range task.Projects {
|
|
|
|
text += fmt.Sprintf(" +%s", project)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 12:01:04 +00:00
|
|
|
if task.HasDueDate() {
|
2014-01-03 13:21:27 +00:00
|
|
|
text += fmt.Sprintf(" due:%s", task.DueDate.Format(DateLayout))
|
2014-01-03 12:01:04 +00:00
|
|
|
}
|
2014-01-03 13:21:27 +00:00
|
|
|
|
2014-01-03 12:01:04 +00:00
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
// Task returns a complete task string in todo.txt format.
|
|
|
|
// The same as *Task.String().
|
|
|
|
func (task *Task) Task() string {
|
|
|
|
return task.String()
|
2014-01-03 00:29:54 +00:00
|
|
|
}
|
|
|
|
|
2014-01-03 12:01:04 +00:00
|
|
|
// HasPriority returns true if the task has a priority.
|
2014-01-03 00:29:54 +00:00
|
|
|
func (task *Task) HasPriority() bool {
|
|
|
|
return task.Priority != ""
|
|
|
|
}
|
|
|
|
|
2014-01-03 12:01:04 +00:00
|
|
|
// HasCreatedDate returns true if the task has a created date.
|
2014-01-03 00:29:54 +00:00
|
|
|
func (task *Task) HasCreatedDate() bool {
|
|
|
|
return !task.CreatedDate.IsZero()
|
|
|
|
}
|
|
|
|
|
2014-01-03 12:01:04 +00:00
|
|
|
// HasDueDate returns true if the task has a due date.
|
2014-01-03 00:29:54 +00:00
|
|
|
func (task *Task) HasDueDate() bool {
|
|
|
|
return !task.DueDate.IsZero()
|
|
|
|
}
|
|
|
|
|
2014-01-03 12:01:04 +00:00
|
|
|
// HasCompletedDate returns true if the task has a completed date.
|
2014-01-03 00:29:54 +00:00
|
|
|
func (task *Task) HasCompletedDate() bool {
|
|
|
|
return !task.CompletedDate.IsZero()
|
|
|
|
}
|
|
|
|
|
2014-01-03 12:01:04 +00:00
|
|
|
// LoadFromFile loads a TaskList from *os.File.
|
|
|
|
//
|
|
|
|
// Using *os.File instead of a filename allows to also use os.Stdin.
|
|
|
|
//
|
|
|
|
// Note: This will clear the current TaskList and overwrite it's contents with whatever is in *os.File.
|
2014-01-03 00:29:54 +00:00
|
|
|
func (tasklist *TaskList) LoadFromFile(file *os.File) error {
|
2014-01-03 12:01:04 +00:00
|
|
|
*tasklist = []Task{} // Empty tasklist
|
2014-01-03 00:29:54 +00:00
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
task := Task{}
|
2014-01-03 12:01:04 +00:00
|
|
|
task.Original = scanner.Text()
|
2014-01-03 00:29:54 +00:00
|
|
|
|
|
|
|
// Check for priority
|
2014-01-03 12:01:04 +00:00
|
|
|
if priorityRx.MatchString(task.Original) {
|
2014-01-03 13:21:27 +00:00
|
|
|
task.Priority = priorityRx.FindStringSubmatch(task.Original)[1]
|
2014-01-03 00:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for created date
|
2014-01-03 12:01:04 +00:00
|
|
|
if createdDateRx.MatchString(task.Original) {
|
|
|
|
if date, err := time.Parse(DateLayout, createdDateRx.FindStringSubmatch(task.Original)[2]); err != nil {
|
2014-01-03 00:29:54 +00:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
task.CreatedDate = date
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 13:21:27 +00:00
|
|
|
// Check for due date
|
|
|
|
if dueDateRx.MatchString(task.Original) {
|
|
|
|
if date, err := time.Parse(DateLayout, dueDateRx.FindStringSubmatch(task.Original)[1]); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
task.DueDate = date
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// function for collecting projects/contexts as slices from text
|
|
|
|
getSlice := func(rx *regexp.Regexp) []string {
|
|
|
|
matches := rx.FindAllStringSubmatch(task.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(task.Original) {
|
|
|
|
task.Contexts = getSlice(contextRx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for projects
|
|
|
|
if projectRx.MatchString(task.Original) {
|
|
|
|
task.Projects = getSlice(projectRx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Todo text
|
|
|
|
// use replacer function here.. strip all other fields from task.Original, then left+right trim --> todo text
|
|
|
|
text := strings.Replace(task.Original, " ", "", -1)
|
|
|
|
task.Todo = text
|
|
|
|
|
2014-01-03 00:29:54 +00:00
|
|
|
*tasklist = append(*tasklist, task)
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-03 12:01:04 +00:00
|
|
|
// LoadFromFilename loads a TaskList from a file (most likely called "todo.txt").
|
|
|
|
//
|
|
|
|
// Note: This will clear the current TaskList and overwrite it's contents with whatever is in the file.
|
2014-01-03 00:29:54 +00:00
|
|
|
func (tasklist *TaskList) LoadFromFilename(filename string) error {
|
|
|
|
file, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
return tasklist.LoadFromFile(file)
|
|
|
|
}
|
2014-01-03 12:01:04 +00:00
|
|
|
|
|
|
|
// LoadFromFile loads and returns a TaskList from *os.File.
|
|
|
|
//
|
|
|
|
// Using *os.File instead of a filename allows to also use os.Stdin.
|
|
|
|
func LoadFromFile(file *os.File) (*TaskList, error) {
|
|
|
|
tasklist := &TaskList{}
|
|
|
|
err := tasklist.LoadFromFile(file)
|
|
|
|
return tasklist, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFromFilename loads and returns a TaskList from a file (most likely called "todo.txt").
|
|
|
|
func LoadFromFilename(filename string) (*TaskList, error) {
|
|
|
|
tasklist := &TaskList{}
|
|
|
|
err := tasklist.LoadFromFilename(filename)
|
|
|
|
return tasklist, err
|
|
|
|
}
|