2019-02-15 17:15:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
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) opListTasks(args []string) int {
|
|
|
|
var lastIdx int
|
|
|
|
var filterString string
|
|
|
|
showAll := len(args) > 0 && args[0] == "-1"
|
|
|
|
if showAll {
|
|
|
|
args = args[1:]
|
|
|
|
}
|
|
|
|
if len(args) > 0 {
|
|
|
|
filterString = strings.Join(args, " ")
|
|
|
|
a.TaskList = a.getFilteredList(filterString)
|
|
|
|
}
|
|
|
|
for _, v := range *a.TaskList {
|
|
|
|
fmt.Println(a.getTaskString(v))
|
|
|
|
lastIdx++
|
|
|
|
}
|
|
|
|
if showAll {
|
|
|
|
if err := app.LoadDoneList(); err != nil {
|
2019-02-26 20:23:12 +00:00
|
|
|
fmt.Println(a.s(ResStrErrLoadingDoneList))
|
2019-02-15 17:15:26 +00:00
|
|
|
fmt.Println(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if filterString != "" {
|
|
|
|
a.DoneList = a.getFilteredList(filterString)
|
|
|
|
}
|
|
|
|
for _, v := range *a.DoneList {
|
|
|
|
fmt.Println(a.getDoneTaskString(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppState) opAddTask(args []string) int {
|
|
|
|
taskString := strings.Join(args, " ")
|
|
|
|
if err := a.addTask(taskString); err != nil {
|
|
|
|
fmt.Println("Error adding task")
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppState) opToggleTaskComplete(args []string) int {
|
|
|
|
if len(args) == 0 {
|
|
|
|
fmt.Println("No id given")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if len(args) == 0 {
|
|
|
|
fmt.Println("No id given")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
for _, v := range args {
|
|
|
|
var id int
|
|
|
|
var err error
|
|
|
|
if id, err = strconv.Atoi(v); err != nil {
|
|
|
|
fmt.Printf("Invalid id given: %s\n", v)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if err := a.toggleTaskComplete(id); err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if a.WriteList() != nil {
|
2019-02-26 20:23:12 +00:00
|
|
|
fmt.Println(a.s(ResStrErrSavingList))
|
2019-02-15 17:15:26 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppState) opArchiveTasks(args []string) int {
|
|
|
|
if len(args) > 0 {
|
|
|
|
for _, v := range args {
|
|
|
|
var id int
|
|
|
|
var task *todotxt.Task
|
|
|
|
var err error
|
|
|
|
if id, err = strconv.Atoi(v); err != nil {
|
|
|
|
fmt.Printf("Invalid id given: %s\n", v)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if task, err = a.TaskList.GetTask(id); err != nil {
|
|
|
|
fmt.Printf("Error getting task %d\n", id)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if err = a.archiveTask(id); err != nil {
|
|
|
|
fmt.Printf("Error archiving task %d\n", id)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
fmt.Println(a.getDoneTaskString(*task))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, v := range *a.TaskList {
|
|
|
|
if v.Completed {
|
|
|
|
if err := a.archiveTask(v.Id); err != nil {
|
|
|
|
fmt.Printf("Error archiving task %d\n", v.Id)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
fmt.Println(a.getDoneTaskString(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppState) opPrintUsage(args []string) int {
|
|
|
|
for _, v := range a.ValidOperations {
|
|
|
|
for _, vv := range v {
|
|
|
|
fmt.Println(" " + vv)
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|