gime/cmd/list.go

101 lines
2.2 KiB
Go
Raw Permalink Normal View History

2022-01-19 18:56:31 +00:00
/*
2022-01-19 21:30:30 +00:00
Copyright © 2022 Brian Buller <brian@bullercodeworks.com>
2022-01-19 18:56:31 +00:00
*/
package cmd
import (
"fmt"
"time"
"git.bullercodeworks.com/brian/gime/cli"
"git.bullercodeworks.com/brian/gime/util"
"git.bullercodeworks.com/brian/go-timertxt"
"github.com/spf13/cobra"
)
// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls", "lsa", "lsd"},
Short: "List Timers",
RunE: opListTimers,
}
func init() {
rootCmd.AddCommand(listCmd)
// Local Flags
listCmd.Flags().BoolP("all", "a", false, "Include done.txt timers")
listCmd.Flags().BoolP("done", "d", false, "Only done.txt timers")
}
func opListTimers(cmd *cobra.Command, args []string) error {
var err error
var includeArchive bool
var onlyArchive bool
//onlyArchive := cmd.CalledAs() == "lsd" || listCmd.Flags().Lookup
if onlyArchive, err = cmd.Flags().GetBool("done"); err != nil {
return err
} else if includeArchive, err = cmd.Flags().GetBool("all"); err != nil {
return err
}
if cmd.CalledAs() == "lsd" {
onlyArchive = true
} else if cmd.CalledAs() == "lsa" {
includeArchive = true
}
p := cli.Program{}
err = p.Initialize()
if err != nil {
return err
}
if !onlyArchive {
if err := p.LoadTimerList(); err != nil {
return err
}
}
if includeArchive || onlyArchive {
if err := p.LoadDoneList(); err != nil {
return err
}
}
list := timertxt.NewTimerList()
if !onlyArchive {
list = p.TimerList
2022-01-19 18:56:31 +00:00
if includeArchive {
for _, tmr := range p.DoneList.GetTimerSlice() {
list.AddTimer(tmr)
2022-01-19 18:56:31 +00:00
}
}
} else {
list = p.DoneList
2022-01-19 18:56:31 +00:00
}
filter := util.BuildFilterFromArgs(args)
list = list.Filter(filter)
2022-01-19 18:56:31 +00:00
dayTotals := make(map[string]time.Duration)
2023-01-12 12:04:17 +00:00
for _, v := range list.GetTimerSlice() {
2022-01-19 18:56:31 +00:00
dur := v.FinishDate.Sub(v.StartDate)
if v.FinishDate.IsZero() {
dur = time.Now().Sub(v.StartDate)
}
dayTotals[v.StartDate.Format("2006/01/02")] += dur
}
var oldDayStr, dayStr string
2023-01-12 12:04:17 +00:00
for _, v := range list.GetTimerSlice() {
2022-01-19 18:56:31 +00:00
oldDayStr = dayStr
dayStr = v.StartDate.Format("2006/01/02")
if dayStr != oldDayStr {
// TODO:
2022-10-04 17:37:11 +00:00
wrkDur := util.Round(dayTotals[dayStr])
2022-01-19 18:56:31 +00:00
fmtStr := dayStr + " ( %.2f )\n"
fmt.Printf(fmtStr, util.DurationToDecimal(wrkDur))
}
fmt.Println(" " + util.TimerToFriendlyString(v))
}
return nil
}