diff options
Diffstat (limited to 'vendor/github.com/TylerBrock')
| -rw-r--r-- | vendor/github.com/TylerBrock/colorjson/LICENSE | 21 | ||||
| -rw-r--r-- | vendor/github.com/TylerBrock/colorjson/README.md | 61 | ||||
| -rw-r--r-- | vendor/github.com/TylerBrock/colorjson/colorjson.go | 171 |
3 files changed, 253 insertions, 0 deletions
diff --git a/vendor/github.com/TylerBrock/colorjson/LICENSE b/vendor/github.com/TylerBrock/colorjson/LICENSE new file mode 100644 index 0000000..0d7089d --- /dev/null +++ b/vendor/github.com/TylerBrock/colorjson/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Tyler Brock + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/TylerBrock/colorjson/README.md b/vendor/github.com/TylerBrock/colorjson/README.md new file mode 100644 index 0000000..f5b1d26 --- /dev/null +++ b/vendor/github.com/TylerBrock/colorjson/README.md @@ -0,0 +1,61 @@ +ColorJSON: The Fast Color JSON Marshaller for Go +================================================ + +What is this? +------------- + +This package is based heavily on hokaccha/go-prettyjson but has some noticible differences: + - Over twice as fast (recursive descent serialization uses buffer instead of string concatenation) + ``` + BenchmarkColorJSONMarshall-4 500000 2498 ns/op + BenchmarkPrettyJSON-4 200000 6145 ns/op + ``` + - more customizable (ability to have zero indent, print raw json strings, etc...) + - better defaults (less bold colors) + +ColorJSON was built in order to produce fast beautiful colorized JSON output for [Saw](http://github.com/TylerBrock/saw). + +Installation +------------ + +```sh +go get -u github.com/TylerBrock/colorjson +``` + +Usage +----- + +Setup + +```go +import "github.com/TylerBrock/colorjson" + +str := `{ + "str": "foo", + "num": 100, + "bool": false, + "null": null, + "array": ["foo", "bar", "baz"], + "obj": { "a": 1, "b": 2 } +}` + +// Create an intersting JSON object to marshal in a pretty format +var obj map[string]interface{} +json.Unmarshal([]byte(str), &obj) +``` + +Vanilla Usage + +```go +s, _ := colorjson.Marshal(obj) +fmt.Println(string(s)) +``` + +Customization (Custom Indent) +```go +f := colorjson.NewFormatter() +f.Indent = 2 + +s, _ := f.Marshal(v) +fmt.Println(string(s)) +``` diff --git a/vendor/github.com/TylerBrock/colorjson/colorjson.go b/vendor/github.com/TylerBrock/colorjson/colorjson.go new file mode 100644 index 0000000..948247c --- /dev/null +++ b/vendor/github.com/TylerBrock/colorjson/colorjson.go @@ -0,0 +1,171 @@ +package colorjson + +import ( + "bytes" + "encoding/json" + "fmt" + "sort" + "strconv" + "strings" + + "github.com/fatih/color" +) + +const initialDepth = 0 +const valueSep = "," +const null = "null" +const startMap = "{" +const endMap = "}" +const startArray = "[" +const endArray = "]" + +const emptyMap = startMap + endMap +const emptyArray = startArray + endArray + +type Formatter struct { + KeyColor *color.Color + StringColor *color.Color + BoolColor *color.Color + NumberColor *color.Color + NullColor *color.Color + StringMaxLength int + Indent int + DisabledColor bool + RawStrings bool +} + +func NewFormatter() *Formatter { + return &Formatter{ + KeyColor: color.New(color.FgWhite), + StringColor: color.New(color.FgGreen), + BoolColor: color.New(color.FgYellow), + NumberColor: color.New(color.FgCyan), + NullColor: color.New(color.FgMagenta), + StringMaxLength: 0, + DisabledColor: false, + Indent: 0, + RawStrings: false, + } +} + +func (f *Formatter) sprintfColor(c *color.Color, format string, args ...interface{}) string { + if f.DisabledColor || c == nil { + return fmt.Sprintf(format, args...) + } + return c.SprintfFunc()(format, args...) +} + +func (f *Formatter) sprintColor(c *color.Color, s string) string { + if f.DisabledColor || c == nil { + return fmt.Sprint(s) + } + return c.SprintFunc()(s) +} + +func (f *Formatter) writeIndent(buf *bytes.Buffer, depth int) { + buf.WriteString(strings.Repeat(" ", f.Indent*depth)) +} + +func (f *Formatter) writeObjSep(buf *bytes.Buffer) { + if f.Indent != 0 { + buf.WriteByte('\n') + } else { + buf.WriteByte(' ') + } +} + +func (f *Formatter) Marshal(jsonObj interface{}) ([]byte, error) { + buffer := bytes.Buffer{} + f.marshalValue(jsonObj, &buffer, initialDepth) + return buffer.Bytes(), nil +} + +func (f *Formatter) marshalMap(m map[string]interface{}, buf *bytes.Buffer, depth int) { + remaining := len(m) + + if remaining == 0 { + buf.WriteString(emptyMap) + return + } + + keys := make([]string, 0) + for key := range m { + keys = append(keys, key) + } + + sort.Strings(keys) + + buf.WriteString(startMap) + f.writeObjSep(buf) + + for _, key := range keys { + f.writeIndent(buf, depth+1) + buf.WriteString(f.KeyColor.Sprintf("\"%s\": ", key)) + f.marshalValue(m[key], buf, depth+1) + remaining-- + if remaining != 0 { + buf.WriteString(valueSep) + } + f.writeObjSep(buf) + } + f.writeIndent(buf, depth) + buf.WriteString(endMap) +} + +func (f *Formatter) marshalArray(a []interface{}, buf *bytes.Buffer, depth int) { + if len(a) == 0 { + buf.WriteString(emptyArray) + return + } + + buf.WriteString(startArray) + f.writeObjSep(buf) + + for i, v := range a { + f.writeIndent(buf, depth+1) + f.marshalValue(v, buf, depth+1) + if i < len(a)-1 { + buf.WriteString(valueSep) + } + f.writeObjSep(buf) + } + f.writeIndent(buf, depth) + buf.WriteString(endArray) +} + +func (f *Formatter) marshalValue(val interface{}, buf *bytes.Buffer, depth int) { + switch v := val.(type) { + case map[string]interface{}: + f.marshalMap(v, buf, depth) + case []interface{}: + f.marshalArray(v, buf, depth) + case string: + f.marshalString(v, buf) + case float64: + buf.WriteString(f.sprintColor(f.NumberColor, strconv.FormatFloat(v, 'f', -1, 64))) + case bool: + buf.WriteString(f.sprintColor(f.BoolColor, (strconv.FormatBool(v)))) + case nil: + buf.WriteString(f.sprintColor(f.NullColor, null)) + case json.Number: + buf.WriteString(f.sprintColor(f.NumberColor, v.String())) + } +} + +func (f *Formatter) marshalString(str string, buf *bytes.Buffer) { + if !f.RawStrings { + strBytes, _ := json.Marshal(str) + str = string(strBytes) + } + + if f.StringMaxLength != 0 && len(str) >= f.StringMaxLength { + str = fmt.Sprintf("%s...", str[0:f.StringMaxLength]) + } + + buf.WriteString(f.sprintColor(f.StringColor, str)) +} + +// Marshal JSON data with default options +func Marshal(jsonObj interface{}) ([]byte, error) { + return NewFormatter().Marshal(jsonObj) +} |
