From d97dd42712dda91c90e7b791c33a028392ff2689 Mon Sep 17 00:00:00 2001 From: theMackabu Date: Tue, 2 Dec 2025 15:02:45 -0800 Subject: [PATCH] colored json printing --- meson.build | 2 +- src/modules/io.c | 108 +++++++++++++++++++++++++++++++++++++++++++++- tests/console.cjs | 3 ++ 3 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 tests/console.cjs diff --git a/meson.build b/meson.build index cc108e2..7965faf 100644 --- a/meson.build +++ b/meson.build @@ -41,7 +41,7 @@ endif build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() version_conf = configuration_data() -version_conf.set('ANT_VERSION', '0.0.5.42') +version_conf.set('ANT_VERSION', '0.0.5.43') version_conf.set('ANT_GIT_HASH', git_hash) version_conf.set('ANT_BUILD_DATE', build_date) diff --git a/src/modules/io.c b/src/modules/io.c index 352c7dc..6883baa 100644 --- a/src/modules/io.c +++ b/src/modules/io.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -11,17 +12,120 @@ #define ANSI_YELLOW "\x1b[33m" #define ANSI_RESET "\x1b[0m" +#define JSON_KEY "\x1b[36m" +#define JSON_STRING "\x1b[32m" +#define JSON_NUMBER "\x1b[33m" +#define JSON_BOOL "\x1b[35m" +#define JSON_NULL "\x1b[90m" +#define JSON_BRACE "\x1b[37m" + +static void print_json_colored(const char *json, FILE *stream) { + bool in_string = false; + bool is_key = true; + bool escape_next = false; + + for (const char *p = json; *p; p++) { + if (escape_next) { + fputc(*p, stream); + escape_next = false; + continue; + } + + if (*p == '\\' && in_string) { + fputc(*p, stream); + escape_next = true; + continue; + } + + if (*p == '"') { + if (!in_string) { + fprintf(stream, "%s\"", is_key ? JSON_KEY : JSON_STRING); + in_string = true; + } else { + fprintf(stream, "\"%s", ANSI_RESET); + in_string = false; + } + continue; + } + + if (in_string) { + fputc(*p, stream); + continue; + } + + if (*p == ':') { + fputc(*p, stream); + is_key = false; + continue; + } + + if (*p == ',' || *p == '{' || *p == '[') { + if (*p == '{' || *p == '[') { + fprintf(stream, "%s%c%s", JSON_BRACE, *p, ANSI_RESET); + } else { + fputc(*p, stream); + } + is_key = (*p == ',' || *p == '{'); + continue; + } + + if (*p == '}' || *p == ']') { + fprintf(stream, "%s%c%s", JSON_BRACE, *p, ANSI_RESET); + continue; + } + + if (strncmp(p, "true", 4) == 0) { + fprintf(stream, "%strue%s", JSON_BOOL, ANSI_RESET); + p += 3; + continue; + } + + if (strncmp(p, "false", 5) == 0) { + fprintf(stream, "%sfalse%s", JSON_BOOL, ANSI_RESET); + p += 4; + continue; + } + + if (strncmp(p, "null", 4) == 0) { + fprintf(stream, "%snull%s", JSON_NULL, ANSI_RESET); + p += 3; + continue; + } + + if ((*p >= '0' && *p <= '9') || *p == '-') { + fprintf(stream, "%s", JSON_NUMBER); + while ((*p >= '0' && *p <= '9') || *p == '.' || *p == '-' || *p == '+' || *p == 'e' || *p == 'E') { + fputc(*p, stream); + if (!(*(p + 1) >= '0' && *(p + 1) <= '9') && *(p + 1) != '.' && *(p + 1) != '-' && *(p + 1) != '+' && *(p + 1) != 'e' && *(p + 1) != 'E') break; + p++; + } + fprintf(stream, "%s", ANSI_RESET); + continue; + } + + fputc(*p, stream); + } +} + static void console_print(struct js *js, jsval_t *args, int nargs, const char *color, FILE *stream) { if (color) fprintf(stream, "%s", color); for (int i = 0; i < nargs; i++) { const char *space = i == 0 ? "" : " "; + fprintf(stream, "%s", space); if (js_type(args[i]) == JS_STR) { char *str = js_getstr(js, args[i], NULL); - fprintf(stream, "%s%s", space, str); + fprintf(stream, "%s", str); } else { - fprintf(stream, "%s%s", space, js_str(js, args[i])); + const char *str = js_str(js, args[i]); + if (str[0] == '{' || str[0] == '[') { + if (color) fprintf(stream, "%s", ANSI_RESET); + print_json_colored(str, stream); + if (color) fprintf(stream, "%s", color); + } else { + fprintf(stream, "%s", str); + } } } diff --git a/tests/console.cjs b/tests/console.cjs new file mode 100644 index 0000000..ada69d2 --- /dev/null +++ b/tests/console.cjs @@ -0,0 +1,3 @@ +console.log('log', { some: 'json', value: true }); +console.warn('warn', { some: 'json', value: true }); +console.error('error', { some: 'json', value: true }); -- 2.51.2