Skip to content

Commit d666a90

Browse files
author
James Haggerty
committed
Add color support
1 parent e287c64 commit d666a90

8 files changed

Lines changed: 59 additions & 9 deletions

File tree

Godeps/Godeps.json

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/_workspace/src/github.com/codegangsta/cli/app_test.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/_workspace/src/github.com/codegangsta/cli/cli_test.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/_workspace/src/github.com/codegangsta/cli/command_test.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/_workspace/src/github.com/codegangsta/cli/context_test.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/_workspace/src/github.com/codegangsta/cli/flag_test.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/_workspace/src/github.com/jmespath/go-jmespath/fuzz/jmespath.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jp.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"io/ioutil"
77
"os"
88

9-
"github.com/jmespath/jp/Godeps/_workspace/src/github.com/codegangsta/cli"
10-
"github.com/jmespath/jp/Godeps/_workspace/src/github.com/jmespath/go-jmespath"
9+
"github.com/codegangsta/cli"
10+
"github.com/fatih/color"
11+
"github.com/jmespath/go-jmespath"
12+
"github.com/nwidger/jsoncolor"
1113
)
1214

1315
const version = "0.1.2"
@@ -28,6 +30,11 @@ func main() {
2830
Name: "expr-file, e",
2931
Usage: "Read JMESPath expression from the specified file.",
3032
},
33+
cli.StringFlag{
34+
Name: "color, c",
35+
Value: "auto",
36+
Usage: "Change the color setting (none, auto, always). auto is based on whether output is a tty.",
37+
},
3138
cli.BoolFlag{
3239
Name: "unquoted, u",
3340
Usage: "If the final result is a string, it will be printed without quotes.",
@@ -67,6 +74,18 @@ func runMain(c *cli.Context) int {
6774
}
6875
expression = c.Args()[0]
6976
}
77+
// Unfortunately, there's a global setting in the underlying library
78+
// which we have to toggle here...
79+
switch c.String("color") {
80+
case "always":
81+
color.NoColor = false
82+
case "auto":
83+
// this is the default in the library
84+
case "never":
85+
color.NoColor = true
86+
default:
87+
return errMsg("Invalid color specification. Must use always/auto/never")
88+
}
7089
if c.Bool("ast") {
7190
parser := jmespath.NewParser()
7291
parsed, err := parser.Parse(expression)
@@ -111,7 +130,14 @@ func runMain(c *cli.Context) int {
111130
if c.Bool("unquoted") && isString {
112131
os.Stdout.WriteString(converted)
113132
} else {
114-
toJSON, err := json.MarshalIndent(result, "", " ")
133+
var toJSON []byte
134+
var err error
135+
if color.NoColor {
136+
// avoid doing the extra processing in jsoncolor
137+
toJSON, err = json.MarshalIndent(result, "", " ")
138+
} else {
139+
toJSON, err = jsoncolor.MarshalIndent(result, "", " ")
140+
}
115141
if err != nil {
116142
errMsg("Error marshalling result to JSON: %s\n", err)
117143
return 3

0 commit comments

Comments
 (0)