MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

colored json printing

+110 -3
+1 -1
meson.build
··· 41 41 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 42 42 43 43 version_conf = configuration_data() 44 - version_conf.set('ANT_VERSION', '0.0.5.42') 44 + version_conf.set('ANT_VERSION', '0.0.5.43') 45 45 version_conf.set('ANT_GIT_HASH', git_hash) 46 46 version_conf.set('ANT_BUILD_DATE', build_date) 47 47
+106 -2
src/modules/io.c
··· 1 1 #include <stdio.h> 2 2 #include <stdlib.h> 3 3 #include <string.h> 4 + #include <stdbool.h> 4 5 #include <libgen.h> 5 6 #include <limits.h> 6 7 ··· 11 12 #define ANSI_YELLOW "\x1b[33m" 12 13 #define ANSI_RESET "\x1b[0m" 13 14 15 + #define JSON_KEY "\x1b[36m" 16 + #define JSON_STRING "\x1b[32m" 17 + #define JSON_NUMBER "\x1b[33m" 18 + #define JSON_BOOL "\x1b[35m" 19 + #define JSON_NULL "\x1b[90m" 20 + #define JSON_BRACE "\x1b[37m" 21 + 22 + static void print_json_colored(const char *json, FILE *stream) { 23 + bool in_string = false; 24 + bool is_key = true; 25 + bool escape_next = false; 26 + 27 + for (const char *p = json; *p; p++) { 28 + if (escape_next) { 29 + fputc(*p, stream); 30 + escape_next = false; 31 + continue; 32 + } 33 + 34 + if (*p == '\\' && in_string) { 35 + fputc(*p, stream); 36 + escape_next = true; 37 + continue; 38 + } 39 + 40 + if (*p == '"') { 41 + if (!in_string) { 42 + fprintf(stream, "%s\"", is_key ? JSON_KEY : JSON_STRING); 43 + in_string = true; 44 + } else { 45 + fprintf(stream, "\"%s", ANSI_RESET); 46 + in_string = false; 47 + } 48 + continue; 49 + } 50 + 51 + if (in_string) { 52 + fputc(*p, stream); 53 + continue; 54 + } 55 + 56 + if (*p == ':') { 57 + fputc(*p, stream); 58 + is_key = false; 59 + continue; 60 + } 61 + 62 + if (*p == ',' || *p == '{' || *p == '[') { 63 + if (*p == '{' || *p == '[') { 64 + fprintf(stream, "%s%c%s", JSON_BRACE, *p, ANSI_RESET); 65 + } else { 66 + fputc(*p, stream); 67 + } 68 + is_key = (*p == ',' || *p == '{'); 69 + continue; 70 + } 71 + 72 + if (*p == '}' || *p == ']') { 73 + fprintf(stream, "%s%c%s", JSON_BRACE, *p, ANSI_RESET); 74 + continue; 75 + } 76 + 77 + if (strncmp(p, "true", 4) == 0) { 78 + fprintf(stream, "%strue%s", JSON_BOOL, ANSI_RESET); 79 + p += 3; 80 + continue; 81 + } 82 + 83 + if (strncmp(p, "false", 5) == 0) { 84 + fprintf(stream, "%sfalse%s", JSON_BOOL, ANSI_RESET); 85 + p += 4; 86 + continue; 87 + } 88 + 89 + if (strncmp(p, "null", 4) == 0) { 90 + fprintf(stream, "%snull%s", JSON_NULL, ANSI_RESET); 91 + p += 3; 92 + continue; 93 + } 94 + 95 + if ((*p >= '0' && *p <= '9') || *p == '-') { 96 + fprintf(stream, "%s", JSON_NUMBER); 97 + while ((*p >= '0' && *p <= '9') || *p == '.' || *p == '-' || *p == '+' || *p == 'e' || *p == 'E') { 98 + fputc(*p, stream); 99 + if (!(*(p + 1) >= '0' && *(p + 1) <= '9') && *(p + 1) != '.' && *(p + 1) != '-' && *(p + 1) != '+' && *(p + 1) != 'e' && *(p + 1) != 'E') break; 100 + p++; 101 + } 102 + fprintf(stream, "%s", ANSI_RESET); 103 + continue; 104 + } 105 + 106 + fputc(*p, stream); 107 + } 108 + } 109 + 14 110 static void console_print(struct js *js, jsval_t *args, int nargs, const char *color, FILE *stream) { 15 111 if (color) fprintf(stream, "%s", color); 16 112 17 113 for (int i = 0; i < nargs; i++) { 18 114 const char *space = i == 0 ? "" : " "; 115 + fprintf(stream, "%s", space); 19 116 20 117 if (js_type(args[i]) == JS_STR) { 21 118 char *str = js_getstr(js, args[i], NULL); 22 - fprintf(stream, "%s%s", space, str); 119 + fprintf(stream, "%s", str); 23 120 } else { 24 - fprintf(stream, "%s%s", space, js_str(js, args[i])); 121 + const char *str = js_str(js, args[i]); 122 + if (str[0] == '{' || str[0] == '[') { 123 + if (color) fprintf(stream, "%s", ANSI_RESET); 124 + print_json_colored(str, stream); 125 + if (color) fprintf(stream, "%s", color); 126 + } else { 127 + fprintf(stream, "%s", str); 128 + } 25 129 } 26 130 } 27 131
+3
tests/console.cjs
··· 1 + console.log('log', { some: 'json', value: true }); 2 + console.warn('warn', { some: 'json', value: true }); 3 + console.error('error', { some: 'json', value: true });