0
0
mirror of https://github.com/mpv-player/mpv.git synced 2024-09-20 03:52:22 +02:00

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.
This commit is contained in:
wm4 2017-10-30 14:56:50 +01:00
parent 2d958dbf2b
commit 694157e024
3 changed files with 41 additions and 6 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -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,