package main import ( "fmt" "strconv" "time" "git.bullercodeworks.com/brian/gime" ) func cmdContinueTimer(args []string) int { // Get the last running timer and start a new one with the same tags te, err := getMostRecentTimeEntry() if err != nil { fmt.Println(err.Error()) return 1 } tagColl := te.GetTags() var tags []string for i := 0; i < tagColl.Length(); i++ { tags = append(tags, tagColl.Get(i)) } args = append(args, tags...) return cmdStartTimer(args) } // switchTimer performs a stop on any currently running timers // and starts a new timer with the given arguments func cmdSwitchTimer(args []string) int { loadActiveTimeEntries() tm := time.Now() if timeEntries.Length() > 0 { fmt.Println("Stopped Timers:") } for i := 0; i < timeEntries.Length(); i++ { tmr := timeEntries.Get(i) tmr.SetEnd(tm) if err := gdb.UpdateTimeEntry(tmr); err != nil { fmt.Println(err.Error()) return 1 } fmt.Println(" " + TimerToString(tmr)) } fmt.Println("Started Timer:") return cmdStartTimer(args) } // cmdStartTimer takes a list of arguments and returns the return code // to be passed along to os.Exit func cmdStartTimer(args []string) int { var err error var tm time.Time tagStart := 0 if len(args) > 0 { // Check if the first argument looks like a date/time tm, err = parseFuzzyTime(args[0]) } if len(args) == 0 || err != nil { // Just start it now tm = time.Now() } else { tagStart = 1 } var entry *gime.TimeEntry var timerArgs []string if tagStart < len(args) { timerArgs = args[tagStart:] } tc := new(gime.TagCollection) for i := range timerArgs { tc.Push(timerArgs[i]) } if entry, err = gime.CreateTimeEntry(tm, time.Time{}, tc); err != nil { fmt.Println(err) return 1 } if err = gdb.SaveTimeEntry(entry); err != nil { fmt.Println(err) return 1 } fmt.Println(" " + TimerToString(entry)) return 0 } // cmdStopTimer takes parameters that describe which times to stop func cmdStopTimer(args []string) int { // args[0] should either be a timer id (starting with '@') or 'all' var err error tm := time.Now() actTimers := gdb.LoadTimeEntryCollection(gime.TypeCurrent) var tmr *gime.TimeEntry stopId := "@0" // By default, stop the first timer for i := range args { if args[i] == "all" || args[i][0] == '@' { stopId = args[i] continue } tmpTm, err := parseFuzzyTime(args[i]) if err == nil { // We found a time tm = tmpTm continue } } if stopId != "all" { // Find the timer that we're stopping timerId, err := strconv.Atoi(stopId[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 } } stopTimer := func(tmr *gime.TimeEntry, at time.Time) int { tmr.SetEnd(at) if err = gdb.UpdateTimeEntry(tmr); err != nil { fmt.Println(err.Error()) return 1 } fmt.Println("Stopped:", InferTimerDetailString(tmr)) return 0 } if stopId == "all" { var ret int for i := 0; i < actTimers.Length(); i++ { ret += stopTimer(actTimers.Get(i), tm) } if ret > 0 { return 1 // One or more stop operations failed } return 0 } // Just stop the one timer return stopTimer(tmr, tm) } // cmdDeleteTimer takes parameters that describe the timers to be deleted. func cmdDeleteTimer(args []string) int { var err error if len(args) < 1 || args[0][0] != '@' { fmt.Println("Couldn't determine which timer(s) to delete") return 1 } // We've got a timer id to delete timerId, err := strconv.Atoi(args[0][1:]) if err != nil { fmt.Println("Error parsing timer id: " + err.Error()) return 1 } tmr, tp, err := findTimerById(timerId) if err != nil { fmt.Println(err.Error()) return 1 } if gdb.RemoveTimeEntry(tmr.GetUUID()) != nil { fmt.Println("Error removing entry " + gime.TypeToString(tp) + "." + tmr.GetUUID()) return 1 } fmt.Println("Deleted Time Entry: " + TimerToString(tmr)) return 0 }