summaryrefslogtreecommitdiffstats
path: root/misc
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2017-10-30 14:56:50 +0100
committerwm4 <wm4@nowhere>2017-10-30 15:32:24 +0100
commit694157e024e52e6fcb01cfd2e9c6d50b85f9621f (patch)
tree3b393b96fb8799381ce03b4fe63248983e903f2f /misc
parent2d958dbf2bd918391f3b3f48fe131d3eb783463a (diff)
downloadmpv-694157e024e52e6fcb01cfd2e9c6d50b85f9621f.tar.bz2
mpv-694157e024e52e6fcb01cfd2e9c6d50b85f9621f.tar.xz
m_option: pretty print mpv_node for OSD
Somewhat useful for debugging. Unfortunately libass (or something else) strips leading whitespace, making it look slightly more ugly than necessary. Still an improvement.
Diffstat (limited to 'misc')
-rw-r--r--misc/json.c35
-rw-r--r--misc/json.h1
2 files changed, 30 insertions, 6 deletions
diff --git a/misc/json.c b/misc/json.c
index 56d440f509..4797fde4d0 100644
--- a/misc/json.c
+++ b/misc/json.c
@@ -235,7 +235,16 @@ static void write_json_str(bstr *b, unsigned char *str)
APPEND(b, "\"");
}
-static int json_append(bstr *b, const struct mpv_node *src)
+static void add_indent(bstr *b, int indent)
+{
+ if (indent < 0)
+ return;
+ bstr_xappend(NULL, b, bstr0("\n"));
+ for (int n = 0; n < indent; n++)
+ bstr_xappend(NULL, b, bstr0(" "));
+}
+
+static int json_append(bstr *b, const struct mpv_node *src, int indent)
{
switch (src->format) {
case MPV_FORMAT_NONE:
@@ -258,15 +267,18 @@ static int json_append(bstr *b, const struct mpv_node *src)
struct mpv_node_list *list = src->u.list;
bool is_obj = src->format == MPV_FORMAT_NODE_MAP;
APPEND(b, is_obj ? "{" : "[");
+ int next_indent = indent >= 0 ? indent + 1 : -1;
for (int n = 0; n < list->num; n++) {
if (n)
APPEND(b, ",");
+ add_indent(b, next_indent);
if (is_obj) {
write_json_str(b, list->keys[n]);
APPEND(b, ":");
}
- json_append(b, &list->values[n]);
+ json_append(b, &list->values[n], next_indent);
}
+ add_indent(b, indent);
APPEND(b, is_obj ? "}" : "]");
return 0;
}
@@ -274,6 +286,14 @@ static int json_append(bstr *b, const struct mpv_node *src)
return -1; // unknown format
}
+static int json_append_str(char **dst, struct mpv_node *src, int indent)
+{
+ bstr buffer = bstr0(*dst);
+ int r = json_append(&buffer, src, indent);
+ *dst = buffer.start;
+ return r;
+}
+
/* Write the contents of *src as JSON, and append the JSON string to *dst.
* This will use strlen() to determine the start offset, and ta_get_size()
* and ta_realloc() to extend the memory allocation of *dst.
@@ -281,8 +301,11 @@ static int json_append(bstr *b, const struct mpv_node *src)
*/
int json_write(char **dst, struct mpv_node *src)
{
- bstr buffer = bstr0(*dst);
- int r = json_append(&buffer, src);
- *dst = buffer.start;
- return r;
+ return json_append_str(dst, src, -1);
+}
+
+// Same as json_write(), but add whitespace to make it readable.
+int json_write_pretty(char **dst, struct mpv_node *src)
+{
+ return json_append_str(dst, src, 0);
}
diff --git a/misc/json.h b/misc/json.h
index 0a9f8d7839..43d565b275 100644
--- a/misc/json.h
+++ b/misc/json.h
@@ -24,5 +24,6 @@
int json_parse(void *ta_parent, struct mpv_node *dst, char **src, int max_depth);
void json_skip_whitespace(char **src);
int json_write(char **s, struct mpv_node *src);
+int json_write_pretty(char **s, struct mpv_node *src);
#endif