gask/task_ops.go

159 lines
3.3 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, " ")
2023-09-07 19:39:45 +00:00
a.TodoList = a.getFilteredList(filterString)
2021-04-29 15:11:51 +00:00
}
2023-09-07 19:39:45 +00:00
total := len(a.TodoList.Todos)
2021-04-29 15:11:51 +00:00
var incomplete int
2023-09-07 19:39:45 +00:00
for _, v := range a.TodoList.Todos {
2021-04-29 15:11:51 +00:00
if !v.HasCompletedDate() {
incomplete++
if dateWithinNextDay(v.DueDate) {
state = "Warning"
}
}
}
if state != "Warning" {
warning := a.config.Get("i3status_warning")
if warning != "" {
t := a.getFilteredList(warning)
2023-09-07 19:39:45 +00:00
for _, v := range t.Todos {
if !v.HasCompletedDate() {
state = "Warning"
break
}
2021-04-29 15:11:51 +00:00
}
}
}
fmt.Printf("{\"icon\":\"tasks\",\"state\":\"%s\", \"text\": \"%s: %d/%d\"}", state, filterString, (total - incomplete), total)
2021-04-29 15:11:51 +00:00
return 0
}
2023-09-07 19:39:45 +00:00
func (a *AppState) opListTodos(args []string) int {
2019-02-15 17:15:26 +00:00
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, " ")
2023-09-07 19:39:45 +00:00
a.TodoList = a.getFilteredList(filterString)
2019-02-15 17:15:26 +00:00
}
2021-03-17 17:37:26 +00:00
2023-09-07 19:39:45 +00:00
a.TodoList.Sort(todotxt.SortPriorityAsc)
2021-03-17 17:37:26 +00:00
2023-09-07 19:39:45 +00:00
for _, v := range a.TodoList.Todos {
fmt.Println(a.getTodoString(*v))
2019-02-15 17:15:26 +00:00
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)
}
2023-09-07 19:39:45 +00:00
for _, v := range a.DoneList.Todos {
fmt.Println(a.getDoneTodoString(*v))
2019-02-15 17:15:26 +00:00
}
}
return 0
}
2023-09-07 19:39:45 +00:00
func (a *AppState) opAddTodo(args []string) int {
2019-02-15 17:15:26 +00:00
taskString := strings.Join(args, " ")
2023-09-07 19:39:45 +00:00
if err := a.addTodo(taskString); err != nil {
2019-02-15 17:15:26 +00:00
fmt.Println("Error adding task")
fmt.Println(err.Error())
return 1
}
return 0
}
2023-09-07 19:39:45 +00:00
func (a *AppState) opToggleTodoComplete(args []string) int {
2019-02-15 17:15:26 +00:00
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
}
2023-09-07 19:39:45 +00:00
if err := a.toggleTodoComplete(id); err != nil {
2019-02-15 17:15:26 +00:00
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
}
2023-09-07 19:39:45 +00:00
func (a *AppState) opArchiveTodos(args []string) int {
2019-02-15 17:15:26 +00:00
if len(args) > 0 {
for _, v := range args {
var id int
2023-09-07 19:39:45 +00:00
var task *todotxt.Todo
2019-02-15 17:15:26 +00:00
var err error
if id, err = strconv.Atoi(v); err != nil {
fmt.Printf("Invalid id given: %s\n", v)
return 1
}
2023-09-07 19:39:45 +00:00
if task, err = a.TodoList.GetTodo(id); err != nil {
2019-02-15 17:15:26 +00:00
fmt.Printf("Error getting task %d\n", id)
return 1
}
2023-09-07 19:39:45 +00:00
if err = a.archiveTodo(id); err != nil {
2019-02-15 17:15:26 +00:00
fmt.Printf("Error archiving task %d\n", id)
return 1
}
2023-09-07 19:39:45 +00:00
fmt.Println(a.getDoneTodoString(*task))
2019-02-15 17:15:26 +00:00
}
} else {
2023-09-07 19:39:45 +00:00
for _, v := range a.TodoList.Todos {
2019-02-15 17:15:26 +00:00
if v.Completed {
2023-09-07 19:39:45 +00:00
if err := a.archiveTodo(v.Id); err != nil {
2019-02-15 17:15:26 +00:00
fmt.Printf("Error archiving task %d\n", v.Id)
return 1
}
2023-09-07 19:39:45 +00:00
fmt.Println(a.getDoneTodoString(*v))
2019-02-15 17:15:26 +00:00
}
}
}
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
}