gask/task_ops.go

141 lines
2.9 KiB
Go
Raw Normal View History

2019-02-15 17:15:26 +00:00
package main
import (
"fmt"
"strconv"
"strings"
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 {
fmt.Println("Error loading 'Done' list")
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
}
if err := a.WriteList(); err != nil {
fmt.Println("Error saving task list")
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 {
fmt.Println("Error saving list")
return 1
}
return 0
}
func (a *AppState) opArchiveTasks(args []string) int {
if err := app.LoadDoneList(); err != nil {
fmt.Println("Error loading 'Done' list")
fmt.Println(err.Error())
return 1
}
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))
}
}
}
if a.WriteDoneList() != nil {
fmt.Println("Error saving archive list")
return 1
}
if a.WriteList() != nil {
fmt.Println("Error saving task list")
return 1
}
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
}