From 5fb79dfe299c22a2dcd884fada37c99afe2c69c9 Mon Sep 17 00:00:00 2001 From: ugol Date: Tue, 13 Feb 2024 09:20:31 +0100 Subject: [PATCH] added --nocolor to several list/show commands --- pkg/cmd/emitterList.go | 14 +++++++++++--- pkg/cmd/emitterShow.go | 12 ++++++++++-- pkg/cmd/functionList.go | 27 +++++++++++++++++++-------- pkg/cmd/producerList.go | 11 +++++++++-- pkg/cmd/templateList.go | 2 +- pkg/cmd/templateShow.go | 2 +- 6 files changed, 51 insertions(+), 17 deletions(-) diff --git a/pkg/cmd/emitterList.go b/pkg/cmd/emitterList.go index ae52cb44..f97b5200 100644 --- a/pkg/cmd/emitterList.go +++ b/pkg/cmd/emitterList.go @@ -31,13 +31,19 @@ var emitterListCmd = &cobra.Command{ Long: `List all available emitters`, Run: func(cmd *cobra.Command, args []string) { + noColor, _ := cmd.Flags().GetBool("nocolor") + + var Green = "" + var Reset = "" + if !noColor { + Green = "\033[32m" + Reset = "\033[0m" + } + fmt.Println() fmt.Println("List of JR emitters:") fmt.Println() - var Green = "\033[32m" - var Reset = "\033[0m" - for _, v := range emitters { fmt.Printf("%s%s%s\n", Green, v.Name, Reset) } @@ -48,4 +54,6 @@ var emitterListCmd = &cobra.Command{ func init() { emitterCmd.AddCommand(emitterListCmd) + emitterListCmd.Flags().BoolP("nocolor", "n", false, "Do not color output") + } diff --git a/pkg/cmd/emitterShow.go b/pkg/cmd/emitterShow.go index f12996ed..d565a193 100644 --- a/pkg/cmd/emitterShow.go +++ b/pkg/cmd/emitterShow.go @@ -33,8 +33,14 @@ jr emitter show net_device`, Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - var Green = "\033[32m" - var Reset = "\033[0m" + noColor, _ := cmd.Flags().GetBool("nocolor") + + var Green = "" + var Reset = "" + if !noColor { + Green = "\033[32m" + Reset = "\033[0m" + } fmt.Println() for _, v := range emitters { @@ -61,4 +67,6 @@ jr emitter show net_device`, func init() { emitterCmd.AddCommand(emitterShowCmd) + emitterShowCmd.Flags().BoolP("nocolor", "n", false, "Do not color output") + } diff --git a/pkg/cmd/functionList.go b/pkg/cmd/functionList.go index 30f05067..eaf555c6 100644 --- a/pkg/cmd/functionList.go +++ b/pkg/cmd/functionList.go @@ -67,23 +67,24 @@ func doList(cmd *cobra.Command, args []string) { find, _ := cmd.Flags().GetBool("find") run, _ := cmd.Flags().GetBool("run") isMarkdown, _ := cmd.Flags().GetBool("markdown") + noColor, _ := cmd.Flags().GetBool("nocolor") if category { for k, v := range functions.DescriptionMap() { if strings.Contains(v.Category, args[0]) { - printFunction(k, isMarkdown) + printFunction(k, isMarkdown, noColor) } } } else if find { for k, v := range functions.DescriptionMap() { if strings.Contains(v.Description, args[0]) || strings.Contains(v.Name, args[0]) { - printFunction(k, isMarkdown) + printFunction(k, isMarkdown, noColor) } } } else if len(args) == 1 { if run { - f, found := printFunction(args[0], isMarkdown) + f, found := printFunction(args[0], isMarkdown, noColor) if found { fmt.Println() cmd := exec.Command("/bin/sh", "-c", f.Example) @@ -92,13 +93,13 @@ func doList(cmd *cobra.Command, args []string) { cmd.Run() } } else { - printFunction(args[0], isMarkdown) + printFunction(args[0], isMarkdown, noColor) } } else { count := 0 for k := range functions.FunctionsMap() { count++ - printFunction(k, isMarkdown) + printFunction(k, isMarkdown, noColor) } fmt.Println() fmt.Printf("Total functions: %d\n", count) @@ -106,12 +107,17 @@ func doList(cmd *cobra.Command, args []string) { fmt.Println() } -func printFunction(name string, isMarkdown bool) (functions.FunctionDescription, bool) { +func printFunction(name string, isMarkdown bool, noColor bool) (functions.FunctionDescription, bool) { f, found := functions.Description(name) + var Cyan = "" + var Reset = "" + if !noColor { + Cyan = "\033[36m" + Reset = "\033[0m" + } + if !isMarkdown { - var Reset = "\033[0m" - var Cyan = "\033[36m" if found { fmt.Println() @@ -151,9 +157,14 @@ func init() { functionCmd.AddCommand(functionListCmd) functionListCmd.Flags().BoolP("category", "c", false, "IndexOf in category") functionListCmd.Flags().BoolP("find", "f", false, "IndexOf in description and name") + functionListCmd.Flags().BoolP("markdown", "m", false, "Output the list as markdown") functionListCmd.Flags().BoolP("run", "r", false, "Run the example") + functionListCmd.Flags().BoolP("nocolor", "n", false, "Do not color output") + functionManCmd.Flags().BoolP("category", "c", false, "IndexOf in category") functionManCmd.Flags().BoolP("markdown", "m", false, "Output the list as markdown") functionManCmd.Flags().BoolP("find", "f", false, "IndexOf in description and name") functionManCmd.Flags().BoolP("run", "r", false, "Run the example") + functionManCmd.Flags().BoolP("nocolor", "n", false, "Do not color output") + } diff --git a/pkg/cmd/producerList.go b/pkg/cmd/producerList.go index 057d8248..411f05d3 100644 --- a/pkg/cmd/producerList.go +++ b/pkg/cmd/producerList.go @@ -32,8 +32,14 @@ var producerListCmd = &cobra.Command{ "jr producer list", Run: func(cmd *cobra.Command, args []string) { - var Green = "\033[32m" - var Reset = "\033[0m" + noColor, _ := cmd.Flags().GetBool("nocolor") + + var Green = "" + var Reset = "" + if !noColor { + Green = "\033[32m" + Reset = "\033[0m" + } fmt.Println() fmt.Println("List of JR producers:") @@ -53,4 +59,5 @@ var producerListCmd = &cobra.Command{ func init() { producerCmd.AddCommand(producerListCmd) + producerListCmd.Flags().BoolP("nocolor", "n", false, "Do not color output") } diff --git a/pkg/cmd/templateList.go b/pkg/cmd/templateList.go index 110fe390..fcb25585 100644 --- a/pkg/cmd/templateList.go +++ b/pkg/cmd/templateList.go @@ -112,5 +112,5 @@ func isValidTemplate(t []byte) (bool, error) { func init() { templateCmd.AddCommand(templateListCmd) templateListCmd.Flags().BoolP("fullPath", "f", false, "Print full path") - templateListCmd.Flags().BoolP("nocolor", "n", false, "Do not color the output") + templateListCmd.Flags().BoolP("nocolor", "n", false, "Do not color output") } diff --git a/pkg/cmd/templateShow.go b/pkg/cmd/templateShow.go index 33112af5..c68665a5 100644 --- a/pkg/cmd/templateShow.go +++ b/pkg/cmd/templateShow.go @@ -73,5 +73,5 @@ var templateShowCmd = &cobra.Command{ func init() { templateCmd.AddCommand(templateShowCmd) - templateShowCmd.Flags().BoolP("nocolor", "n", false, "disable colorized output") + templateShowCmd.Flags().BoolP("nocolor", "n", false, "Do not color output") }