From 725b6ea33fd65b31fa0718bc446d4e7d2704af19 Mon Sep 17 00:00:00 2001 From: Maxime Labelle Date: Sat, 25 Mar 2023 11:44:09 +0100 Subject: [PATCH] Added colorized output. (#1) * Added colorized output. Signed-off-by: Springcomp * Colorized compact output. Signed-off-by: Springcomp * Using simpler code. Signed-off-by: Springcomp * Made function private (really this time) Signed-off-by: Springcomp * Documented NO_COLOR environment variable. Signed-off-by: Springcomp * Using updated upstream jsoncolor. Signed-off-by: Springcomp --------- Signed-off-by: Springcomp --- go.mod | 5 +++++ go.sum | 18 ++++++++++++++++++ jp.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index b818279..2b5072e 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,11 @@ require ( require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect + github.com/fatih/color v1.15.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.17 // indirect + github.com/nwidger/jsoncolor v0.3.2 github.com/russross/blackfriday/v2 v2.1.0 // indirect golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 // indirect + golang.org/x/sys v0.6.0 // indirect ) diff --git a/go.sum b/go.sum index 50f5c77..84612eb 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,21 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= +github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= +github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= github.com/jmespath-community/go-jmespath v1.1.0 h1:jDWE/sGR5ELgzAczUNX/maQFmh3BeLsvQX/0PXZBrA0= github.com/jmespath-community/go-jmespath v1.1.0/go.mod h1:sY3q31u+PPDRp5yLcckgzg6eltf6Dw8nUkDYuRH8OtI= +github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= +github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/nwidger/jsoncolor v0.3.2 h1:rVJJlwAWDJShnbTYOQ5RM7yTA20INyKXlJ/fg4JMhHQ= +github.com/nwidger/jsoncolor v0.3.2/go.mod h1:Cs34umxLbJvgBMnVNVqhji9BhoT/N/KinHqZptQ7cf4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= @@ -21,6 +34,11 @@ github.com/urfave/cli v1.22.12 h1:igJgVw1JdKH+trcLWLeLwZjU9fEfPesQ+9/e4MQ44S8= github.com/urfave/cli v1.22.12/go.mod h1:sSBEIC79qR6OvcmsD4U3KABeOTxDqQtdDnaFuUN30b8= golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 h1:Jvc7gsqn21cJHCmAWx0LiimpP18LZmUxkT5Mp7EZ1mI= golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/jp.go b/jp.go index 8a90f6e..80a9f07 100644 --- a/jp.go +++ b/jp.go @@ -7,6 +7,7 @@ import ( "os" "github.com/jmespath-community/go-jmespath" + "github.com/nwidger/jsoncolor" "github.com/urfave/cli" ) @@ -32,6 +33,11 @@ func main() { Name: "expr-file, e", Usage: "Read JMESPath expression from the specified file.", }, + cli.StringFlag{ + Name: "color", + Value: "auto", + Usage: "Change the color setting (none, auto, always). auto is based on whether output is a tty.", + }, cli.BoolFlag{ Name: "unquoted, u", Usage: "If the final result is a string, it will be printed without quotes.", @@ -71,6 +77,20 @@ func runMain(c *cli.Context) int { } expression = c.Args()[0] } + switch c.String("color") { + case "always": + enableColor(true) + case "auto": + // this requests the default behaviour in the jsoncolor library + // color output is enabled or disabled dynamically based on the + // stdout's file descriptor referring to a terminal or not. + // Additionally, if the NO_COLOR environment variable is set + // (regardless of its value) color output will be disabled. + case "never": + enableColor(false) + default: + return errMsg("Invalid color specification. Must use always/auto/never") + } if c.Bool("ast") { parser := jmespath.NewParser() parsed, err := parser.Parse(expression) @@ -117,9 +137,9 @@ func runMain(c *cli.Context) int { } else { var toJSON []byte if c.Bool("compact") { - toJSON, err = json.Marshal(result) + toJSON, err = jsoncolor.Marshal(result) } else { - toJSON, err = json.MarshalIndent(result, "", " ") + toJSON, err = jsoncolor.MarshalIndent(result, "", " ") } if err != nil { errMsg("Error marshalling result to JSON: %s\n", err) @@ -130,3 +150,37 @@ func runMain(c *cli.Context) int { os.Stdout.WriteString("\n") return 0 } + +func enableColor(enabled bool) { + + if enabled { + jsoncolor.DefaultArrayColor.EnableColor() + jsoncolor.DefaultColonColor.EnableColor() + jsoncolor.DefaultCommaColor.EnableColor() + jsoncolor.DefaultFalseColor.EnableColor() + jsoncolor.DefaultFieldColor.EnableColor() + jsoncolor.DefaultFieldQuoteColor.EnableColor() + jsoncolor.DefaultNullColor.EnableColor() + jsoncolor.DefaultNumberColor.EnableColor() + jsoncolor.DefaultObjectColor.EnableColor() + jsoncolor.DefaultSpaceColor.EnableColor() + jsoncolor.DefaultStringColor.EnableColor() + jsoncolor.DefaultStringQuoteColor.EnableColor() + jsoncolor.DefaultTrueColor.EnableColor() + + } else { + jsoncolor.DefaultArrayColor.DisableColor() + jsoncolor.DefaultColonColor.DisableColor() + jsoncolor.DefaultCommaColor.DisableColor() + jsoncolor.DefaultFalseColor.DisableColor() + jsoncolor.DefaultFieldColor.DisableColor() + jsoncolor.DefaultFieldQuoteColor.DisableColor() + jsoncolor.DefaultNullColor.DisableColor() + jsoncolor.DefaultNumberColor.DisableColor() + jsoncolor.DefaultObjectColor.DisableColor() + jsoncolor.DefaultSpaceColor.DisableColor() + jsoncolor.DefaultStringColor.DisableColor() + jsoncolor.DefaultStringQuoteColor.DisableColor() + jsoncolor.DefaultTrueColor.DisableColor() + } +}