gime/cmd/list.go
Brian Buller 54c91f0d3c Dev a few more operations
* Archive
* Config
* Pull out filter generation (from list) for reuse
2022-01-20 08:51:39 -06:00

101 lines
2.2 KiB
Go

/*
Copyright © 2022 Brian Buller <brian@bullercodeworks.com>
*/
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
if includeArchive {
for _, tmr := range p.DoneList.GetTimerSlice() {
list.AddTimer(tmr)
}
}
} else {
list = p.DoneList
}
filter := util.BuildFilterFromArgs(args)
list = list.Filter(filter)
dayTotals := make(map[string]time.Duration)
for _, v := range *list {
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
for _, v := range *list {
oldDayStr = dayStr
dayStr = v.StartDate.Format("2006/01/02")
if dayStr != oldDayStr {
// TODO:
wrkDur := dayTotals[dayStr].Round(util.GetRoundToDuration())
fmtStr := dayStr + " ( %.2f )\n"
fmt.Printf(fmtStr, util.DurationToDecimal(wrkDur))
}
fmt.Println(" " + util.TimerToFriendlyString(v))
}
return nil
}