Skip to content

Commit

Permalink
Add nicer indented output of JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
birchb1024 committed May 17, 2020
1 parent b7d5e2d commit 55dd74c
Show file tree
Hide file tree
Showing 5 changed files with 2,598 additions and 2,512 deletions.
64 changes: 26 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,16 @@ The default behaviour is to fold tree branches with no sub-branches into a singl
Having restructured the data into a tree format we can output in other formats. We can ask for JSON by adding the `-format json` option. When passed through a formatter (`jq '.'`) we get this output:

```json
{
"etc": {
"bluetooth": [
"rfcomm.conf.dpkg-remove",
"serial.conf.dpkg-remove",
"input.conf",
"audio.conf.dpkg-remove",
"network.conf",
"main.conf"
],
"fish": {
"completions": "task.fish"
}
}
}
{"etc" :
{"bluetooth" :
["rfcomm.conf.dpkg-remove",
"serial.conf.dpkg-remove",
"input.conf",
"audio.conf.dpkg-remove",
"network.conf",
"main.conf"],
"fish" :
{"completions" : "task.fish"}}}
```
By default, `frangipanni` breaks lines into tokens on any non-alphanumeric character.

Expand Down Expand Up @@ -158,32 +153,25 @@ XDG_SESSION_COOKIE=fe37f2ef4-158904.727668-469753

And run with
```
$ env | egrep '^XDG' | ./frangipanni -breaks '_=' -order alpha -format json | jq '.'
$ env | egrep '^XDG' | ./frangipanni -breaks '_=' -order alpha -format json
```
we get

```json
{
"XDG": {
"CURRENT": {
"DESKTOP": "KDE"
},
"DATA": {
"DIRS": "/usr/share:/usr/share:/usr/local/share"
},
"RUNTIME": {
"DIR": "/run/user/1000"
},
"SEAT": "seat0",
"SESSION": {
"COOKIE": "fe37f2ef4-158904.727668-469753",
"DESKTOP": "plasma",
"ID": 5,
"TYPE": "x11"
},
"VTNR": 2
}
}
{"XDG" :
{"CURRENT" :
{"DESKTOP" : "KDE"},
"DATA" :
{"DIRS" : "/usr/share:/usr/share:/usr/local/share"},
"RUNTIME" :
{"DIR" : "/run/user/1000"},
"SEAT" : "seat0",
"SESSION" :
{"COOKIE" : "fe3c308cda1ed18ad0deaaba527f2ef4-1589682304.727668-469721653",
"DESKTOP" : "plasma",
"ID" : 5,
"TYPE" : "x11"},
"VTNR" : 2}}
```
## Split the PATH

Expand Down Expand Up @@ -248,7 +236,7 @@ $ cat test/fixtures/triples.csv | \
jq '."jupiter"'
```

```
```json
{
"defaultAccount": "alice1",
"hasUser": [
Expand Down
30 changes: 23 additions & 7 deletions frangipanni.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ func indent(out io.Writer, depth int) {
func fprintNodeChildrenListJSON(out io.Writer, childs []*node, depth int) {

if depth+1 > printDepth {
//fmt.Fprint(out, "null")
return
}
if len(childs) == 0 {
Expand All @@ -173,14 +172,22 @@ func fprintNodeChildrenListJSON(out io.Writer, childs []*node, depth int) {
fprintNodeJSON(out, childs[0], depth)
return
}
if depth >= 0 {
fmt.Fprint(out, "\n")
}
indent(out, depth+1)
fmt.Fprint(out, "[")
for i, c := range childs {
if i > 0 {
fmt.Fprint(out, "\n")
indent(out, depth+1)
}
fprintNodeJSON(out, c, depth)
if i < len(childs)-1 {
fmt.Fprint(out, ",\n")
fmt.Fprint(out, ",")
}
}
fmt.Fprint(out, "]\n")
fmt.Fprint(out, "]")

}

Expand All @@ -193,20 +200,28 @@ func fprintNodeChildrenMapJSON(out io.Writer, childs []*node, depth int, parent
if len(childs) == 0 {
return
}
if depth >= 0 {
fmt.Fprint(out, "\n")
}
indent(out, depth+1)
fmt.Fprint(out, "{")
for i, c := range childs {
ctext := escapeJSON(c.text)
if printSeparators {
ctext = escapeJSON(c.sep + c.text)
}

if i > 0 {
fmt.Fprint(out, "\n")
indent(out, depth+1)
}
fmt.Fprint(out, ctext+" : ")
fprintNodeChildrenJSON(out, c.children, depth+1, c)
if i < len(childs)-1 {
fmt.Fprint(out, ",\n")
fmt.Fprint(out, ",")
}
}
fmt.Fprint(out, "}\n")
fmt.Fprint(out, "}")
}

func fprintNodeChildrenJSON(out io.Writer, nodemap map[string]*node, depth int, parent *node) {
Expand Down Expand Up @@ -249,7 +264,7 @@ func fprintNodeJSON(out io.Writer, n *node, depth int) {
}
fmt.Fprint(out, "{"+ntext+" : ")
fprintNodeChildrenJSON(out, n.children, depth+1, n)
fmt.Fprint(out, "}\n")
fmt.Fprint(out, "}")
}

func fakeCounts(n *node) {
Expand Down Expand Up @@ -402,7 +417,8 @@ func main() {
if printCounts {
fakeCounts(&root)
}
fprintNodeChildrenJSON(stdoutBuffered, root.children, 0, &root)
fprintNodeChildrenJSON(stdoutBuffered, root.children, -1, &root)
fmt.Fprintln(stdoutBuffered)

default:
log.Fatalf("Error: unknown format option '%v'", format)
Expand Down
1 change: 1 addition & 0 deletions test/confidence.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ do
for sw in '' '-breaks /' -separators -counts '-level 2' '-depth 2'
do
echo "$tf--- -format json -order alpha $sw ----------------------------------------------------------------------------------------------------" >> "$tempfile"
head -50 "$tf" | ../frangipanni $sw -format json -order alpha | jq '.' > /dev/null
head -50 "$tf" | ../frangipanni $sw -format json -order alpha >> "$tempfile"
done
done
Expand Down
Loading

0 comments on commit 55dd74c

Please sign in to comment.