From 004ca7ab5bf5ae80e0a7c97cec85a7bcdf6070f9 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 3 Apr 2023 10:37:40 -0500 Subject: [PATCH] v1.1 --- cmd/break.go | 4 +--- cmd/config.go | 4 +--- cmd/i3.go | 23 +++++++++++++++++------ cmd/root.go | 13 +++++++++---- cmd/start.go | 8 ++++---- cmd/stop.go | 8 ++++---- cmd/toggle.go | 25 +++++++++++++++++++++++++ 7 files changed, 61 insertions(+), 24 deletions(-) create mode 100644 cmd/toggle.go diff --git a/cmd/break.go b/cmd/break.go index 6c87141..9298317 100644 --- a/cmd/break.go +++ b/cmd/break.go @@ -21,6 +21,4 @@ var breakCmd = &cobra.Command{ }, } -func init() { - rootCmd.AddCommand(breakCmd) -} +func init() { rootCmd.AddCommand(breakCmd) } diff --git a/cmd/config.go b/cmd/config.go index 068db9d..94780eb 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -23,9 +23,7 @@ var configCmd = &cobra.Command{ RunE: opConfig, } -func init() { - rootCmd.AddCommand(configCmd) -} +func init() { rootCmd.AddCommand(configCmd) } func opConfig(cmd *cobra.Command, args []string) error { updConfig := make(map[string]string) diff --git a/cmd/i3.go b/cmd/i3.go index c2dd451..d4bccc5 100644 --- a/cmd/i3.go +++ b/cmd/i3.go @@ -17,13 +17,24 @@ var i3Cmd = &cobra.Command{ Use: "i3", Short: "Print state to stdout in i3 bar format", Run: func(cmd *cobra.Command, args []string) { - if len(args) == 1 && args[0] == "click" { - if viper.GetBool("active") { - breakCmd.Run(cmd, []string{}) - } else { - startCmd.Run(cmd, []string{}) + if len(args) == 1 { + switch args[0] { + case "toggle": + toggleCmd.Run(cmd, []string{"-s"}) + case "click": + if viper.GetBool("active") { + breakCmd.Run(cmd, []string{"-s"}) + } else { + startCmd.Run(cmd, []string{"-s"}) + } + case "status": + if viper.GetBool("active") { + fmt.Print("{\"text\":\"\uf04c\",\"state\":\"Good\"}") + } else { + fmt.Print("{\"text\":\"\uf04b\"}") + } + return } - return } if viper.GetBool("active") { lastBreak := viper.GetTime("lastbreak") diff --git a/cmd/root.go b/cmd/root.go index 948147e..8525be2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -43,8 +43,6 @@ func init() { cobra.OnInitialize(initConfig) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.breaktime.yaml)") - - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } // initConfig reads in config file and ENV variables if set. @@ -104,6 +102,13 @@ func initConfig() { } } -func runRootCmd(cmd *cobra.Command, args []string) error { - return nil +func runRootCmd(cmd *cobra.Command, args []string) error { return nil } + +func hasArg(arg string, args []string) bool { + for i := range args { + if args[i] == arg { + return true + } + } + return false } diff --git a/cmd/start.go b/cmd/start.go index 957bd42..4448de7 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -18,10 +18,10 @@ var startCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { viper.Set("active", true) viper.WriteConfig() - fmt.Println("Timer Started.") + if !hasArg("-s", args) { + fmt.Println("Timer Started.") + } }, } -func init() { - rootCmd.AddCommand(startCmd) -} +func init() { rootCmd.AddCommand(startCmd) } diff --git a/cmd/stop.go b/cmd/stop.go index 88ad809..072c317 100644 --- a/cmd/stop.go +++ b/cmd/stop.go @@ -18,10 +18,10 @@ var stopCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { viper.Set("active", false) viper.WriteConfig() - fmt.Println("Timer Stopped.") + if !hasArg("-s", args) { + fmt.Println("Timer Stopped.") + } }, } -func init() { - rootCmd.AddCommand(stopCmd) -} +func init() { rootCmd.AddCommand(stopCmd) } diff --git a/cmd/toggle.go b/cmd/toggle.go new file mode 100644 index 0000000..165c7c3 --- /dev/null +++ b/cmd/toggle.go @@ -0,0 +1,25 @@ +/* +Copyright © 2023 Brian Buller + +*/ +package cmd + +import ( + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +// toggleCmd represents the toggle command +var toggleCmd = &cobra.Command{ + Use: "toggle", + Short: "Toggle timer off/on", + Run: func(cmd *cobra.Command, args []string) { + if viper.GetBool("active") { + stopCmd.Run(cmd, args) + } else { + startCmd.Run(cmd, args) + } + }, +} + +func init() { rootCmd.AddCommand(toggleCmd) }