allow deleting a range of tasks

This commit is contained in:
Kevin Schoon 2021-08-07 12:09:00 -04:00
parent a897198dae
commit 28e436f73d
1 changed files with 51 additions and 3 deletions

View File

@ -8,6 +8,8 @@ import (
"os/user" "os/user"
"path" "path"
"sort" "sort"
"strconv"
"strings"
"time" "time"
cli "github.com/jawher/mow.cli" cli "github.com/jawher/mow.cli"
@ -28,6 +30,26 @@ func defaultConfigPath() string {
return path.Join(u.HomeDir, "/.pomo/config.json") return path.Join(u.HomeDir, "/.pomo/config.json")
} }
func parseRange(arg string) (int, int, error) {
if strings.Contains(arg, ":") {
split := strings.Split(arg, ":")
start, err := strconv.ParseInt(split[0], 0, 64)
if err != nil {
return -1, -1, err
}
end, err := strconv.ParseInt(split[1], 0, 64)
if err != nil {
return -1, -1, err
}
return int(start), int(end), nil
}
n, err := strconv.ParseInt(arg, 0, 64)
if err != nil {
return -1, -1, err
}
return int(n), int(n), err
}
func start(config *pomo.Config) func(*cli.Cmd) { func start(config *pomo.Config) func(*cli.Cmd) {
return func(cmd *cli.Cmd) { return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS] MESSAGE" cmd.Spec = "[OPTIONS] MESSAGE"
@ -193,14 +215,40 @@ func list(config *pomo.Config) func(*cli.Cmd) {
func _delete(config *pomo.Config) func(*cli.Cmd) { func _delete(config *pomo.Config) func(*cli.Cmd) {
return func(cmd *cli.Cmd) { return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS] TASK_ID" cmd.Spec = "[OPTIONS] [TASK_ID...]"
var taskID = cmd.IntArg("TASK_ID", -1, "task to delete") cmd.LongDesc = `
delete one or more tasks by ID
## Examples:
# delete a single task
pomo delete 1
# delete a range of tasks (1 - 10)
pomo delete 1:10
# delete multiple tasks 5, 10, and 20
pomo delete 5 10 20
`
var taskIDs = cmd.StringsArg("TASK_ID", nil, "task to delete")
cmd.Action = func() { cmd.Action = func() {
db, err := pomo.NewStore(config.DBPath) db, err := pomo.NewStore(config.DBPath)
maybe(err) maybe(err)
defer db.Close() defer db.Close()
maybe(db.With(func(tx *sql.Tx) error { maybe(db.With(func(tx *sql.Tx) error {
return db.DeleteTask(tx, *taskID) for _, expr := range *taskIDs {
start, end, err := parseRange(expr)
if err != nil {
return err
}
for i := start; i <= end; i++ {
err := db.DeleteTask(tx, i)
if err != nil {
return err
}
fmt.Printf("deleted task %d\n", i)
}
}
return nil
})) }))
} }
} }