mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:57:35 +00:00
jp: Only output colors when stdout is a TTY
This commit is contained in:
parent
e68f4111ea
commit
c07176bd53
1 changed files with 21 additions and 15 deletions
|
@ -34,7 +34,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static void print(const JsonValue& value, int indent = 0);
|
static void print(const JsonValue& value, int indent = 0, bool use_color = true);
|
||||||
static void print_indent(int indent)
|
static void print_indent(int indent)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < indent; ++i)
|
for (int i = 0; i < indent; ++i)
|
||||||
|
@ -73,20 +73,23 @@ int main(int argc, char** argv)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
print(json.value());
|
print(json.value(), 0, isatty(STDOUT_FILENO));
|
||||||
outln();
|
outln();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print(const JsonValue& value, int indent)
|
void print(const JsonValue& value, int indent, bool use_color)
|
||||||
{
|
{
|
||||||
if (value.is_object()) {
|
if (value.is_object()) {
|
||||||
outln("{{");
|
outln("{{");
|
||||||
value.as_object().for_each_member([&](auto& member_name, auto& member_value) {
|
value.as_object().for_each_member([&](auto& member_name, auto& member_value) {
|
||||||
print_indent(indent + 1);
|
print_indent(indent + 1);
|
||||||
out("\"\033[33;1m{}\033[0m\": ", member_name);
|
if (use_color)
|
||||||
print(member_value, indent + 1);
|
out("\"\033[33;1m{}\033[0m\": ", member_name);
|
||||||
|
else
|
||||||
|
out("\"{}\": ", member_name);
|
||||||
|
print(member_value, indent + 1, use_color);
|
||||||
outln(",");
|
outln(",");
|
||||||
});
|
});
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
|
@ -97,25 +100,28 @@ void print(const JsonValue& value, int indent)
|
||||||
outln("[");
|
outln("[");
|
||||||
value.as_array().for_each([&](auto& entry_value) {
|
value.as_array().for_each([&](auto& entry_value) {
|
||||||
print_indent(indent + 1);
|
print_indent(indent + 1);
|
||||||
print(entry_value, indent + 1);
|
print(entry_value, indent + 1, use_color);
|
||||||
outln(",");
|
outln(",");
|
||||||
});
|
});
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
out("]");
|
out("]");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (value.is_string())
|
if (use_color) {
|
||||||
out("\033[31;1m");
|
if (value.is_string())
|
||||||
else if (value.is_number())
|
out("\033[31;1m");
|
||||||
out("\033[35;1m");
|
else if (value.is_number())
|
||||||
else if (value.is_bool())
|
out("\033[35;1m");
|
||||||
out("\033[32;1m");
|
else if (value.is_bool())
|
||||||
else if (value.is_null())
|
out("\033[32;1m");
|
||||||
out("\033[34;1m");
|
else if (value.is_null())
|
||||||
|
out("\033[34;1m");
|
||||||
|
}
|
||||||
if (value.is_string())
|
if (value.is_string())
|
||||||
out("\"");
|
out("\"");
|
||||||
out("{}", value.to_string());
|
out("{}", value.to_string());
|
||||||
if (value.is_string())
|
if (value.is_string())
|
||||||
out("\"");
|
out("\"");
|
||||||
out("\033[0m");
|
if (use_color)
|
||||||
|
out("\033[0m");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue