Skip to content

Commit

Permalink
Colorized compact output.
Browse files Browse the repository at this point in the history
Signed-off-by: Springcomp <[email protected]>
  • Loading branch information
springcomp committed Mar 21, 2023
1 parent f752c98 commit c1b329a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 16 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module github.com/jmespath-community/jp
go 1.18

require (
github.com/fatih/color v1.15.0
github.com/jmespath-community/go-jmespath v1.1.0
github.com/nwidger/jsoncolor v0.3.1
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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOA
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.1 h1:5CZXA1TgqCjzKMyWP/WCuxKEzcC6f+HUlmJez9OKsHw=
github.com/nwidger/jsoncolor v0.3.1/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=
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 Down
80 changes: 68 additions & 12 deletions jp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (
"io/ioutil"
"os"

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

Expand Down Expand Up @@ -78,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
// 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 @@ -123,16 +137,8 @@ func runMain(c *cli.Context) int {
os.Stdout.WriteString(converted)
} else {
var toJSON []byte
if c.Bool("compact") {
toJSON, err = json.Marshal(result)
} else {
if color.NoColor {
// avoid doing the extra processing in jsoncolor
toJSON, err = json.MarshalIndent(result, "", " ")
} else {
toJSON, err = jsoncolor.MarshalIndent(result, "", " ")
}
}
var jsonWriter = getJSONWriter(c.Bool("compact"))
toJSON, err = jsonWriter(result)
if err != nil {
errMsg("Error marshalling result to JSON: %s\n", err)
return 3
Expand All @@ -142,3 +148,53 @@ 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()
}
}

func getJSONWriter(compact bool) func(v interface{}) ([]byte, error) {
var writers = [2]func(v interface{}) ([]byte, error){
func(v interface{}) ([]byte, error) { return jsoncolor.MarshalIndent(v, "", " ") },
jsoncolor.Marshal,
}
return writers[bool2int(compact)]
}

func bool2int(b bool) int {
if b {
return 1
} else {
return 0
}
}

0 comments on commit c1b329a

Please sign in to comment.