From 694157e024e52e6fcb01cfd2e9c6d50b85f9621f Mon Sep 17 00:00:00 2001 From: wm4 Date: Mon, 30 Oct 2017 14:56:50 +0100 Subject: 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. --- misc/json.c | 35 +++++++++++++++++++++++++++++------ misc/json.h | 1 + options/m_option.c | 11 +++++++++++ 3 files changed, 41 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 diff --git a/options/m_option.c b/options/m_option.c index 00ea5d6d93..9472f3c870 100644 --- a/options/m_option.c +++ b/options/m_option.c @@ -3249,6 +3249,16 @@ static char *print_node(const m_option_t *opt, const void *val) return t; } +static char *pretty_print_node(const m_option_t *opt, const void *val) +{ + char *t = talloc_strdup(NULL, ""); + if (json_write_pretty(&t, &VAL(val)) < 0) { + talloc_free(t); + t = NULL; + } + return t; +} + static void dup_node(void *ta_parent, struct mpv_node *node) { switch (node->format) { @@ -3342,6 +3352,7 @@ const m_option_type_t m_option_type_node = { .size = sizeof(struct mpv_node), .parse = parse_node, .print = print_node, + .pretty_print = pretty_print_node, .copy = copy_node, .free = free_node, .set = node_set, -- cgit v1.2.3