package main import ( "fmt" "os" "os/exec" "strconv" "strings" todotxt "git.bullercodeworks.com/brian/go-todotxt" ) func (a *AppState) opEditor(args []string) int { editor := os.Getenv("EDITOR") if editor == "" { fmt.Println("No EDITOR set") return 1 } fmt.Println("Starting", editor, a.getTodoFile()) c := exec.Command(editor, a.getTodoFile()) c.Stdin = os.Stdin c.Stdout = os.Stdout if err := c.Run(); err != nil { fmt.Println(err) return 1 } return 0 } func (a *AppState) opI3Status(args []string) int { var filterString string state := "Idle" if len(args) > 0 { filterString = strings.Join(args, " ") a.TodoList = a.getFilteredList(filterString) } total := len(a.TodoList.Todos) var incomplete int for _, v := range a.TodoList.Todos { 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) for _, v := range t.Todos { if !v.HasCompletedDate() { state = "Warning" break } } } } fmt.Printf("{\"icon\":\"tasks\",\"state\":\"%s\", \"text\": \"%s: %d/%d\"}", state, filterString, (total - incomplete), total) return 0 } func (a *AppState) opListTodos(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.TodoList = a.getFilteredList(filterString) } a.TodoList.Sort(todotxt.SortPriorityAsc) for _, v := range a.TodoList.Todos { fmt.Println(a.getTodoString(*v)) lastIdx++ } if showAll { if err := app.LoadDoneList(); err != nil { fmt.Println(a.s(ResStrErrLoadingDoneList)) fmt.Println(err.Error()) return 1 } if filterString != "" { a.DoneList = a.getFilteredList(filterString) } for _, v := range a.DoneList.Todos { fmt.Println(a.getDoneTodoString(*v)) } } return 0 } func (a *AppState) opAddTodo(args []string) int { taskString := strings.Join(args, " ") if err := a.addTodo(taskString); err != nil { fmt.Println("Error adding task") fmt.Println(err.Error()) return 1 } return 0 } func (a *AppState) opToggleTodoComplete(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.toggleTodoComplete(id); err != nil { fmt.Println(err.Error()) return 1 } } if a.WriteList() != nil { fmt.Println(a.s(ResStrErrSavingList)) return 1 } return 0 } func (a *AppState) opArchiveTodos(args []string) int { if len(args) > 0 { for _, v := range args { var id int var task *todotxt.Todo 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.TodoList.GetTodo(id); err != nil { fmt.Printf("Error getting task %d\n", id) return 1 } if err = a.archiveTodo(id); err != nil { fmt.Printf("Error archiving task %d\n", id) return 1 } fmt.Println(a.getDoneTodoString(*task)) } } else { for _, v := range a.TodoList.Todos { if v.Completed { if err := a.archiveTodo(v.Id); err != nil { fmt.Printf("Error archiving task %d\n", v.Id) return 1 } fmt.Println(a.getDoneTodoString(*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 }