4 Commits
1.2.0 ... 1.4.0

Author SHA1 Message Date
JamesClonk
97a991fa26 fix overdue test case 2018-01-01 11:29:49 +01:00
Fabio Berchtold
e5c37a1106 Update README.md 2016-12-25 19:21:23 +01:00
JamesClonk
dbb967d319 updated Readme.md 2014-01-18 11:40:04 +01:00
JamesClonk
2e04f1d233 GetTask bugfix
correctly return pointer to Task inside TaskList.
2014-01-16 12:18:21 +01:00
5 changed files with 80 additions and 19 deletions

View File

@@ -3,7 +3,7 @@ go-todotxt
A Go todo.txt library. A Go todo.txt library.
[![GoDoc](https://godoc.org/github.com/JamesClonk/go-todotxt?status.png)](https://godoc.org/github.com/JamesClonk/go-todotxt) [![Build Status](https://travis-ci.org/JamesClonk/go-todotxt.png?branch=master)](https://travis-ci.org/JamesClonk/go-todotxt) [![Codebot](https://codebot.io/badge/github.com/JamesClonk/go-todotxt.png)](http://codebot.io/doc/pkg/github.com/JamesClonk/go-todotxt "Codebot") [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/JamesClonk/go-todotxt/trend.png)](https://bitdeli.com/free "Bitdeli Badge") [![GoDoc](https://godoc.org/github.com/JamesClonk/go-todotxt?status.png)](https://godoc.org/github.com/JamesClonk/go-todotxt) [![Build Status](https://travis-ci.org/JamesClonk/go-todotxt.png?branch=master)](https://travis-ci.org/JamesClonk/go-todotxt)
The *todotxt* package is a Go client library for Gina Trapani's [todo.txt](https://github.com/ginatrapani/todo.txt-cli/) files. The *todotxt* package is a Go client library for Gina Trapani's [todo.txt](https://github.com/ginatrapani/todo.txt-cli/) files.
It allows for parsing and manipulating of task lists and tasks in the todo.txt format. It allows for parsing and manipulating of task lists and tasks in the todo.txt format.
@@ -35,16 +35,30 @@ go-todotxt requires Go1.1 or higher.
log.Fatal(err) log.Fatal(err)
} }
// tasklist now contains a slice of Tasks
fmt.Printf("Task 2, todo: %v\n", tasklist[1].Todo) fmt.Printf("Task 2, todo: %v\n", tasklist[1].Todo)
fmt.Printf("Task 3: %v\n", tasklist[2]) fmt.Printf("Task 3: %v\n", tasklist[2])
fmt.Printf("Task 4, has priority: %v\n\n", tasklist[3].HasPriority()) fmt.Printf("Task 4, has priority: %v\n\n", tasklist[3].HasPriority())
fmt.Print(tasklist) fmt.Print(tasklist)
// Filter list to get only completed tasks // Filter list to get only completed tasks
completedList := testTasklist.Filter(func(t Task) bool { completedList := tasklist.Filter(func(t Task) bool {
return t.Completed return t.Completed
}) })
fmt.Print(completedList) 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")
} }
``` ```

View File

@@ -1,6 +1,6 @@
2013-02-22 Pick up milk @GroceryStore 2013-02-22 Pick up milk @GroceryStore
x Download Todo.txt mobile app @Phone x Download Todo.txt mobile app @Phone
(B) 2013-12-01 Outline chapter 5 @Computer +Novel Level:5 private:false due:2014-02-17 (C) 2013-12-01 Go home! @Computer +Novel Level:5 private:false due:2011-11-11
x 2014-01-02 (B) 2013-12-30 Create golang library test cases @Go +go-todotxt x 2014-01-02 (B) 2013-12-30 Create golang library test cases @Go +go-todotxt
x 2014-01-03 2014-01-01 Create some more golang library test cases @Go +go-todotxt x 2014-01-03 2014-01-01 Create some more golang library test cases @Go +go-todotxt
(B) 2013-12-01 Outline chapter 5 @Computer +Novel Level:5 private:false due:2014-02-17 (B) 2013-12-01 Outline chapter 5 @Computer +Novel Level:5 private:false due:2014-02-17

View File

