jsre: fix annoying indentation when printing arrays of objects
The pretty printer, dumb as it is, printed arrays of objects as [{ ... }] With this change, they now print as: [{ ... }]
This commit is contained in:
parent
49703bea0a
commit
1086e2f298
@ -51,7 +51,7 @@ var boringKeys = map[string]bool{
|
|||||||
|
|
||||||
// prettyPrint writes value to standard output.
|
// prettyPrint writes value to standard output.
|
||||||
func prettyPrint(vm *otto.Otto, value otto.Value) {
|
func prettyPrint(vm *otto.Otto, value otto.Value) {
|
||||||
ppctx{vm}.printValue(value, 0)
|
ppctx{vm}.printValue(value, 0, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func prettyPrintJS(call otto.FunctionCall) otto.Value {
|
func prettyPrintJS(call otto.FunctionCall) otto.Value {
|
||||||
@ -68,10 +68,10 @@ func (ctx ppctx) indent(level int) string {
|
|||||||
return strings.Repeat(indentString, level)
|
return strings.Repeat(indentString, level)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx ppctx) printValue(v otto.Value, level int) {
|
func (ctx ppctx) printValue(v otto.Value, level int, inArray bool) {
|
||||||
switch {
|
switch {
|
||||||
case v.IsObject():
|
case v.IsObject():
|
||||||
ctx.printObject(v.Object(), level)
|
ctx.printObject(v.Object(), level, inArray)
|
||||||
case v.IsNull():
|
case v.IsNull():
|
||||||
specialColor.Print("null")
|
specialColor.Print("null")
|
||||||
case v.IsUndefined():
|
case v.IsUndefined():
|
||||||
@ -92,7 +92,7 @@ func (ctx ppctx) printValue(v otto.Value, level int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx ppctx) printObject(obj *otto.Object, level int) {
|
func (ctx ppctx) printObject(obj *otto.Object, level int, inArray bool) {
|
||||||
switch obj.Class() {
|
switch obj.Class() {
|
||||||
case "Array":
|
case "Array":
|
||||||
lv, _ := obj.Get("length")
|
lv, _ := obj.Get("length")
|
||||||
@ -109,7 +109,7 @@ func (ctx ppctx) printObject(obj *otto.Object, level int) {
|
|||||||
for i := int64(0); i < len; i++ {
|
for i := int64(0); i < len; i++ {
|
||||||
el, err := obj.Get(strconv.FormatInt(i, 10))
|
el, err := obj.Get(strconv.FormatInt(i, 10))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
ctx.printValue(el, level+1)
|
ctx.printValue(el, level+1, true)
|
||||||
}
|
}
|
||||||
if i < len-1 {
|
if i < len-1 {
|
||||||
fmt.Printf(", ")
|
fmt.Printf(", ")
|
||||||
@ -137,12 +137,15 @@ func (ctx ppctx) printObject(obj *otto.Object, level int) {
|
|||||||
for i, k := range keys {
|
for i, k := range keys {
|
||||||
v, _ := obj.Get(k)
|
v, _ := obj.Get(k)
|
||||||
fmt.Printf("%s%s: ", ctx.indent(level+1), k)
|
fmt.Printf("%s%s: ", ctx.indent(level+1), k)
|
||||||
ctx.printValue(v, level+1)
|
ctx.printValue(v, level+1, false)
|
||||||
if i < len(keys)-1 {
|
if i < len(keys)-1 {
|
||||||
fmt.Printf(",")
|
fmt.Printf(",")
|
||||||
}
|
}
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
if inArray {
|
||||||
|
level--
|
||||||
|
}
|
||||||
fmt.Printf("%s}", ctx.indent(level))
|
fmt.Printf("%s}", ctx.indent(level))
|
||||||
|
|
||||||
case "Function":
|
case "Function":
|
||||||
|
Loading…
Reference in New Issue
Block a user