gime/cmd/time.go

56 lines
1.0 KiB
Go
Raw Normal View History

2022-01-19 18:56:31 +00:00
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"time"
"git.bullercodeworks.com/brian/gime/cli"
"git.bullercodeworks.com/brian/gime/util"
"github.com/spf13/cobra"
)
// timeCmd represents the time command
var timeCmd = &cobra.Command{
Use: "time",
Short: "A brief description of your command",
RunE: opShowTimers,
}
func init() {
rootCmd.AddCommand(timeCmd)
}
func opShowTimers(cmd *cobra.Command, args []string) error {
var err error
p := cli.Program{}
err = p.Initialize()
if err != nil {
return err
}
if err := p.LoadTimerList(); err != nil {
return err
}
list := p.GetFilteredTimerList(args)
var isActive bool
var total time.Duration
for _, v := range *list {
dur := v.FinishDate.Sub(v.StartDate)
if v.FinishDate.IsZero() {
dur = time.Now().Sub(v.StartDate)
isActive = true
}
total += dur
}
total = total.Round(util.GetRoundToDuration())
if isActive {
fmt.Printf("%.2f+\n", util.DurationToDecimal(total))
} else {
fmt.Printf("%.2f\n", util.DurationToDecimal(total))
}
return nil
}