From 54c91f0d3c50b3176baab3f178c5dab24024979c Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 20 Jan 2022 08:51:39 -0600 Subject: [PATCH] Dev a few more operations * Archive * Config * Pull out filter generation (from list) for reuse --- cli/cli.go | 8 +++++ cmd/archive.go | 34 ++++++++++++++++------ cmd/config.go | 22 ++++++++------ cmd/editor.go | 10 ++----- cmd/list.go | 77 +++++-------------------------------------------- util/helpers.go | 70 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 125 insertions(+), 96 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index 73eb695..52bf93a 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -163,3 +163,11 @@ func (p *Program) GetMostRecentTimer() (*timertxt.Timer, error) { } return work, nil } + +func (p *Program) GetTimerString(timer *timertxt.Timer) string { + return fmt.Sprintf("%d. %s", timer.Id, strings.TrimPrefix(timer.String(), "x ")) +} + +func (p *Program) GetDoneTimerString(timer *timertxt.Timer) string { + return fmt.Sprintf("--. %s", timer.String()) +} diff --git a/cmd/archive.go b/cmd/archive.go index 5f6bb0e..634988f 100644 --- a/cmd/archive.go +++ b/cmd/archive.go @@ -7,20 +7,16 @@ package cmd import ( "fmt" + "git.bullercodeworks.com/brian/gime/cli" + "git.bullercodeworks.com/brian/gime/util" "github.com/spf13/cobra" ) // archiveCmd represents the archive command var archiveCmd = &cobra.Command{ Use: "archive", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - RunE: opArchive, + Short: "Archive timers to the done file", + RunE: opArchive, } func init() { @@ -28,6 +24,26 @@ func init() { } func opArchive(cmd *cobra.Command, args []string) error { - fmt.Println("archive called") + var err error + p := cli.Program{} + err = p.Initialize() + if err != nil { + return err + } + if err = p.LoadTimerList(); err != nil { + return err + } + + filter := util.BuildFilterFromArgs(args) + list := p.TimerList.Filter(filter) + for _, v := range list.GetTimerSlice() { + if v.Finished { + if err := p.ArchiveTimer(v); err != nil { + return fmt.Errorf("Error archiving task %d: %w", v.Id, err) + } + fmt.Println(p.GetDoneTimerString(v)) + } + } + return nil } diff --git a/cmd/config.go b/cmd/config.go index 1967dfb..1194d65 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -6,21 +6,17 @@ package cmd import ( "fmt" + "sort" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // configCmd represents the config command var configCmd = &cobra.Command{ Use: "config", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - RunE: opConfig, + Short: "Print all configuration values", + RunE: opConfig, } func init() { @@ -28,6 +24,14 @@ func init() { } func opConfig(cmd *cobra.Command, args []string) error { - fmt.Println("config called") + var settings []string + for k, v := range viper.AllSettings() { + settings = append(settings, fmt.Sprintf("%s: %s", k, v)) + } + sort.Strings(settings) + fmt.Println("Configuration File:", viper.ConfigFileUsed()) + for _, v := range settings { + fmt.Println(v) + } return nil } diff --git a/cmd/editor.go b/cmd/editor.go index 71bd8c6..318e0d0 100644 --- a/cmd/editor.go +++ b/cmd/editor.go @@ -13,14 +13,8 @@ import ( // editorCmd represents the editor command var editorCmd = &cobra.Command{ Use: "editor", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - RunE: opEditor, + Short: "Open either the timer.txt file or the done.txt file in the default editor ", + RunE: opEditor, } func init() { diff --git a/cmd/list.go b/cmd/list.go index 180a551..c60d1c3 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -6,7 +6,6 @@ package cmd import ( "fmt" - "strings" "time" "git.bullercodeworks.com/brian/gime/cli" @@ -26,12 +25,6 @@ var listCmd = &cobra.Command{ func init() { rootCmd.AddCommand(listCmd) - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // listCmd.PersistentFlags().String("foo", "", "A help for foo") - // Local Flags listCmd.Flags().BoolP("all", "a", false, "Include done.txt timers") listCmd.Flags().BoolP("done", "d", false, "Only done.txt timers") @@ -68,75 +61,19 @@ func opListTimers(cmd *cobra.Command, args []string) error { return err } } - start := time.Time{} - end := time.Now() - var contextFilters []string - var projectFilters []string - - var allFilters []func(timertxt.Timer) bool - - if len(args) > 0 { - contextFilters, args = util.GetContextsFromSlice(args) - projectFilters, args = util.GetProjectsFromSlice(args) - } - if len(args) > 0 { - if start, err = util.ParseFuzzyTime(args[0]); err != nil { - y, m, d := time.Now().Date() - start = time.Date(y, m, d, 0, 0, 0, 0, time.Now().Location()) - } else { - args = args[1:] - } - if len(args) > 0 { - if end, err = util.ParseFuzzyTime(args[0]); err != nil { - y, m, d := time.Now().Date() - end = time.Date(y, m, d, 23, 59, 59, 0, time.Now().Location()) - } else { - args = args[1:] - } - } - } - list := timertxt.NewTimerList() if !onlyArchive { - list = p.TimerList.GetTimersInRange(start, end) + list = p.TimerList if includeArchive { - *list = append(*list, (*p.DoneList.GetTimersInRange(start, end))...) + for _, tmr := range p.DoneList.GetTimerSlice() { + list.AddTimer(tmr) + } } } else { - list = p.DoneList.GetTimersInRange(start, end) + list = p.DoneList } - if len(contextFilters) > 0 { - allFilters = append(allFilters, func(t timertxt.Timer) bool { - for _, v := range contextFilters { - v = strings.TrimPrefix(v, "@") - if !t.HasContext(v) { - return false - } - } - return true - }) - } - if len(projectFilters) > 0 { - allFilters = append(allFilters, func(t timertxt.Timer) bool { - for _, v := range projectFilters { - v = strings.TrimPrefix(v, "+") - if !t.HasProject(v) { - return false - } - } - return true - }) - } - doFilters := func(t *timertxt.Timer) bool { - for _, v := range allFilters { - if !v(*t) { - return false - } - } - // If we made it all the way down here, it matches - return true - } - list = list.Filter(doFilters) + filter := util.BuildFilterFromArgs(args) + list = list.Filter(filter) dayTotals := make(map[string]time.Duration) for _, v := range *list { diff --git a/util/helpers.go b/util/helpers.go index eacec00..a9ce052 100644 --- a/util/helpers.go +++ b/util/helpers.go @@ -312,3 +312,73 @@ func BeginningOfMonth() time.Time { now := time.Now() return time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()) } + +func BuildFilterFromArgs(args []string) func(*timertxt.Timer) bool { + start := time.Time{} + end := time.Now() + var contextFilters []string + var projectFilters []string + var allFilters []func(timertxt.Timer) bool + if len(args) > 0 { + contextFilters, args = GetContextsFromSlice(args) + projectFilters, args = GetProjectsFromSlice(args) + } + if len(args) > 0 { + var err error + if start, err = ParseFuzzyTime(args[0]); err != nil { + y, m, d := time.Now().Date() + start = time.Date(y, m, d, 0, 0, 0, 0, time.Now().Location()) + } else { + args = args[1:] + } + if len(args) > 0 { + if end, err = ParseFuzzyTime(args[0]); err != nil { + y, m, d := time.Now().Date() + end = time.Date(y, m, d, 23, 59, 59, 0, time.Now().Location()) + } else { + args = args[1:] + } + } + } + allFilters = append(allFilters, func(t timertxt.Timer) bool { + if t.StartDate.Before(end) && t.StartDate.After(start) { + return true + } + if t.FinishDate.Before(end) && t.FinishDate.After(start) { + return true + } + return false + }) + if len(contextFilters) > 0 { + allFilters = append(allFilters, func(t timertxt.Timer) bool { + for _, v := range contextFilters { + v = strings.TrimPrefix(v, "@") + if !t.HasContext(v) { + return false + } + } + return true + }) + } + if len(projectFilters) > 0 { + allFilters = append(allFilters, func(t timertxt.Timer) bool { + for _, v := range projectFilters { + v = strings.TrimPrefix(v, "+") + if !t.HasProject(v) { + return false + } + } + return true + }) + } + doFilters := func(t *timertxt.Timer) bool { + for _, v := range allFilters { + if !v(*t) { + return false + } + } + // If we made it all the way down here, it matches + return true + } + return doFilters +}