Skip to content

Commit

Permalink
cmd: Add inc/dec timers
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphexion committed Sep 6, 2021
1 parent 2ef526e commit 9836c03
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions cmd/timers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"nina/mid"
"nina/noko"
"nina/utils"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -62,13 +63,29 @@ func NewTimerCmd() *cobra.Command {
Run: deleteCmdFunc,
}

incCmd := &cobra.Command{
Use: "inc 2h10m",
Short: "Increase the running timer",
Args: cobra.MinimumNArgs(1),
Run: incCmdFunc,
}

decCmd := &cobra.Command{
Use: "dec 2h10m",
Short: "Decrease the running timer",
Args: cobra.MinimumNArgs(1),
Run: decCmdFunc,
}

rootCmd.AddCommand(listCmd)
rootCmd.AddCommand(pauseCmd)
rootCmd.AddCommand(unpauseCmd)
rootCmd.AddCommand(noteCmd)
rootCmd.AddCommand(createCmd)
rootCmd.AddCommand(logCmd)
rootCmd.AddCommand(deleteCmd)
rootCmd.AddCommand(incCmd)
rootCmd.AddCommand(decCmd)

return rootCmd
}
Expand Down Expand Up @@ -179,6 +196,40 @@ func deleteCmdFunc(cmd *cobra.Command, args []string) {
outputTimer(timer)
}

func incCmdFunc(cmd *cobra.Command, args []string) {
minutes, err := utils.MinutesFromHMFormat(args[0])
if err != nil {
log.Fatal(err)
return
}

addOrSubMinutesOnRunningTimer(+minutes)
}

func decCmdFunc(cmd *cobra.Command, args []string) {
minutes, err := utils.MinutesFromHMFormat(args[0])
if err != nil {
log.Fatal(err)
return
}

addOrSubMinutesOnRunningTimer(-minutes)
}

func addOrSubMinutesOnRunningTimer(minutes int) {
timer, err := mid.GetRunningTimer()
if err != nil {
log.Fatal(err)
}

err = mid.AddOrSubTimer(timer, minutes)
if err != nil {
log.Fatal(err)
}

outputTimerWithName(timer.Project.Name)
}

// outputTimerWithName retreives the latest state of the timer
// before outputing it.
func outputTimerWithName(name string) {
Expand Down

0 comments on commit 9836c03

Please sign in to comment.