Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
8620b17cdc | |||
4b60d8b7e0 |
2
LICENSE
2
LICENSE
@ -1,4 +1,4 @@
|
||||
Copyright 2020 Nils Jakobi
|
||||
Copyright Nils Stinnesbeck
|
||||
|
||||
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:
|
||||
|
||||
|
14
go.mod
14
go.mod
@ -1,9 +1,13 @@
|
||||
module git.nils.zone/nils/prettify
|
||||
module git.stinnesbeck.com/nils/prettify
|
||||
|
||||
go 1.14
|
||||
go 1.20
|
||||
|
||||
require github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2
|
||||
|
||||
require (
|
||||
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2
|
||||
github.com/fatih/color v1.9.0 // indirect
|
||||
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e // indirect
|
||||
github.com/fatih/color v1.15.0 // indirect
|
||||
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.18 // indirect
|
||||
golang.org/x/sys v0.6.0 // indirect
|
||||
)
|
||||
|
24
go.sum
24
go.sum
@ -1,14 +1,14 @@
|
||||
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4=
|
||||
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w=
|
||||
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
|
||||
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
|
||||
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e h1:0aewS5NTyxftZHSnFaJmWE5oCCrj4DyEXkAiMa1iZJM=
|
||||
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
|
||||
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
|
||||
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
|
||||
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
|
||||
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
|
||||
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f h1:7LYC+Yfkj3CTRcShK0KOL/w6iTiKyqqBA9a41Wnggw8=
|
||||
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
|
||||
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
|
56
main.go
56
main.go
@ -1,56 +0,0 @@
|
||||
package prettify
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/TylerBrock/colorjson"
|
||||
)
|
||||
|
||||
// Print will beautify the output of body
|
||||
func Print(body interface{}) {
|
||||
b, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var obj map[string]interface{}
|
||||
json.Unmarshal(b, &obj)
|
||||
|
||||
// Make a custom formatter with indent set
|
||||
f := colorjson.NewFormatter()
|
||||
f.Indent = 2
|
||||
|
||||
// Marshal the Colorized JSON
|
||||
s, err := f.Marshal(obj)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Print out string to stdout
|
||||
fmt.Println(string(s))
|
||||
}
|
||||
|
||||
// PrintArray will beautify the output of an interface array (bodyArray)
|
||||
func PrintArray(bodyArray interface{}) {
|
||||
b, err := json.Marshal(bodyArray)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var obj []map[string]interface{}
|
||||
json.Unmarshal(b, &obj)
|
||||
|
||||
// Make a custom formatter with indent set
|
||||
f := colorjson.NewFormatter()
|
||||
f.Indent = 2
|
||||
|
||||
for i := range obj {
|
||||
// Marshal the Colorized JSON
|
||||
s, err := f.Marshal(obj[i])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// Print out string to stdout
|
||||
fmt.Println(string(s))
|
||||
}
|
||||
|
||||
}
|
77
prettify.go
Normal file
77
prettify.go
Normal file
@ -0,0 +1,77 @@
|
||||
package prettify
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/TylerBrock/colorjson"
|
||||
)
|
||||
|
||||
// Print will beautify the output of v
|
||||
func Print(v any) {
|
||||
typeOfVar := reflect.TypeOf(v)
|
||||
switch typeOfVar.Kind() {
|
||||
case reflect.Slice:
|
||||
pprintArray(v)
|
||||
case reflect.Struct:
|
||||
pprint(v)
|
||||
}
|
||||
}
|
||||
|
||||
// pprint will beautify the output of body
|
||||
func pprint(body any) error {
|
||||
b, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var obj map[string]any
|
||||
json.Unmarshal(b, &obj)
|
||||
|
||||
// Make a custom formatter with indent set
|
||||
f := colorjson.NewFormatter()
|
||||
f.Indent = 2
|
||||
|
||||
// Marshal the Colorized JSON
|
||||
s, err := f.Marshal(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Print out string to stdout
|
||||
fmt.Printf("%s\n", s)
|
||||
return nil
|
||||
}
|
||||
|
||||
// pprintArray will beautify the output of bodyArray
|
||||
func pprintArray(bodyArray any) error {
|
||||
b, err := json.Marshal(bodyArray)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var obj []map[string]any
|
||||
json.Unmarshal(b, &obj)
|
||||
|
||||
// Make a custom formatter with indent set
|
||||
f := colorjson.NewFormatter()
|
||||
f.Indent = 2
|
||||
|
||||
for i := range obj {
|
||||
// Marshal the Colorized JSON
|
||||
s, err := f.Marshal(obj[i])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Print out string to stdout
|
||||
fmt.Printf("%s", s)
|
||||
|
||||
// include a comma between elements
|
||||
if i < len(obj)-1 {
|
||||
fmt.Printf(",")
|
||||
}
|
||||
// include newline at the end of all elements
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user