Skip to content
forked from 1set/todotxt

todo.txt parser in Golang with custom sort feature.

License

Notifications You must be signed in to change notification settings

KEINOS/go-todotxt

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-todotxt

go1.22+ Go Reference License

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.

Usage

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

Todo.txt format

Contributing

go1.22+ Go Reference

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:
  • 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.
    • To run tests locally, we provide a convenient Makefile. Please run the below command to run all the tests (docker and compose 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 to main branch. The branch original is simply a copy from the master branch of the upstream repo. This is for the purpose of keeping the original code as is and contribute to the upstream.

Statuses

UnitTests golangci-lint CodeQL-Analysis PlatformTests

codecov Go Report Card

License and Credits

About

todo.txt parser in Golang with custom sort feature.

Resources

License

Security policy

Stars

Watchers

Forks

Languages

  • Go 99.1%
  • Makefile 0.9%