Implemented total output

This commit is contained in:
マリウス 2020-10-17 20:19:05 +01:00
parent fda944ab17
commit 35cb75088e
No known key found for this signature in database
GPG key ID: C228EF0A530AF06F

View file

@ -5,8 +5,11 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/shopspring/decimal"
) )
var listTotalTime bool
var listCmd = &cobra.Command{ var listCmd = &cobra.Command{
Use: "list", Use: "list",
Short: "List activities", Short: "List activities",
@ -46,10 +49,17 @@ var listCmd = &cobra.Command{
os.Exit(1) os.Exit(1)
} }
totalHours := decimal.NewFromInt(0);
for _, entry := range filteredEntries { for _, entry := range filteredEntries {
duration := entry.Finish.Sub(entry.Begin)
durationDec := decimal.NewFromFloat(duration.Hours())
totalHours = totalHours.Add(durationDec)
fmt.Printf("%s\n", entry.GetOutput()) fmt.Printf("%s\n", entry.GetOutput())
} }
if listTotalTime == true {
fmt.Printf("\nTOTAL: %s H\n\n", totalHours.StringFixed(2))
}
return return
}, },
} }
@ -60,6 +70,7 @@ func init() {
listCmd.Flags().StringVar(&until, "until", "", "Date/time to list until") listCmd.Flags().StringVar(&until, "until", "", "Date/time to list until")
listCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be listed") listCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be listed")
listCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be listed") listCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be listed")
listCmd.Flags().BoolVar(&listTotalTime, "total", false, "Show total time of hours for listed activities")
var err error var err error
database, err = InitDatabase() database, err = InitDatabase()