Documentation ¶
Overview ¶
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
Index ¶
- Constants
- Variables
- func WriteToFile(tasklist *TaskList, file *os.File) error
- func WriteToFilename(tasklist *TaskList, filename string) error
- type Task
- func (task *Task) Complete()
- func (task *Task) Due() time.Duration
- func (task *Task) HasCompletedDate() bool
- func (task *Task) HasCreatedDate() bool
- func (task *Task) HasDueDate() bool
- func (task *Task) HasPriority() bool
- func (task *Task) IsOverdue() bool
- func (task *Task) Reopen()
- func (task Task) String() string
- func (task *Task) Task() string
- type TaskList
- func (tasklist *TaskList) AddTask(task *Task)
- func (tasklist *TaskList) Filter(predicate func(Task) bool) *TaskList
- func (tasklist *TaskList) GetTask(id int) (*Task, error)
- func (tasklist *TaskList) LoadFromFile(file *os.File) error
- func (tasklist *TaskList) LoadFromFilename(filename string) error
- func (tasklist *TaskList) RemoveTask(task Task) error
- func (tasklist *TaskList) RemoveTaskById(id int) error
- func (tasklist *TaskList) Sort(sortFlag int) error
- func (tasklist TaskList) String() (text string)
- func (tasklist *TaskList) WriteToFile(file *os.File) error
- func (tasklist *TaskList) WriteToFilename(filename string) error
Examples ¶
Constants ¶
const ( SORT_PRIORITY_ASC = iota SORT_PRIORITY_DESC SORT_CREATED_DATE_ASC SORT_CREATED_DATE_DESC SORT_COMPLETED_DATE_ASC SORT_COMPLETED_DATE_DESC SORT_DUE_DATE_ASC SORT_DUE_DATE_DESC )
Flags for defining sort element and order.
Variables ¶
var (
// DateLayout is used for formatting time.Time into todo.txt date format and vice-versa.
DateLayout = "2006-01-02"
)
var ( // IgnoreComments is used to switch ignoring of comments (lines starting with "#"). // If this is set to 'false', then lines starting with "#" will be parsed as tasks. IgnoreComments = true )
IgnoreComments can be set to 'false', in order to revert to a more standard todo.txt behaviour. The todo.txt format does not define comments.
Functions ¶
func WriteToFile ¶
WriteToFile writes a TaskList to *os.File.
Using *os.File instead of a filename allows to also use os.Stdout.
func WriteToFilename ¶
WriteToFilename writes a TaskList to the specified file (most likely called "todo.txt").
Types ¶
type Task ¶
type Task struct { Id int // Internal task id. Original string // Original raw task text. Todo string // Todo part of task 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 }
Task represents a todo.txt task entry.
func NewTask ¶
func NewTask() Task
NewTask creates a new empty Task with default values. (CreatedDate is set to Now())
func (*Task) Complete ¶
func (task *Task) Complete()
Complete sets Task.Completed to 'true' if the task was not already completed. Also sets Task.CompletedDate to time.Now()
func (*Task) Due ¶
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 (*Task) HasCompletedDate ¶
HasCompletedDate returns true if the task has a completed date.
func (*Task) HasCreatedDate ¶
HasCreatedDate returns true if the task has a created date.
func (*Task) HasDueDate ¶
HasDueDate returns true if the task has a due date.
func (*Task) HasPriority ¶
HasPriority returns true if the task has a priority.
func (*Task) IsOverdue ¶
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 (*Task) Reopen ¶
func (task *Task) Reopen()
Reopen sets Task.Completed to 'false' if the task was completed. Also resets Task.CompletedDate.
func (Task) String ¶
String returns a complete task 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!"
type TaskList ¶
type TaskList []Task
TaskList represents a list of todo.txt task entries. It is usually loaded from a whole todo.txt file.
func LoadFromFile ¶
LoadFromFile loads and returns a TaskList from *os.File.
Using *os.File instead of a filename allows to also use os.Stdin.
func LoadFromFilename ¶
LoadFromFilename loads and returns a TaskList from a file (most likely called "todo.txt").
Example ¶
if tasklist, err := LoadFromFilename("todo.txt"); err != nil { log.Fatal(err) } else { fmt.Print(tasklist) // String representation of TaskList works as expected. }
Output: (A) Call Mom @Phone +Family (A) Schedule annual checkup +Health (B) Outline chapter 5 @Computer +Novel (C) Add cover sheets @Office +TPSReports Plan backyard herb garden @Home Pick up milk @GroceryStore Research self-publishing services @Computer +Novel x Download Todo.txt mobile app @Phone
func (*TaskList) AddTask ¶
AddTask appends a Task to the current TaskList and takes care to set the Task.Id correctly, modifying the Task by the given pointer!
func (*TaskList) Filter ¶
Filter filters the current TaskList for the given predicate (a function that takes a task as input and returns a bool), and returns a new TaskList. The original TaskList is not modified.
func (*TaskList) GetTask ¶
GetTask returns a Task by given task 'id' from the TaskList. The returned Task pointer can be used to update the Task inside the TaskList. Returns an error if Task could not be found.
func (*TaskList) LoadFromFile ¶
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.
func (*TaskList) LoadFromFilename ¶
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.
Example ¶
var tasklist TaskList // This will overwrite whatever was in the tasklist before. // Irrelevant here since the list is still empty. if err := tasklist.LoadFromFilename("todo.txt"); err != nil { log.Fatal(err) } fmt.Println(tasklist[0].Todo) // Text part of first task (Call Mom) fmt.Println(tasklist[2].Contexts) // Slice of contexts from third task ([Computer]) fmt.Println(tasklist[3].Priority) // Priority of fourth task (C) fmt.Println(tasklist[7].Completed) // Completed flag of eigth task (true)
Output: Call Mom [Computer] C true
func (*TaskList) RemoveTask ¶
RemoveTask removes any Task from the TaskList with the same String representation as the given Task. Returns an error if no Task was removed.
func (*TaskList) RemoveTaskById ¶
RemoveTaskById removes any Task with given Task 'id' from the TaskList. Returns an error if no Task was removed.
func (*TaskList) Sort ¶
Sort allows a TaskList to be sorted by certain predefined fields. See constants SORT_* for fields and sort order.
func (*TaskList) WriteToFile ¶
WriteToFile writes a TaskList to *os.File.
Using *os.File instead of a filename allows to also use os.Stdout.
func (*TaskList) WriteToFilename ¶
WriteToFilename writes a TaskList to the specified file (most likely called "todo.txt").