2019-02-15 17:15:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-02-20 13:49:41 +00:00
|
|
|
todotxt "github.com/br0xen/go-todotxt"
|
2019-02-15 17:15:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (a *AppState) addTask(taskString string) error {
|
|
|
|
t, err := todotxt.ParseTask(taskString)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if t.CreatedDate.IsZero() {
|
|
|
|
t.CreatedDate = time.Now()
|
|
|
|
}
|
|
|
|
a.TaskList.AddTask(t)
|
2019-02-21 16:47:08 +00:00
|
|
|
return a.WriteList()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppState) toggleTaskComplete(id int) error {
|
|
|
|
var task *todotxt.Task
|
|
|
|
var err error
|
|
|
|
if task, err = a.TaskList.GetTask(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if task.Completed {
|
|
|
|
task.Reopen()
|
|
|
|
} else {
|
|
|
|
task.Complete()
|
|
|
|
}
|
2019-02-21 16:47:08 +00:00
|
|
|
return a.WriteList()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppState) archiveTask(id int) error {
|
|
|
|
var err error
|
|
|
|
var task *todotxt.Task
|
|
|
|
if task, err = a.TaskList.GetTask(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-20 13:49:41 +00:00
|
|
|
if err := a.TaskList.ArchiveTaskToFile(*task, app.getDoneFile()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.TaskList.RemoveTask(*task)
|
2019-02-21 16:47:08 +00:00
|
|
|
return a.WriteList()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppState) unarchiveTask(id int) error {
|
|
|
|
var err error
|
|
|
|
var task *todotxt.Task
|
|
|
|
if task, err = a.DoneList.GetTask(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.TaskList.AddTask(task)
|
|
|
|
if err = a.WriteList(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.DoneList.RemoveTask(*task)
|
|
|
|
return a.WriteDoneList()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppState) getFilterPredicate(filter string) func(todotxt.Task) bool {
|
|
|
|
var predicates []func(todotxt.Task) bool
|
|
|
|
// If none of the 'filter' is in upper-case, do a case-insensitive filter
|
|
|
|
checkCase := true
|
|
|
|
if strings.ToLower(filter) == filter {
|
|
|
|
checkCase = false
|
|
|
|
}
|
|
|
|
filterParts := strings.Split(filter, " ")
|
|
|
|
for _, part := range filterParts {
|
|
|
|
if strings.HasPrefix(part, "@") {
|
|
|
|
predicates = append(predicates, func(t todotxt.Task) bool {
|
|
|
|
for _, v := range t.Contexts {
|
|
|
|
if "@"+v == part {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
} else if strings.HasPrefix(part, "+") {
|
|
|
|
predicates = append(predicates, func(t todotxt.Task) bool {
|
|
|
|
for _, v := range t.Projects {
|
|
|
|
if "+"+v == part {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
predicates = append(predicates, func(t todotxt.Task) bool {
|
|
|
|
val := t.Original
|
|
|
|
if !checkCase {
|
|
|
|
val = strings.ToLower(t.Original)
|
|
|
|
}
|
|
|
|
return strings.Contains(val, part)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return func(t todotxt.Task) bool {
|
|
|
|
for _, v := range predicates {
|
|
|
|
if v(t) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppState) LoadTaskList() error {
|
|
|
|
var err error
|
|
|
|
var tl todotxt.TaskList
|
|
|
|
tl, err = todotxt.LoadFromFilename(a.getTodoFile())
|
|
|
|
a.TaskList = &tl
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppState) LoadDoneList() error {
|
|
|
|
var err error
|
|
|
|
var tl todotxt.TaskList
|
|
|
|
tl, err = todotxt.LoadFromFilename(a.getDoneFile())
|
|
|
|
a.DoneList = &tl
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppState) WriteList() error {
|
|
|
|
return a.TaskList.WriteToFilename(a.getTodoFile())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppState) WriteDoneList() error {
|
|
|
|
return a.DoneList.WriteToFilename(a.getDoneFile())
|
|
|
|
}
|