Skip to content

Commit

Permalink
Using simpler code.
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 c1b329a commit ad91049
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions jp.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,11 @@ func runMain(c *cli.Context) int {
os.Stdout.WriteString(converted)
} else {
var toJSON []byte
var jsonWriter = getJSONWriter(c.Bool("compact"))
toJSON, err = jsonWriter(result)
if c.Bool("compact") {
toJSON, err = jsoncolor.Marshal(result)
} else {
toJSON, err = jsoncolor.MarshalIndent(result, "", " ")
}
if err != nil {
errMsg("Error marshalling result to JSON: %s\n", err)
return 3
Expand Down Expand Up @@ -182,19 +185,3 @@ func EnableColor(enabled bool) {
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 ad91049

Please sign in to comment.