Skip to content

Commit cf7b132

Browse files
committed
Expose lib JQ's own pretty printing functions
Rather than writing our own for now this is good enough.
1 parent 5848b1f commit cf7b132

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

jq/jv.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,29 @@ func (jv *Jv) ToGoVal() interface{} {
198198
panic(fmt.Sprintf("Unknown JV kind %d", kind))
199199
}
200200
}
201+
202+
type JvPrintFlags int
203+
204+
const (
205+
JvPrintNone JvPrintFlags = 0 // None of the below
206+
JvPrintPretty JvPrintFlags = C.JV_PRINT_PRETTY // Print across multiple lines
207+
JvPrintAscii JvPrintFlags = C.JV_PRINT_ASCII // Escape non-ascii printable characters
208+
JvPrintColour JvPrintFlags = C.JV_PRINT_COLOUR // Include ANSI color escapes based on data types
209+
JvPrintSorted JvPrintFlags = C.JV_PRINT_SORTED // Sort output keys
210+
JvPrintInvalid JvPrintFlags = C.JV_PRINT_INVALID // Print invalid as "<invalid>"
211+
JvPrintRefCount JvPrintFlags = C.JV_PRINT_REFCOUNT // Display refcount of objects in in parenthesis
212+
JvPrintTab JvPrintFlags = C.JV_PRINT_TAB // Indent with tabs,
213+
JvPrintIsATty JvPrintFlags = C.JV_PRINT_ISATTY //
214+
JvPrintSpace0 JvPrintFlags = C.JV_PRINT_SPACE0 // Indent with zero extra chars beyond the parent bracket
215+
JvPrintSpace1 JvPrintFlags = C.JV_PRINT_SPACE1 // Indent with zero extra chars beyond the parent bracket
216+
JvPrintSpace2 JvPrintFlags = C.JV_PRINT_SPACE2 // Indent with zero extra chars beyond the parent bracket
217+
)
218+
219+
// Dump produces a human readable version of the string with the requested formatting.
220+
//
221+
// Consumes the invocant
222+
func (jv *Jv) Dump(flags JvPrintFlags) string {
223+
jv_str := Jv{C.jv_dump_string(jv.jv, C.int(flags))}
224+
defer jv_str.Free()
225+
return jv_str._string()
226+
}

jq/jv_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,17 @@ func TestJvFromJSONString(t *testing.T) {
6565
is.Err(err)
6666
is.Nil(jv)
6767
}
68+
69+
func TestJvDump(t *testing.T) {
70+
is := is.New(t)
71+
72+
jv := jq.JvFromString("test")
73+
defer jv.Free()
74+
75+
dump := jv.Copy().Dump(jq.JvPrintNone)
76+
77+
is.Equal(`"test"`, dump)
78+
dump = jv.Copy().Dump(jq.JvPrintColour)
79+
80+
is.Equal([]byte("\x1b[0;32m"+`"test"`+"\x1b[0m"), []byte(dump))
81+
}

0 commit comments

Comments
 (0)