|
14 | 14 | */
|
15 | 15 |
|
16 | 16 | module color;
|
| 17 | + |
| 18 | +import ctype; |
| 19 | +import stdio; |
| 20 | +import stdlib; |
| 21 | +import string; |
17 | 22 | import unistd;
|
18 | 23 |
|
19 |
| -public const char[] Black = "\033[0;30m"; |
20 |
| -public const char[] Red = "\033[0;31m"; |
21 |
| -public const char[] Green = "\033[0;32m"; |
22 |
| -public const char[] Yellow = "\033[0;33m"; |
23 |
| -public const char[] Blue = "\033[0;34m"; |
24 |
| -public const char[] Magenta = "\033[0;35m"; |
25 |
| -public const char[] Cyan = "\033[0;36m"; |
26 |
| -public const char[] Grey = "\033[0;37m"; |
27 |
| -public const char[] Darkgrey = "\033[01;30m"; |
28 |
| -public const char[] Bred = "\033[01;31m"; |
29 |
| -public const char[] Bgreen = "\033[01;32m"; |
30 |
| -public const char[] Byellow = "\033[01;33m"; |
31 |
| -public const char[] Bblue = "\033[01;34m"; |
32 |
| -public const char[] Bmagenta = "\033[01;35m"; |
33 |
| -public const char[] Bcyan = "\033[01;36m"; |
34 |
| -public const char[] White = "\033[01;37m"; |
35 |
| -public const char[] Normal = "\033[0m"; |
| 24 | +public const char[] Black @(unused) = "\033[0;30m"; |
| 25 | +public const char[] Red @(unused) = "\033[0;31m"; |
| 26 | +public const char[] Green @(unused) = "\033[0;32m"; |
| 27 | +public const char[] Yellow @(unused) = "\033[0;33m"; |
| 28 | +public const char[] Blue @(unused) = "\033[0;34m"; |
| 29 | +public const char[] Magenta @(unused) = "\033[0;35m"; |
| 30 | +public const char[] Cyan @(unused) = "\033[0;36m"; |
| 31 | +public const char[] Grey @(unused) = "\033[0;37m"; |
| 32 | +public const char[] Darkgrey @(unused) = "\033[01;30m"; |
| 33 | +public const char[] Bred @(unused) = "\033[01;31m"; |
| 34 | +public const char[] Bgreen @(unused) = "\033[01;32m"; |
| 35 | +public const char[] Byellow @(unused) = "\033[01;33m"; |
| 36 | +public const char[] Bblue @(unused) = "\033[01;34m"; |
| 37 | +public const char[] Bmagenta @(unused) = "\033[01;35m"; |
| 38 | +public const char[] Bcyan @(unused) = "\033[01;36m"; |
| 39 | +public const char[] White @(unused) = "\033[01;37m"; |
| 40 | +public const char[] Normal @(unused) = "\033[0m"; |
| 41 | + |
| 42 | +const char*[] standardColors = { |
| 43 | + "black", Black, |
| 44 | + "red", Red, |
| 45 | + "green", Green, |
| 46 | + "yellow", Yellow, |
| 47 | + "blue", Blue, |
| 48 | + "magenta", Magenta, |
| 49 | + "cyan", Cyan, |
| 50 | + "grey", Grey, |
| 51 | + "darkgrey", Darkgrey, |
| 52 | + "bred", Bred, |
| 53 | + "bgreen", Bgreen, |
| 54 | + "byellow", Byellow, |
| 55 | + "bblue", Bblue, |
| 56 | + "bmagenta", Bmagenta, |
| 57 | + "bcyan", Bcyan, |
| 58 | + "white", White, |
| 59 | + "normal", Normal, |
| 60 | + "brightred", Bred, |
| 61 | + "brightgreen", Bgreen, |
| 62 | + "brightyellow", Byellow, |
| 63 | + "brightblue", Bblue, |
| 64 | + "brightmagenta", Bmagenta, |
| 65 | + "brightcyan", Bcyan, |
| 66 | +} |
| 67 | + |
| 68 | +// colors used in the parser |
| 69 | +public const char* normal = color.Normal; |
| 70 | +public const char* keyword = color.Green; |
| 71 | +public const char* identifier = color.Cyan; |
| 72 | +public const char* literal = color.Cyan; |
| 73 | +public const char* comment = color.Cyan; |
| 74 | +public const char* warning = color.Yellow; |
| 75 | +public const char* error = color.Red; |
| 76 | + |
| 77 | +i8 usecolors = -1; |
| 78 | +char *c2_colors; |
| 79 | + |
| 80 | +fn bool getStyleDef(char* buf1, u32 size1, char* buf2, u32 size2, const char** pp) { |
| 81 | + const char *p = *pp; |
| 82 | + while (ctype.isspace(*p)) |
| 83 | + p++; |
| 84 | + if (!*p) |
| 85 | + return false; |
| 86 | + u32 i = 0; |
| 87 | + while (ctype.isalpha(*p) || *p == '.' || *p == '_') { |
| 88 | + char c = cast<char>(ctype.tolower(*p++)); |
| 89 | + if (i + 1 < size1) |
| 90 | + buf1[i++] = c; |
| 91 | + } |
| 92 | + buf1[i] = '\0'; |
| 93 | + if (*p != '=' && *p != ':') |
| 94 | + return false; |
| 95 | + p++; |
| 96 | + i = 0; |
| 97 | + while (*p && *p != ' ' && *p != ',' && *p != ';') { |
| 98 | + char c = cast<char>(ctype.tolower(*p++)); |
| 99 | + if (i + 1 < size2 && c != '-' && c != '_') |
| 100 | + buf2[i++] = c; |
| 101 | + } |
| 102 | + buf2[i] = '\0'; |
| 103 | + if (*p == ',' || *p == ';') |
| 104 | + p++; |
| 105 | + *pp = p; |
| 106 | + return true; |
| 107 | +} |
| 108 | + |
| 109 | +fn const char* convertColor(const char *val, const char *def) { |
| 110 | + if (*val == '\0') |
| 111 | + return ""; |
| 112 | + |
| 113 | + for (u32 i = 0; i < elemsof(standardColors); i += 2) { |
| 114 | + if (!string.strcmp(standardColors[i], val)) |
| 115 | + return standardColors[i + 1]; |
| 116 | + } |
| 117 | + if (!string.strcmp(val, "default")) |
| 118 | + return def; |
| 119 | + |
| 120 | + char[32] buf; |
| 121 | + i32 pal; |
| 122 | + i32 r; |
| 123 | + i32 g; |
| 124 | + i32 b; |
| 125 | + if (stdio.sscanf(val, "p%d", &pal) == 1) { |
| 126 | + stdio.snprintf(buf, elemsof(buf), "\033[38;5;%dm", pal); |
| 127 | + } else |
| 128 | + if (stdio.sscanf(val, "#%2x%2x%2x", &r, &g, &b) == 3) { |
| 129 | + stdio.snprintf(buf, elemsof(buf), "\033[38;2;%d;%d;%dm", r, g, b); |
| 130 | + } else { |
| 131 | + // TODO: complain about unknown color |
| 132 | + return def; |
| 133 | + } |
| 134 | + return string.strdup(buf); |
| 135 | +} |
| 136 | + |
| 137 | +public fn const char* getConfigColor(const char* cat, const char* def) { |
| 138 | + if (!usecolors) |
| 139 | + return ""; |
| 140 | + if (c2_colors) { |
| 141 | + const char *p = c2_colors; |
| 142 | + char[16] style; |
| 143 | + char[16] val; |
| 144 | + while (getStyleDef(style, elemsof(style), val, elemsof(val), &p)) { |
| 145 | + if (!string.strcmp(style, cat)) |
| 146 | + return convertColor(val, def); |
| 147 | + } |
| 148 | + } |
| 149 | + return def; |
| 150 | +} |
36 | 151 |
|
37 | 152 | public fn bool useColor() {
|
38 |
| - return unistd.isatty(1); |
| 153 | + if (usecolors < 0) { |
| 154 | + usecolors = unistd.isatty(1) != 0; |
| 155 | + if (usecolors) { |
| 156 | + char *p = stdlib.getenv("C2_COLORS"); |
| 157 | + if (p) { |
| 158 | + if (!string.strcmp(p, "none")) |
| 159 | + usecolors = 0; |
| 160 | + else |
| 161 | + c2_colors = p; |
| 162 | + } |
| 163 | + } |
| 164 | + color.normal = getConfigColor("normal", color.Normal); |
| 165 | + color.keyword = getConfigColor("keyword", color.Green); |
| 166 | + color.identifier = getConfigColor("identifier", color.Cyan); |
| 167 | + color.literal = getConfigColor("literal", color.Cyan); |
| 168 | + color.comment = getConfigColor("comment", color.Cyan); |
| 169 | + color.warning = getConfigColor("warning", color.Yellow); |
| 170 | + color.error = getConfigColor("error", color.Red); |
| 171 | + } |
| 172 | + return usecolors; |
39 | 173 | }
|
40 | 174 |
|
0 commit comments