gask-old/task_ops.go

155 lines
3.2 KiB
Go
Raw Normal View History

2019-02-15 17:15:26 +00:00
package main
import (
"fmt"
"strconv"
"strings"
2021-03-17 17:37:26 +00:00
todotxt "git.bullercodeworks.com/brian/go-todotxt"
2019-02-15 17:15:26 +00:00
)
2021-04-29 15:11:51 +00:00
func (a *AppState) opI3Status(args []string) int {
var filterString string
state := "Idle"
if len(args) > 0 {
filterString = strings.Join(args, " ")
a.TaskList = a.getFilteredList(filterString)
}
total := len(*a.TaskList)
var incomplete int
for _, v := range *a.TaskList {
if !v.HasCompletedDate() {
incomplete++
if dateWithinNextDay(v.DueDate) {
state = "Warning"
}
}
}
if state != "Warning" {
warning := a.config.Get("i3status_warning")
if warning != "" {
if len(*a.getFilteredList(warning)) > 0 {
state = "Warning"
}
}
}
fmt.Printf("{\"icon\":\"tasks\",\"state\":\"%s\", \"text\": \"%s: %d/%d\"}", state, filterString, incomplete, total)
return 0
}
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)
}
2021-03-17 17:37:26 +00:00
a.TaskList.Sort(todotxt.SORT_USEFULNESS_ASC)
2019-02-15 17:15:26 +00:00
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
}