Skip to content

Commit

Permalink
add pretty printing
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Jun 14, 2018
1 parent 579eaaf commit 49ebb43
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 18 deletions.
5 changes: 3 additions & 2 deletions formats/bencode.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ func (bencodeEncoding) UnmarshalJSONBytes(jsonBytes []byte) ([]byte, error) {
return bencode.EncodeBytes(obj)
}

func (bencodeEncoding) Raw(bencodeBytes []byte) ([]byte, error) { return bencodeBytes, nil }
func (bencodeEncoding) Color(bencodeBytes []byte) ([]byte, error) { return bencodeBytes, nil }
func (bencodeEncoding) Raw(bencodeBytes []byte) ([]byte, error) { return bencodeBytes, nil }
func (bencodeEncoding) PrettyPrint(bencodeBytes []byte) ([]byte, error) { return bencodeBytes, nil }
func (bencodeEncoding) Color(bencodeBytes []byte) ([]byte, error) { return bencodeBytes, nil }

func init() {
ByName["bencode"] = bencodeEncoding{}
Expand Down
5 changes: 3 additions & 2 deletions formats/bson.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ func (bsonEncoding) UnmarshalJSONBytes(jsonBytes []byte) ([]byte, error) {
return bson.Marshal(obj)
}

func (bsonEncoding) Raw(bsonBytes []byte) ([]byte, error) { return bsonBytes, nil }
func (bsonEncoding) Color(bsonBytes []byte) ([]byte, error) { return bsonBytes, nil }
func (bsonEncoding) Raw(bsonBytes []byte) ([]byte, error) { return bsonBytes, nil }
func (bsonEncoding) PrettyPrint(bsonBytes []byte) ([]byte, error) { return bsonBytes, nil }
func (bsonEncoding) Color(bsonBytes []byte) ([]byte, error) { return bsonBytes, nil }

func init() {
ByName["bson"] = bsonEncoding{}
Expand Down
1 change: 1 addition & 0 deletions formats/formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Encoding interface {
MarshalJSONBytes([]byte) ([]byte, error)
UnmarshalJSONBytes([]byte) ([]byte, error)
Raw([]byte) ([]byte, error)
PrettyPrint([]byte) ([]byte, error)
Color([]byte) ([]byte, error)
}

Expand Down
13 changes: 13 additions & 0 deletions formats/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package formats

import (
"bytes"
"encoding/json"

"github.com/alecthomas/chroma/quick"
)

type jsonEncoding struct{}

func (jsonEncoding) MarshalJSONBytes(jsonBytes []byte) ([]byte, error) {
// It's already JSON, silly!
return jsonBytes, nil
}

func (jsonEncoding) UnmarshalJSONBytes(jsonBytes []byte) ([]byte, error) {
// It's already JSON, silly!
return jsonBytes, nil
}

Expand All @@ -25,6 +28,16 @@ func (jsonEncoding) Raw(jsonBytes []byte) ([]byte, error) {
return jsonBytes, nil
}

func (jsonEncoding) PrettyPrint(jsonBytes []byte) ([]byte, error) {
var i interface{}
err := json.Unmarshal(jsonBytes, &i)
if err != nil {
return nil, err
}

return json.MarshalIndent(i, "", " ")
}

func (jsonEncoding) Color(jsonBytes []byte) ([]byte, error) {
var b bytes.Buffer
if err := quick.Highlight(&b, string(jsonBytes), "json", ChromaFormatter(), ChromaStyle()); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion formats/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func (tomlEncoding) UnmarshalJSONBytes(jsonBytes []byte) ([]byte, error) {
return buf.Bytes(), nil
}

func (tomlEncoding) Raw(tomlBytes []byte) ([]byte, error) { return tomlBytes, nil }
func (tomlEncoding) Raw(tomlBytes []byte) ([]byte, error) { return tomlBytes, nil }
func (tomlEncoding) PrettyPrint(tomlBytes []byte) ([]byte, error) { return tomlBytes, nil }

func (tomlEncoding) Color(tomlBytes []byte) ([]byte, error) {
var b bytes.Buffer
Expand Down
10 changes: 10 additions & 0 deletions formats/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ func (xmlEncoding) UnmarshalJSONBytes(jsonBytes []byte) ([]byte, error) {
}

func (xmlEncoding) Raw(xmlBytes []byte) ([]byte, error) { return xmlBytes, nil }

func (xmlEncoding) PrettyPrint(xmlBytes []byte) ([]byte, error) {
xmap, err := mxj.NewMapXml(xmlBytes, true)
if err != nil {
return nil, err
}

return xmap.XmlIndent("", " ")
}

func (xmlEncoding) Color(xmlBytes []byte) ([]byte, error) {
var b bytes.Buffer
if err := quick.Highlight(&b, string(xmlBytes), "xml", ChromaFormatter(), ChromaStyle()); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion formats/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func (yamlEncoding) UnmarshalJSONBytes(jsonBytes []byte) ([]byte, error) {
return yaml.JSONToYAML(jsonBytes)
}

func (yamlEncoding) Raw(yamlBytes []byte) ([]byte, error) { return yamlBytes, nil }
func (yamlEncoding) Raw(yamlBytes []byte) ([]byte, error) { return yamlBytes, nil }
func (yamlEncoding) PrettyPrint(yamlBytes []byte) ([]byte, error) { return yamlBytes, nil }

func (yamlEncoding) Color(yamlBytes []byte) ([]byte, error) {
var b bytes.Buffer
Expand Down
30 changes: 18 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,24 @@ Supported formats:
}

rootCmd.Flags().Bool("debug", false, "enable debug logging")
rootCmd.Flags().StringP("format", "f", "auto", "input format")
rootCmd.Flags().StringP("input-format", "f", "auto", "input format")
rootCmd.Flags().StringP("output-format", "o", "auto", "output format")
rootCmd.Flags().BoolP("raw", "r", false, "output raw strings, not JSON texts")
rootCmd.Flags().BoolP("color-output", "C", true, "colorize the output")
rootCmd.Flags().BoolP("monochrome-output", "M", false, "monochrome (don't colorize the output)")
//rootCmd.Flags().BoolP("compact", "c", false, "compact instead of pretty-printed output")
rootCmd.Flags().BoolP("tab", "t", false, "use tabs for indentation")
rootCmd.Flags().BoolP("raw-output", "r", false, "output raw strings, not JSON texts")
rootCmd.Flags().BoolP("color-output", "c", true, "colorize the output")
rootCmd.Flags().BoolP("monochrome-output", "m", false, "monochrome (don't colorize the output)")
rootCmd.Flags().BoolP("pretty-output", "p", true, "pretty-printed output")

rootCmd.Flags().MarkHidden("debug")

rootCmd.Execute()
}

func runCmdFunc(cmd *cobra.Command, args []string) error {
inputFormat, _ := cmd.Flags().GetString("format")
inputFormat, _ := cmd.Flags().GetString("input-format")
outputFormat, _ := cmd.Flags().GetString("output-format")
raw, _ := cmd.Flags().GetBool("raw")
raw, _ := cmd.Flags().GetBool("raw-output")
color, _ := cmd.Flags().GetBool("color-output")
//compact, _ := cmd.Flags().GetBool("compact")
prettyPrint, _ := cmd.Flags().GetBool("pretty-output")
monochrome, _ := cmd.Flags().GetBool("monochrome-output")
if runtime.GOOS == "windows" {
monochrome = true
Expand Down Expand Up @@ -166,18 +165,25 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
resultBytes := []byte(resultJv.Dump(jq.JvPrintNone))
output, err := encoder.UnmarshalJSONBytes(resultBytes)
if err != nil {
return fmt.Errorf("failed to encode jq program output as %s: %s", inputFormat, err)
return fmt.Errorf("failed to encode jq program output as %s: %s", outputFormat, err)
}

if prettyPrint {
output, err = encoder.PrettyPrint(output)
if err != nil {
return fmt.Errorf("failed to encode jq program output as pretty %s: %s", outputFormat, err)
}
}

if raw {
output, err = encoder.Raw(output)
if err != nil {
return fmt.Errorf("failed to encode jq program output as raw %s: %s", inputFormat, err)
return fmt.Errorf("failed to encode jq program output as raw %s: %s", outputFormat, err)
}
} else if color && !monochrome && stdoutIsTTY {
output, err = encoder.Color(output)
if err != nil {
return fmt.Errorf("failed to encode jq program output as color %s: %s", inputFormat, err)
return fmt.Errorf("failed to encode jq program output as color %s: %s", outputFormat, err)
}
}

Expand Down

0 comments on commit 49ebb43

Please sign in to comment.