github.com/KEINOS/go-todotxt
is a Go package for parsing and editing todo.txt files, a text format for task annotations designed by Gina Trapani.
Note: This package is based on the following packages with custom user sort functionality.
- todotxt from Kevin Tang
- go-todotxt from Fabio Berchtold
// Download the package.
go get "github.com/KEINOS/go-todotxt"
// Import the package.
import "github.com/KEINOS/go-todotxt/todo"
func Example() {
// Load tasks from a string.
// You can also load from a file by using LoadFromFile().
tasks, err := todo.LoadFromString(`
(A) Call Mom @Phone +Family
x (A) Schedule annual checkup +Health
(C) Add cover sheets @Office +TPSReports
Plan backyard herb garden @Home
Pick up milk @GroceryStore
Research self-publishing services +Novel @Computer
x Download Todo.txt mobile app @Phone
(A) This is a task should be due before yesterday due:2020-11-15
`)
if err != nil {
log.Fatal(err)
}
// Add a new task.
// Note the order of project ("+Novel") and context ("@Computer") later
// in the output.
newTask, err := todo.ParseTask("(B) Outline chapter 5 +Novel @Computer")
if err != nil {
log.Fatal(err)
}
tasks.AddTask(newTask) // append to the end of the list
// Sort tasks by priority and then by context in ascending order.
if err := tasks.Sort(todo.SortPriorityAsc, todo.SortContextAsc); err != nil {
log.Fatal(err)
}
// AND filter.
// Get tasks that have any priority AND are not completed AND are not overdue.
filteredTasks := tasks.
Filter(todo.FilterHasPriority).
Filter(todo.FilterNotCompleted).
Filter(todo.FilterNot(todo.FilterOverdue)) // NOT overdue
// OR filter.
// Filter the above tasks with priority "A" OR has project "Novel" OR has
// context "Office".
filteredTasks = filteredTasks.Filter(
todo.FilterByPriority("A"), // has (A)
todo.FilterByProject("Novel"), // has +Novel
todo.FilterByContext("Office"), // has @Office
)
// Print the filtered tasks.
for _, task := range filteredTasks {
fmt.Println(task.String())
}
// Output:
// (A) Call Mom @Phone +Family
// (B) Outline chapter 5 @Computer +Novel
// (C) Add cover sheets @Office +TPSReports
}
func ExampleTaskList_CustomSort() {
tasks, err := todo.LoadFromString(`
Task 3
Task 1
Task 4
Task 2
`)
if err != nil {
log.Fatal(err)
}
// customFunc returns true if taskA is less than taskB.
customFunc := func(taskA, taskB todo.Task) bool {
// Compare strings of the text part of the task.
return taskA.Todo < taskB.Todo
}
tasks.CustomSort(customFunc)
fmt.Println(tasks[0].Todo)
fmt.Println(tasks[1].Todo)
fmt.Println(tasks[2].Todo)
fmt.Println(tasks[3].Todo)
// Output:
// Task 1
// Task 2
// Task 3
// Task 4
}
- Image from: https://github.com/todotxt/todo.txt
Any contribution for the better is welcome. We provide full code coverage of unit tests, so feel free to refactor or play around with the code.
- Branch to PR:
main
(Draft PR is recommended)
- Open an issue
- Please attach a simple and reproducible test code if possible. This helps us alot and to fix the issue faster.
- CI/CD:
-
The below tests will run on Push/Pull Request via GitHub Actions. You need to pass all the tests before requesting a review.
- Unit testing on various Go versions (1.22 ... latest)
- Unit testing on various platforms (Linux, macOS, Windows)
- Static analysis/lint check by golangci-lint.
- Configuration: .golangci.yml
-
To run tests locally, we provide a convenient Makefile. Please run the below command to run all the tests (
docker
andcompose
are required).# Runs unit tests on Go 1.22 to latest and `golangci-lint` check. make test
-
Note : Please DO NOT PR to the
original
branch but tomain
branch. The branchoriginal
is simply a copy from themaster
branch of the upstream repo. This is for the purpose of keeping the original code as is and contribute to the upstream.
- MIT License. Copyright (c) 2022 KEINOS and the go-todotxt contributors with all the respect to Kevin Tang, Fabio Berchtold and Gina Trapani.
- This package is based on the below packages and ideas:
- Mother/Upstream: todotxt authored by Kevin Tang @ MIT
- Grand Mother/Most upstream: go-todotxt authored by Fabio Berchtold @ MPL-2.0
- Origin: todo.txt is an awesome task format. Initially designed by Gina Trapani. @ GPL-3.0