diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-12-03 20:33:02 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2018-12-03 22:42:38 +0100 |
commit | ab91733c7e463c657358536540ca91d54521f3e6 (patch) | |
tree | 8fc8d95d8814dd7b7cd2f27012343cb17ee6355c /src | |
parent | 897f099bb0adf69d2b9332fe84d115f12a34da46 (diff) | |
download | systemd-ab91733c7e463c657358536540ca91d54521f3e6.tar.gz systemd-ab91733c7e463c657358536540ca91d54521f3e6.tar.bz2 systemd-ab91733c7e463c657358536540ca91d54521f3e6.zip |
json: add new display flag JSON_FORMAT_COLOR_AUTO
Typically we want to enable color when colors_enabled() says so, hence
let's automatize this.
Diffstat (limited to 'src')
-rw-r--r-- | src/busctl/busctl.c | 2 | ||||
-rw-r--r-- | src/shared/json.c | 3 | ||||
-rw-r--r-- | src/shared/json.h | 13 |
3 files changed, 11 insertions, 7 deletions
diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c index 6188216d28..0bcc89f6f2 100644 --- a/src/busctl/busctl.c +++ b/src/busctl/busctl.c @@ -1939,7 +1939,7 @@ static void json_dump_with_flags(JsonVariant *v, FILE *f) { json_variant_dump(v, (arg_json == JSON_PRETTY ? JSON_FORMAT_PRETTY : JSON_FORMAT_NEWLINE) | - colors_enabled() * JSON_FORMAT_COLOR, + JSON_FORMAT_COLOR_AUTO, f, NULL); } diff --git a/src/shared/json.c b/src/shared/json.c index 75795c2850..c410b62008 100644 --- a/src/shared/json.c +++ b/src/shared/json.c @@ -1585,6 +1585,9 @@ void json_variant_dump(JsonVariant *v, JsonFormatFlags flags, FILE *f, const cha print_source(f, v, flags, false); + if (((flags & (JSON_FORMAT_COLOR_AUTO|JSON_FORMAT_COLOR)) == JSON_FORMAT_COLOR_AUTO) && colors_enabled()) + flags |= JSON_FORMAT_COLOR; + if (flags & JSON_FORMAT_SSE) fputs("data: ", f); if (flags & JSON_FORMAT_SEQ) diff --git a/src/shared/json.h b/src/shared/json.h index 04687d4135..4eba91c272 100644 --- a/src/shared/json.h +++ b/src/shared/json.h @@ -151,12 +151,13 @@ struct json_variant_foreach_state { int json_variant_get_source(JsonVariant *v, const char **ret_source, unsigned *ret_line, unsigned *ret_column); typedef enum JsonFormatFlags { - JSON_FORMAT_NEWLINE = 1 << 0, /* suffix with newline */ - JSON_FORMAT_PRETTY = 1 << 1, /* add internal whitespace to appeal to human readers */ - JSON_FORMAT_COLOR = 1 << 2, /* insert ANSI color sequences */ - JSON_FORMAT_SOURCE = 1 << 3, /* prefix with source filename/line/column */ - JSON_FORMAT_SSE = 1 << 4, /* prefix/suffix with W3C server-sent events */ - JSON_FORMAT_SEQ = 1 << 5, /* prefix/suffix with RFC 7464 application/json-seq */ + JSON_FORMAT_NEWLINE = 1 << 0, /* suffix with newline */ + JSON_FORMAT_PRETTY = 1 << 1, /* add internal whitespace to appeal to human readers */ + JSON_FORMAT_COLOR = 1 << 2, /* insert ANSI color sequences */ + JSON_FORMAT_COLOR_AUTO = 1 << 3, /* insetr ANSI color sequences if colors_enabled() says so */ + JSON_FORMAT_SOURCE = 1 << 4, /* prefix with source filename/line/column */ + JSON_FORMAT_SSE = 1 << 5, /* prefix/suffix with W3C server-sent events */ + JSON_FORMAT_SEQ = 1 << 6, /* prefix/suffix with RFC 7464 application/json-seq */ } JsonFormatFlags; int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret); |