From 3ada0ba27725e0f20c5d90e2144b09a6dc7e556d Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 16 Apr 2018 17:17:22 -0500 Subject: [PATCH] 'Modify' function that allows adding tags --- main.go | 10 +++++++++ timer_operations.go | 49 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index bd08978..7f025b7 100644 --- a/main.go +++ b/main.go @@ -262,6 +262,16 @@ func initialize() { "ls [duration] [+tags] - The same as list", } + opFuncs["modify"] = cmdModifyTimer + validOperations["modify"] = []string{ + "modify [+tags] - Modify a timer", + } + + opFuncs["mod"] = cmdModifyTimer + validOperations["mod"] = []string{ + "mod [+tags] - Modify a timer", + } + opFuncs["remove"] = cmdDeleteTimer validOperations["remove"] = []string{ "remove @id - See 'delete'", diff --git a/timer_operations.go b/timer_operations.go index 77a71b9..97943da 100644 --- a/timer_operations.go +++ b/timer_operations.go @@ -90,6 +90,41 @@ func cmdAddTimer(args []string) int { return 0 } +func cmdModifyTimer(args []string) int { + // If no timer id is specified, edit the most recent one + modId := "@0" + tags, rem := pullTagsFromArgs(args) + for i := range rem { + if rem[i][0] == '@' { + modId = rem[i] + } + } + timerId, err := strconv.Atoi(modId[1:]) + if err != nil { + fmt.Println("error parsing timer id:", err.Error()) + return 1 + } + tmr, _, err := findTimerById(timerId) + if err != nil { + fmt.Println(err.Error()) + return 1 + } + if len(tags) <= 0 { + fmt.Println("Modify only supports adding tags at this time.") + return 1 + } + for i := range tags { + tmr.AddTag(tags[i]) + } + if err = gdb.SaveTimeEntry(tmr); err != nil { + fmt.Println("Error saving modified timer:", err.Error()) + return 1 + } + fmt.Println("Modified Timer:") + fmt.Println(" ", TimerToString(tmr)) + return 0 +} + func cmdContinueTimer(args []string) int { // Get the last running timer and start a new one with the same tags te, err := getMostRecentTimeEntry() @@ -217,8 +252,8 @@ func cmdPrintList(args []string) int { useDefaultFilter := true var showIds bool var beg, end time.Time - searchTags := []string{} - for _, opt := range args { + tags, rem := pullTagsFromArgs(args) + for _, opt := range rem { var tmpBeg, tmpEnd time.Time // Check for command modifiers if strings.HasPrefix(opt, ":") { @@ -245,12 +280,6 @@ func cmdPrintList(args []string) int { continue } - // Find tags - if strings.HasPrefix(opt, "+") { - searchTags = append(searchTags, opt[1:]) - continue - } - // Do our best to figure out what timers the user wants to list var err error if strings.Contains(opt, "-") { @@ -288,8 +317,8 @@ func cmdPrintList(args []string) int { return t.GetStart().After(beg) && t.GetEnd().Before(end) } tagFilter := func(t *gime.TimeEntry) bool { - for i := range searchTags { - if !t.HasTag(searchTags[i]) { + for i := range tags { + if !t.HasTag(tags[i]) { return false } }