@@ -1,6 +1,6 @@
# String test cases # String test cases
2013-02-22 Pick up milk @GroceryStore 2013-02-22 Pick up milk @GroceryStore
x Download Todo.txt mobile app @Phone x Download Todo.txt mobile app @Phone
(B) 2013-12-01 private:false Outline chapter 5 +Novel @Computer Level:5 due:2014-02-17 (B) 2013-12-01 private:false Outline chapter 5 +Novel @Computer Level:5 due:2014-02-17
x 2014-01-02 (B) 2013-12-30 Create golang library test cases @Go +go-todotxt x 2014-01-02 (B) 2013-12-30 Create golang library test cases @Go +go-todotxt
x 2014-01-03 2014-01-01 Create some more golang library test cases @Go +go-todotxt x 2014-01-03 2014-01-01 Create some more golang library test cases @Go +go-todotxt
@@ -9,15 +9,15 @@ x 2014-01-03 2014-01-01 Create some more golang library test cases @Go +go-todot
(B) 2013-12-01 private:false Outline chapter 5 +Novel @Computer Level:5 due:2014-02-17 (B) 2013-12-01 private:false Outline chapter 5 +Novel @Computer Level:5 due:2014-02-17
x (C) 2014-01-01 Create golang library documentation @Go +go-todotxt due:2014-01-12 x (C) 2014-01-01 Create golang library documentation @Go +go-todotxt due:2014-01-12
x 2014-01-02 (B) 2013-12-30 Create golang library test cases @Go +go-todotxt x 2014-01-02 (B) 2013-12-30 Create golang library test cases @Go +go-todotxt
+Gardening Plan backyard herb garden +Planning @Home +Improving +Gardening Plan backyard herb garden +Planning @Home +Improving
# CreatedDate test cases # CreatedDate test cases
(A) 2012-01-30 @Phone Call Mom @Call +Family (A) 2012-01-30 @Phone Call Mom @Call +Family
2013-02-22 Pick up milk @GroceryStore 2013-02-22 Pick up milk @GroceryStore
x (C) 2014-01-01 Create golang library documentation @Go +go-todotxt due:2014-01-12 x (C) 2014-01-01 Create golang library documentation @Go +go-todotxt due:2014-01-12
x 2014-01-02 (B) 2013-12-30 Create golang library test cases @Go +go-todotxt x 2014-01-02 (B) 2013-12-30 Create golang library test cases @Go +go-todotxt
x 2014-01-03 2014-01-01 Create some more golang library test cases @Go +go-todotxt x 2014-01-03 2014-01-01 Create some more golang library test cases @Go +go-todotxt
(C) Add cover sheets @Office +TPSReports (C) Add cover sheets @Office +TPSReports
# Contexts test cases # Contexts test cases
(A) 2012-01-30 @Phone Call Mom @Call +Family (A) 2012-01-30 @Phone Call Mom @Call +Family
@@ -31,7 +31,7 @@ Research self-publishing services +Novel +Novel +Novel due:2014-01-01
@Home Turn off TV @Electricity @Television @Electricity Importance:Very! @Home Turn off TV @Electricity @Television @Electricity Importance:Very!
# DueDate test cases # DueDate test cases
(B) 2013-12-01 private:false Outline chapter 5 +Novel @Computer due:2014-02-17 Level:5 (B) 2013-12-01 private:false Outline chapter 5 +Novel @Computer due:2014-02-17 Level:5
x 2014-01-02 (B) 2013-12-30 Create golang library test cases @Go +go-todotxt x 2014-01-02 (B) 2013-12-30 Create golang library test cases @Go +go-todotxt
# AdditionalTags test cases # AdditionalTags test cases
@@ -57,7 +57,7 @@ x 2014-01-03 2014-01-01 Create some more golang library test cases @Go +go-todot
# Overdue test cases # Overdue test cases
x 2014-01-04 (B) 2013-12-30 Create golang library @Go +go-todotxt due:2014-01-02 x 2014-01-04 (B) 2013-12-30 Create golang library @Go +go-todotxt due:2014-01-02
(B) 2013-12-01 private:false Outline chapter 5 +Novel @Computer due:2017-07-17 Level:5 (B) 2013-12-01 private:false Outline chapter 5 +Novel @Computer due:2027-07-17 Level:5
Research self-publishing services +Novel +Novel +Novel due:2014-01-01 Research self-publishing services +Novel +Novel +Novel due:2014-01-01
x 2014-01-03 2014-01-01 Create some more golang library test cases @Go +go-todotxt x 2014-01-03 2014-01-01 Create some more golang library test cases @Go +go-todotxt

