Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added colorized output. #1

Merged
merged 6 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ go 1.18

require (
github.com/jmespath-community/go-jmespath v1.1.0
github.com/springcomp/jsoncolor v0.3.2-rc2
github.com/urfave/cli v1.22.12
)

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/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,12 +4,25 @@ 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/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=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/springcomp/jsoncolor v0.3.2-rc2 h1:6iWfH3O8UW8NdyHg7wvH2R5Mwd7vrUXfBZdvQMyHnik=
github.com/springcomp/jsoncolor v0.3.2-rc2/go.mod h1:AsAsacerXZjz6F3kg/bhfPdIPY8Sz8Ny8+0J9niPYIM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
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
59 changes: 57 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/springcomp/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,21 @@ func runMain(c *cli.Context) int {
}
expression = c.Args()[0]
}
// NoColor defines if the output is colorized or not. It's dynamically set to
springcomp marked this conversation as resolved.
Show resolved Hide resolved
// false or true based on the stdout's file descriptor referring to a terminal
// or not. It's also set to true if the NO_COLOR environment variable is
// set (regardless of its value). This is a global option and affects all
// colors.
switch c.String("color") {
case "always":
enableColor(true)
case "auto":
// this is the default in the library
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 +138,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 +151,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()
}
}