Documentation ¶
Overview ¶
Package todotxt is yet another a Go library for Gina Trapani's todo.txt files. It allows for parsing and manipulating of task lists and tasks in the todo.txt format.
Index ¶
- Variables
- func FilterCompleted(t Task) bool
- func FilterDueToday(t Task) bool
- func FilterHasDueDate(t Task) bool
- func FilterHasPriority(t Task) bool
- func FilterNotCompleted(t Task) bool
- func FilterOverdue(t Task) bool
- func WriteToFile(tasklist *TaskList, file *os.File) error
- func WriteToPath(tasklist *TaskList, filename string) error
- type Predicate
- type Task
- func (task *Task) Complete()
- func (task *Task) Due() time.Duration
- func (task *Task) HasAdditionalTags() bool
- func (task *Task) HasCompletedDate() bool
- func (task *Task) HasContexts() bool
- func (task *Task) HasCreatedDate() bool
- func (task *Task) HasDueDate() bool
- func (task *Task) HasPriority() bool
- func (task *Task) HasProjects() bool
- func (task *Task) IsCompleted() bool
- func (task *Task) IsDueToday() bool
- func (task *Task) IsOverdue() bool
- func (task *Task) Reopen()
- func (task *Task) Segments() []*TaskSegment
- func (task Task) String() string
- func (task *Task) Task() string
- type TaskList
- func (tasklist *TaskList) AddTask(task *Task)
- func (tasklist TaskList) Filter(predicate Predicate, predicates ...Predicate) TaskList
- func (tasklist *TaskList) GetTask(id int) (*Task, error)
- func (tasklist *TaskList) LoadFromFile(file *os.File) error
- func (tasklist *TaskList) LoadFromPath(filename string) error
- func (tasklist *TaskList) RemoveTask(task Task) error
- func (tasklist *TaskList) RemoveTaskByID(id int) error
- func (tasklist *TaskList) Sort(flag TaskSortByType, flags ...TaskSortByType) error
- func (tasklist TaskList) String() string
- func (tasklist *TaskList) WriteToFile(file *os.File) error
- func (tasklist *TaskList) WriteToPath(filename string) error
- type TaskSegment
- type TaskSegmentType
- type TaskSortByType
Examples ¶
Constants ¶
This section is empty.
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 FilterDueToday ¶
FilterDueToday filters tasks that are due today.
func FilterHasDueDate ¶
FilterHasDueDate filters tasks that have due date.
func FilterHasPriority ¶
FilterHasPriority filters tasks that have priority.
func FilterNotCompleted ¶
FilterNotCompleted filters tasks that are not completed.
func WriteToFile ¶
WriteToFile writes a TaskList to *os.File.
Using *os.File instead of a filename allows to also use os.Stdout.
Note: Comments from original file will be omitted and not written to target *os.File, if IgnoreComments is set to 'true'.
func WriteToPath ¶
WriteToPath writes a TaskList to the specified file (most likely called "todo.txt").
Types ¶
type Predicate ¶
Predicate is a function that takes a task as input and returns a bool.
func FilterByContext ¶
FilterByContext returns a filter for tasks that have the given context. String comparison in the filters is case-insensitive.
func FilterByPriority ¶
FilterByPriority returns a filter for tasks that have the given priority. String comparison in the filters is case-insensitive.
func FilterByProject ¶
FilterByProject returns a filter for tasks that have the given project. String comparison in the filters is case-insensitive.
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 left until due date from now. The duration is negative if the task is overdue.
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) HasAdditionalTags ¶
HasAdditionalTags returns true if the task has any additional tags.
func (*Task) HasCompletedDate ¶
HasCompletedDate returns true if the task has a completed date.
func (*Task) HasContexts ¶
HasContexts returns true if the task has any contexts.
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) HasProjects ¶
HasProjects returns true if the task has any projects.
func (*Task) IsCompleted ¶
IsCompleted returns true if the task has already been completed.
func (*Task) IsDueToday ¶
IsDueToday returns true if the task is due todasy.
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) Segments ¶
func (task *Task) Segments() []*TaskSegment
Segments returns a segmented task string in todo.txt format. The order of segments is the same as String().
func (Task) String ¶
String returns a complete task string in todo.txt format.
Contexts, Projects and additional tags are alphabetically sorted, and appended 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 LoadFromPath ¶
LoadFromPath loads and returns a TaskList from a file (most likely called "todo.txt").
Example ¶
if tasklist, err := LoadFromPath("testdata/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, and returns a new TaskList. The original TaskList is not modified.
Example ¶
var tasklist TaskList if err := tasklist.LoadFromPath("testdata/todo.txt"); err != nil { log.Fatal(err) } // filter tasks that are not overdue and are priority A or B. tasklist = tasklist.Filter(FilterNot(FilterOverdue)).Filter(FilterByPriority("A"), FilterByPriority("B")) fmt.Println(tasklist[0].Todo) fmt.Println(tasklist[1].Projects) fmt.Println(tasklist[2].Priority)
Output: Call Mom [Health] B
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) LoadFromPath ¶
LoadFromPath 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.LoadFromPath("testdata/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 eighth 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 ¶
func (tasklist *TaskList) Sort(flag TaskSortByType, flags ...TaskSortByType) error
Sort allows a TaskList to be sorted by certain predefined fields. Multiple-key sorting is supported. See constants Sort* for fields and sort order.
Example ¶
var tasklist TaskList if err := tasklist.LoadFromPath("testdata/todo.txt"); err != nil { log.Fatal(err) } // sort tasks by project and then priority in ascending order if err := tasklist.Sort(SortProjectAsc, SortPriorityAsc); err != nil { log.Fatal(err) } fmt.Println(tasklist[0].Todo) fmt.Println(tasklist[1].Projects) fmt.Println(tasklist[2].Priority) fmt.Println(tasklist[3].Contexts)
Output: Call Mom [Health] B [Computer]
func (*TaskList) WriteToFile ¶
WriteToFile writes a TaskList to *os.File.
Using *os.File instead of a filename allows to also use os.Stdout.
Note: Comments from original file will be omitted and not written to target *os.File, if IgnoreComments is set to 'true'.
func (*TaskList) WriteToPath ¶
WriteToPath writes a TaskList to the specified file (most likely called "todo.txt").
type TaskSegment ¶
type TaskSegment struct { Type TaskSegmentType Originals []string Display string }
TaskSegment represents a segment in task string.
type TaskSegmentType ¶
type TaskSegmentType uint8
TaskSegmentType represents type of segment in task string.
const ( SegmentIsCompleted TaskSegmentType = iota + 1 SegmentCompletedDate SegmentPriority SegmentCreatedDate SegmentTodoText SegmentContext SegmentProject SegmentTag SegmentDueDate )
Flags for indicating type of segment in task string.
func (TaskSegmentType) String ¶ added in v0.0.4
func (i TaskSegmentType) String() string
type TaskSortByType ¶ added in v0.0.4
type TaskSortByType uint8
TaskSortByType represents type of sorting element and order.
const ( SortTaskIDAsc TaskSortByType = iota + 1 SortTaskIDDesc SortTodoTextAsc SortTodoTextDesc SortPriorityAsc SortPriorityDesc SortCreatedDateAsc SortCreatedDateDesc SortCompletedDateAsc SortCompletedDateDesc SortDueDateAsc SortDueDateDesc SortContextAsc SortContextDesc SortProjectAsc SortProjectDesc )
Flags for defining sorting element and order.
func (TaskSortByType) String ¶ added in v0.0.4
func (i TaskSortByType) String() string