View File

@@ -43,7 +43,7 @@ func (tasklist TaskList) String() (text string) {
return text return text
} }
// AddTask appends a task to the current TaskList and takes care to set the Task.Id correctly, modifying the Task by the given pointer! // 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 *TaskList) AddTask(task *Task) { func (tasklist *TaskList) AddTask(task *Task) {
task.Id = 0 task.Id = 0
for _, t := range *tasklist { for _, t := range *tasklist {
@@ -56,18 +56,19 @@ func (tasklist *TaskList) AddTask(task *Task) {
*tasklist = append(*tasklist, *task) *tasklist = append(*tasklist, *task)
} }
// GetTask returns task by given task 'id' from the TaskList. Returns an error if task could not be found. // 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 *TaskList) GetTask(id int) (*Task, error) { func (tasklist *TaskList) GetTask(id int) (*Task, error) {
for _, t := range *tasklist { for i := range *tasklist {
if t.Id == id { if ([]Task(*tasklist))[i].Id == id {
return &t, nil return &([]Task(*tasklist))[i], nil
} }
} }
return nil, errors.New("task not found") return nil, errors.New("task not found")
} }
// RemoveTaskById removes any task with given task 'id' from the TaskList. // RemoveTaskById removes any Task with given Task 'id' from the TaskList.
// Returns an error if no task was removed. // Returns an error if no Task was removed.
func (tasklist *TaskList) RemoveTaskById(id int) error { func (tasklist *TaskList) RemoveTaskById(id int) error {
var newList TaskList var newList TaskList
@@ -87,8 +88,8 @@ func (tasklist *TaskList) RemoveTaskById(id int) error {
return nil return nil
} }
// RemoveTask removes any task from the TaskList with the same String representation as the given task. // RemoveTask removes any Task from the TaskList with the same String representation as the given Task.
// Returns an error if no task was removed. // Returns an error if no Task was removed.
func (tasklist *TaskList) RemoveTask(task Task) error { func (tasklist *TaskList) RemoveTask(task Task) error {
var newList TaskList var newList TaskList

View File

@@ -329,6 +329,52 @@ func TestTaskListGetTask(t *testing.T) {
taskId++ taskId++
} }
func TestTaskListUpdateTask(t *testing.T) {
if err := testTasklist.LoadFromFilename(testInputTasklist); err != nil {
t.Fatal(err)
}
taskId := 3
task, err := testTasklist.GetTask(taskId)
if err != nil {
t.Error(err)
}
testExpected = "(B) 2013-12-01 Outline chapter 5 @Computer +Novel Level:5 private:false due:2014-02-17"
testGot = task.String()
if testGot != testExpected {
t.Errorf("Expected Task[%d] to be [%s], but got [%s]", taskId, testExpected, testGot)
}
testExpected = 3
testGot = testTasklist[taskId-1].Id
if testGot != testExpected {
t.Errorf("Expected Task[%d] to be [%d], but got [%d]", taskId, testExpected, testGot)
}
task.Priority = "C"
task.Todo = "Go home!"
date, err := time.Parse(DateLayout, "2011-11-11")
if err != nil {
t.Error(err)
}
task.DueDate = date
testGot := task
os.Remove(testOutput)
if err := testTasklist.WriteToFilename(testOutput); err != nil {
t.Fatal(err)
}
if err := testTasklist.LoadFromFilename(testOutput); err != nil {
t.Fatal(err)
}
testExpected, err := testTasklist.GetTask(taskId)
if err != nil {
t.Error(err)
}
if testGot.Task() != testExpected.Task() {
t.Errorf("Expected Task to be [%v]\n, but got [%v]", testExpected, testGot)
}
}
func TestTaskListRemoveTaskById(t *testing.T) { func TestTaskListRemoveTaskById(t *testing.T) {
if err := testTasklist.LoadFromFilename(testInputTasklist); err != nil { if err := testTasklist.LoadFromFilename(testInputTasklist); err != nil {
t.Fatal(err) t.Fatal(err)