'Modify' function that allows adding tags

This commit is contained in:
Brian Buller 2018-04-16 17:17:22 -05:00
parent 90dfd0f245
commit 3ada0ba277
2 changed files with 49 additions and 10 deletions

10
main.go
View File

@ -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'",

View File

@ -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
}
}