Skip to content

Commit

Permalink
Added colorized output. (#1)
Browse files Browse the repository at this point in the history
* Added colorized output.

Signed-off-by: Springcomp <[email protected]>

* Colorized compact output.

Signed-off-by: Springcomp <[email protected]>

* Using simpler code.

Signed-off-by: Springcomp <[email protected]>

* Made function private (really this time)

Signed-off-by: Springcomp <[email protected]>

* Documented NO_COLOR environment variable.

Signed-off-by: Springcomp <[email protected]>

* Using updated upstream jsoncolor.

Signed-off-by: Springcomp <[email protected]>

---------

Signed-off-by: Springcomp <[email protected]>
  • Loading branch information
springcomp authored Mar 25, 2023
1 parent 675ba38 commit 725b6ea
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
58 changes: 56 additions & 2 deletions jp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"

"github.com/jmespath-community/go-jmespath"
"github.com/nwidger/jsoncolor"
"github.com/urfave/cli"
)

Expand All @@ -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.",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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()
}
}

0 comments on commit 725b6ea

Please sign in to comment.