From 28e436f73d02c835fe070a10c540aaba84e125fa Mon Sep 17 00:00:00 2001 From: Kevin Schoon Date: Sat, 7 Aug 2021 12:09:00 -0400 Subject: [PATCH] allow deleting a range of tasks --- pkg/cmd/cmd.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go index 2c8fdc9..8540c48 100644 --- a/pkg/cmd/cmd.go +++ b/pkg/cmd/cmd.go @@ -8,6 +8,8 @@ import ( "os/user" "path" "sort" + "strconv" + "strings" "time" cli "github.com/jawher/mow.cli" @@ -28,6 +30,26 @@ func defaultConfigPath() string { 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) { return func(cmd *cli.Cmd) { cmd.Spec = "[OPTIONS] MESSAGE" @@ -193,14 +215,40 @@ func list(config *pomo.Config) func(*cli.Cmd) { func _delete(config *pomo.Config) func(*cli.Cmd) { return func(cmd *cli.Cmd) { - cmd.Spec = "[OPTIONS] TASK_ID" - var taskID = cmd.IntArg("TASK_ID", -1, "task to delete") + cmd.Spec = "[OPTIONS] [TASK_ID...]" + 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() { + db, err := pomo.NewStore(config.DBPath) maybe(err) defer db.Close() 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 })) } }