|
| 1 | +// Package jsonpretty implements a terminal pretty-printer for JSON. |
| 2 | +package jsonpretty |
| 3 | + |
| 4 | +import ( |
| 5 | + "bytes" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + colorDelim = "\x1b[1;38m" // bright white |
| 14 | + colorKey = "\x1b[1;34m" // bright blue |
| 15 | + colorNull = "\x1b[36m" // cyan |
| 16 | + colorString = "\x1b[32m" // green |
| 17 | + colorBool = "\x1b[33m" // yellow |
| 18 | + colorReset = "\x1b[m" |
| 19 | +) |
| 20 | + |
| 21 | +// Format reads JSON from r and writes a prettified version of it to w. |
| 22 | +func Format(w io.Writer, r io.Reader, indent string, colorize bool) error { |
| 23 | + dec := json.NewDecoder(r) |
| 24 | + dec.UseNumber() |
| 25 | + |
| 26 | + c := func(ansi string) string { |
| 27 | + if !colorize { |
| 28 | + return "" |
| 29 | + } |
| 30 | + return ansi |
| 31 | + } |
| 32 | + |
| 33 | + var idx int |
| 34 | + var stack []json.Delim |
| 35 | + |
| 36 | + for { |
| 37 | + t, err := dec.Token() |
| 38 | + if err == io.EOF { |
| 39 | + break |
| 40 | + } |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + switch tt := t.(type) { |
| 46 | + case json.Delim: |
| 47 | + switch tt { |
| 48 | + case '{', '[': |
| 49 | + stack = append(stack, tt) |
| 50 | + idx = 0 |
| 51 | + if _, err := fmt.Fprint(w, c(colorDelim), tt, c(colorReset)); err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + if dec.More() { |
| 55 | + if _, err := fmt.Fprint(w, "\n", strings.Repeat(indent, len(stack))); err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + } |
| 59 | + continue |
| 60 | + case '}', ']': |
| 61 | + stack = stack[:len(stack)-1] |
| 62 | + idx = 0 |
| 63 | + if _, err := fmt.Fprint(w, c(colorDelim), tt, c(colorReset)); err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + } |
| 67 | + default: |
| 68 | + b, err := marshalJSON(tt) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + isKey := len(stack) > 0 && stack[len(stack)-1] == '{' && idx%2 == 0 |
| 74 | + idx++ |
| 75 | + |
| 76 | + var color string |
| 77 | + if isKey { |
| 78 | + color = colorKey |
| 79 | + } else if tt == nil { |
| 80 | + color = colorNull |
| 81 | + } else { |
| 82 | + switch t.(type) { |
| 83 | + case string: |
| 84 | + color = colorString |
| 85 | + case bool: |
| 86 | + color = colorBool |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if color != "" { |
| 91 | + if _, err := fmt.Fprint(w, c(color)); err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + } |
| 95 | + if _, err := w.Write(b); err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + if color != "" { |
| 99 | + if _, err := fmt.Fprint(w, c(colorReset)); err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if isKey { |
| 105 | + if _, err := fmt.Fprint(w, c(colorDelim), ":", c(colorReset), " "); err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + continue |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if dec.More() { |
| 113 | + if _, err := fmt.Fprint(w, c(colorDelim), ",", c(colorReset), "\n", strings.Repeat(indent, len(stack))); err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + } else if len(stack) > 0 { |
| 117 | + if _, err := fmt.Fprint(w, "\n", strings.Repeat(indent, len(stack)-1)); err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + } else { |
| 121 | + if _, err := fmt.Fprint(w, "\n"); err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +// marshalJSON works like json.Marshal, but with HTML-escaping disabled. |
| 131 | +func marshalJSON(v interface{}) ([]byte, error) { |
| 132 | + buf := bytes.Buffer{} |
| 133 | + enc := json.NewEncoder(&buf) |
| 134 | + enc.SetEscapeHTML(false) |
| 135 | + if err := enc.Encode(v); err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + bb := buf.Bytes() |
| 139 | + // omit trailing newline added by json.Encoder |
| 140 | + if len(bb) > 0 && bb[len(bb)-1] == '\n' { |
| 141 | + return bb[:len(bb)-1], nil |
| 142 | + } |
| 143 | + return bb, nil |
| 144 | +} |
0 commit comments