todotxt

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2020 License: MIT Imports: 11 Imported by: 5

README

todotxt

Yet another a Go library for Gina Trapani's todo.txt files. ✅

PkgGoDev License GitHub Action Workflow Go Report Card Codacy Badge Codecov

Features

Based on go-todotxt from Fabio Berchtold with:

  • Go mod support
  • Segments for task string
  • Task due today is not overdue
  • Negative Due() for overdue tasks
  • Support multiple options for sorting and filtering
  • More sorting options: by ID, text, context, project
  • Preset filters

Usage

A quick start example:

import (
	todo "github.com/1set/todotxt"
)

// ...

if tasklist, err := todo.LoadFromPath("todo.txt"); err != nil {
    log.Fatal(err)
} else {
    tasks := tasklist.Filter(todo.FilterNotCompleted).Filter(todo.FilterDueToday, todo.FilterHasPriority)
    _ = tasks.Sort(todo.SortPriorityAsc, todo.SortProjectAsc)
    for i, t := range tasks {
        fmt.Println(t.Todo)
        // oh really?
        tasks[i].Complete()
    }
    if err = tasks.WriteToPath("today-todo.txt"); err != nil {
        log.Fatal(err)
    }
}

For more examples and details, please check the Go Doc.

Credits

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

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DateLayout is used for formatting time.Time into todo.txt date format and vice-versa.
	DateLayout = "2006-01-02"
)
View Source
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 FilterCompleted

func FilterCompleted(t Task) bool

FilterCompleted filters completed tasks.

func FilterDueToday

func FilterDueToday(t Task) bool

FilterDueToday filters tasks that are due today.

func FilterHasDueDate

func FilterHasDueDate(t Task) bool

FilterHasDueDate filters tasks that have due date.

func FilterHasPriority

func FilterHasPriority(t Task) bool

FilterHasPriority filters tasks that have priority.

func FilterNotCompleted

func FilterNotCompleted(t Task) bool

FilterNotCompleted filters tasks that are not completed.

func FilterOverdue

func FilterOverdue(t Task) bool

FilterOverdue filters tasks that are overdue.

func WriteToFile

func WriteToFile(tasklist *TaskList, file *os.File) error

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

func WriteToPath(tasklist *TaskList, filename string) error

WriteToPath writes a TaskList to the specified file (most likely called "todo.txt").

Types

type Predicate

type Predicate func(Task) bool

Predicate is a function that takes a task as input and returns a bool.

func FilterByContext

func FilterByContext(context string) Predicate

FilterByContext returns a filter for tasks that have the given context. String comparison in the filters is case-insensitive.

func FilterByPriority

func FilterByPriority(priority string) Predicate

FilterByPriority returns a filter for tasks that have the given priority. String comparison in the filters is case-insensitive.

func FilterByProject

func FilterByProject(project string) Predicate

FilterByProject returns a filter for tasks that have the given project. String comparison in the filters is case-insensitive.

func FilterNot

func FilterNot(predicate Predicate) Predicate

FilterNot returns a reversed filter for existing predicate.

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 ParseTask

func ParseTask(text string) (*Task, error)

ParseTask parses the input text string into a Task struct.

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

func (task *Task) Due() time.Duration

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

func (task *Task) HasAdditionalTags() bool

HasAdditionalTags returns true if the task has any additional tags.

func (*Task) HasCompletedDate

func (task *Task) HasCompletedDate() bool

HasCompletedDate returns true if the task has a completed date.

func (*Task) HasContexts

func (task *Task) HasContexts() bool

HasContexts returns true if the task has any contexts.

func (*Task) HasCreatedDate

func (task *Task) HasCreatedDate() bool

HasCreatedDate returns true if the task has a created date.

func (*Task) HasDueDate

func (task *Task) HasDueDate() bool

HasDueDate returns true if the task has a due date.

func (*Task) HasPriority

func (task *Task) HasPriority() bool

HasPriority returns true if the task has a priority.

func (*Task) HasProjects

func (task *Task) HasProjects() bool

HasProjects returns true if the task has any projects.

func (*Task) IsCompleted

func (task *Task) IsCompleted() bool

IsCompleted returns true if the task has already been completed.

func (*Task) IsDueToday

func (task *Task) IsDueToday() bool

IsDueToday returns true if the task is due todasy.

func (*Task) IsOverdue

func (task *Task) IsOverdue() bool

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

func (task Task) String() 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!"

func (*Task) Task

func (task *Task) Task() string

Task returns a complete task string in todo.txt format. See *Task.String() for further information.

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

func LoadFromFile(file *os.File) (TaskList, error)

LoadFromFile loads and returns a TaskList from *os.File.

Using *os.File instead of a filename allows to also use os.Stdin.

func LoadFromPath

func LoadFromPath(filename string) (TaskList, error)

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 NewTaskList

func NewTaskList() TaskList

NewTaskList creates a new empty TaskList.

func (*TaskList) AddTask

func (tasklist *TaskList) AddTask(task *Task)

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

func (tasklist TaskList) Filter(predicate Predicate, predicates ...Predicate) TaskList

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

func (tasklist *TaskList) GetTask(id int) (*Task, error)

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

func (tasklist *TaskList) LoadFromFile(file *os.File) error

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

func (tasklist *TaskList) LoadFromPath(filename string) error

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

func (tasklist *TaskList) RemoveTask(task Task) error

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

func (tasklist *TaskList) RemoveTaskByID(id int) error

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) String

func (tasklist TaskList) String() string

String returns a complete list of tasks in todo.txt format.

func (*TaskList) WriteToFile

func (tasklist *TaskList) WriteToFile(file *os.File) error

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

func (tasklist *TaskList) WriteToPath(filename string) error

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL