Format and indent JSON values

This commit is contained in:
Seth Etter 2019-10-07 07:43:10 -05:00
parent 8c302bd236
commit 7a8972fb5c
No known key found for this signature in database
GPG Key ID: 17D56DA5082FA084
4 changed files with 34 additions and 12 deletions

View File

@ -617,10 +617,8 @@ func exportValue(path []string, fName string) error {
bk := []byte(path[len(path)-1])
v := b.Get(bk)
return writeToFile(fName, string(v)+"\n", os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
} else {
return errors.New("exportValue: Invalid Bucket")
}
return nil
return errors.New("exportValue: Invalid Bucket")
})
}
@ -646,10 +644,8 @@ func exportJSON(path []string, fName string) error {
return writeToFile(fName, genJSONString(b.Bucket(bk)), os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
}
return writeToFile(fName, genJSONString(b), os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
} else {
return errors.New("exportValue: Invalid Bucket")
}
return nil
return errors.New("exportValue: Invalid Bucket")
})
}

3
go.mod
View File

@ -3,6 +3,7 @@ module github.com/br0xen/boltbrowser
require (
github.com/boltdb/bolt v1.3.1
github.com/br0xen/termbox-util v0.0.0-20170904143325-de1d4c83380e
github.com/mattn/go-runewidth v0.0.3 // indirect
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/nsf/termbox-go v0.0.0-20180819125858-b66b20ab708e
golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed // indirect
)

6
go.sum
View File

@ -2,7 +2,9 @@ github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/br0xen/termbox-util v0.0.0-20170904143325-de1d4c83380e h1:PF4gYXcZfTbAoAk5DPZcvjmq8gyg4gpcmWdT8W+0X1c=
github.com/br0xen/termbox-util v0.0.0-20170904143325-de1d4c83380e/go.mod h1:x9wJlgOj74OFTOBwXOuO8pBguW37EgYNx51Dbjkfzo4=
github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/nsf/termbox-go v0.0.0-20180819125858-b66b20ab708e h1:fvw0uluMptljaRKSU8459cJ4bmi3qUYyMs5kzpic2fY=
github.com/nsf/termbox-go v0.0.0-20180819125858-b66b20ab708e/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed h1:5TJcLJn2a55mJjzYk0yOoqN8X1OdvBDUnaZaKKyQtkY=
golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View File

@ -1,6 +1,7 @@
package main
import (
"encoding/json"
"fmt"
"strings"
"time"
@ -597,7 +598,7 @@ func (screen *BrowserScreen) drawRightPane(style Style) {
startY += screen.drawMultilineText(pathString, 6, startX, startY, (w/2)-1, style.defaultFg, style.defaultBg)
keyString := fmt.Sprintf("Key: %s", stringify([]byte(p.key)))
startY += screen.drawMultilineText(keyString, 5, startX, startY, (w/2)-1, style.defaultFg, style.defaultBg)
valString := fmt.Sprintf("Value: %s", stringify([]byte(p.val)))
valString := fmt.Sprintf("Value: %s", formatValue([]byte(p.val)))
startY += screen.drawMultilineText(valString, 7, startX, startY, (w/2)-1, style.defaultFg, style.defaultBg)
}
} else {
@ -608,6 +609,28 @@ func (screen *BrowserScreen) drawRightPane(style Style) {
}
}
func formatValue(val []byte) []byte {
// Attempt JSON parsing and formatting
out, err := formatValueJSON(val)
if err == nil {
return out
}
return []byte(val)
}
func formatValueJSON(val []byte) ([]byte, error) {
var jsonOut interface{}
err := json.Unmarshal(val, &jsonOut)
if err != nil {
return val, err
}
out, err := json.MarshalIndent(jsonOut, "", " ")
if err != nil {
return val, err
}
return out, nil
}
/* drawBucket
* @bkt *BoltBucket - The bucket to draw
* @style Style - The style to use
@ -919,9 +942,9 @@ func (screen *BrowserScreen) drawMultilineText(msg string, indentPadding, startX
msg = spacePadding + msg[maxWidth-1:]
numLines++
}
termboxUtil.DrawStringAtPoint(msg, startX, (startY + numLines), fg, bg)
numLines++
}
termboxUtil.DrawStringAtPoint(msg, startX, (startY + numLines), fg, bg)
numLines++
return numLines
}