ctl-client: Print trailing newline for events

If someone wants to parse this instead of using jq, a trailing
newline delimits the end of the event.
wayvncctl-polishing
Andri Yngvason 2023-01-15 17:41:56 +00:00
parent 018d42db8a
commit d6ebc9ecc0
1 changed files with 7 additions and 19 deletions

View File

@ -497,45 +497,34 @@ static bool json_has_content(json_t* root)
return false; return false;
} }
static void print_for_human(json_t* data, int level, bool needs_leading_newline) static void print_for_human(json_t* data, int level)
{ {
size_t i; size_t i;
const char* key; const char* key;
json_t* value; json_t* value;
bool needs_indent = needs_leading_newline;
switch(json_typeof(data)) { switch(json_typeof(data)) {
case JSON_NULL: case JSON_NULL:
printf("<null>\n"); printf("<null>\n");
break; break;
case JSON_OBJECT: case JSON_OBJECT:
if (json_object_size(data) > 0 && needs_leading_newline)
printf("\n");
json_object_foreach(data, key, value) { json_object_foreach(data, key, value) {
if (!json_has_content(value)) if (!json_has_content(value))
continue; continue;
if (needs_indent)
print_indent(level); print_indent(level);
else
needs_indent = true;
printf("%s: ", key); printf("%s: ", key);
print_for_human(value, level + 1, true); print_for_human(value, level + 1);
} }
break; break;
case JSON_ARRAY: case JSON_ARRAY:
if (json_array_size(data) > 0 && needs_leading_newline)
printf("\n");
json_array_foreach(data, i, value) { json_array_foreach(data, i, value) {
if (!json_has_content(value)) if (!json_has_content(value))
continue; continue;
print_indent(level); print_indent(level);
printf("- "); printf("- ");
print_for_human(value, level + 1, json_is_array(value)); print_for_human(value, level + 1);
} }
break; break;
case JSON_STRING: case JSON_STRING:
@ -561,10 +550,9 @@ static void print_event(struct jsonipc_request* event, unsigned flags)
if (flags & CTL_CLIENT_PRINT_JSON) { if (flags & CTL_CLIENT_PRINT_JSON) {
print_compact_json(event->json); print_compact_json(event->json);
} else { } else {
printf("\n%s:", event->method); printf("%s:\n", event->method);
if (event->params) if (event->params)
print_for_human(event->params, 1, true); print_for_human(event->params, 1);
else
printf("\n"); printf("\n");
} }
fflush(stdout); fflush(stdout);