todotxt

package module
v0.0.0-...-e7c5dfe Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2021 License: MPL-2.0 Imports: 9 Imported by: 0

README

go-todotxt

A Go todo.txt library.

The todotxt package 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.

Installation

$ go get github.com/br0xen/go-todotxt

Requirements

go-todotxt requires Go1.1 or higher.

Usage

	package main

	import (
		"fmt"
		"github.com/br0xen/go-todotxt"
		"log"
	)

	func main() {
		todotxt.IgnoreComments = false

		tasklist, err := todotxt.LoadFromFilename("todo.txt")
		if err != nil {
			log.Fatal(err)
		}

		// tasklist now contains a slice of Tasks
		fmt.Printf("Task 2, todo: %v\n", tasklist[1].Todo)
		fmt.Printf("Task 3: %v\n", tasklist[2])
		fmt.Printf("Task 4, has priority: %v\n\n", tasklist[3].HasPriority())
		fmt.Print(tasklist)

		// Filter list to get only completed tasks
		completedList := tasklist.Filter(func(t Task) bool {
			return t.Completed 
		})
		fmt.Print(completedList)

		// Add a new empty Task to tasklist
		task := NewTask()
		tasklist.AddTask(&task)

		// Or a parsed Task from a string
		parsedTask, _ := ParseTask("x (C) 2014-01-01 Create golang library documentation @Go +go-todotxt due:2014-01-12")
		tasklist.AddTask(parsed)

		// Update an existing task
		task, _ := tasklist.GetTask(2) // Task pointer
		task.Todo = "Do something different.."
		tasklist.WriteToFilename("todo.txt")
	}

Documentation

See GoDoc - Documentation for further documentation.

License

The source files are distributed under the Mozilla Public License, version 2.0, unless otherwise noted.
Please read the FAQ if you have further questions regarding the license.

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

Examples

Constants

View Source
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
	SORT_USEFULNESS_ASC
	SORT_USEFULNESS_DESC
)

Flags for defining sort element and order.

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 = false
)

IgnoreComments can be set to 'true', to ignore lines that start with # The todo.txt format does not define comments.

Functions

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.

func WriteToFilename

func WriteToFilename(tasklist *TaskList, filename string) error

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

func (task *Task) HasCompletedDate() bool

HasCompletedDate returns true if the task has a completed date.

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) 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) 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 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 (*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 LoadFromFilename

func LoadFromFilename(filename string) (TaskList, error)

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

func (tasklist *TaskList) ArchiveTaskToFile(task Task, filename string) error

ArchiveTaskToFile removes the task from the active list and concatenates it to the passed in filename Return an err if any part of that fails

func (*TaskList) Filter

func (tasklist *TaskList) Filter(predicate func(Task) bool) *TaskList

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

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

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

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

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(sortFlag int) error

Sort allows a TaskList to be sorted by certain predefined fields. See constants SORT_* for fields and sort order.

func (TaskList) String

func (tasklist TaskList) String() (text 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.

func (*TaskList) WriteToFilename

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

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

Jump to

Keyboard shortcuts

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