|
| 1 | +/* Copyright 2025 Charlie Gordon |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +module color; |
| 17 | + |
| 18 | +import ctype local; |
| 19 | +import stdio local; |
| 20 | +import stdlib local; |
| 21 | +import string local; |
| 22 | + |
| 23 | +const char*[] standardColors = { |
| 24 | + "black", Black, |
| 25 | + "red", Red, |
| 26 | + "green", Green, |
| 27 | + "yellow", Yellow, |
| 28 | + "blue", Blue, |
| 29 | + "magenta", Magenta, |
| 30 | + "cyan", Cyan, |
| 31 | + "grey", Grey, |
| 32 | + "darkgrey", Darkgrey, |
| 33 | + "bred", Bred, |
| 34 | + "bgreen", Bgreen, |
| 35 | + "byellow", Byellow, |
| 36 | + "bblue", Bblue, |
| 37 | + "bmagenta", Bmagenta, |
| 38 | + "bcyan", Bcyan, |
| 39 | + "white", White, |
| 40 | + "normal", Normal, |
| 41 | +} |
| 42 | + |
| 43 | +fn bool getStyleDef(char* buf1, u32 size1, char* buf2, u32 size2, const char** pp) { |
| 44 | + const char *p = *pp; |
| 45 | + while (isspace(*p)) |
| 46 | + p++; |
| 47 | + if (!*p) |
| 48 | + return false; |
| 49 | + u32 i = 0; |
| 50 | + while (isalpha(*p) || *p == '.' || *p == '_') { |
| 51 | + char c = (char)tolower(*p++); |
| 52 | + if (i + 1 < size1) |
| 53 | + buf1[i++] = c; |
| 54 | + } |
| 55 | + buf1[i] = '\0'; |
| 56 | + if (*p != '=' && *p != ':') |
| 57 | + return false; |
| 58 | + p++; |
| 59 | + i = 0; |
| 60 | + while (*p && *p != ' ' && *p != ',' && *p != ';') { |
| 61 | + char c = (char)tolower(*p++); |
| 62 | + if (i + 1 < size2 && c != '-' && c != '_') |
| 63 | + buf2[i++] = c; |
| 64 | + } |
| 65 | + buf2[i] = '\0'; |
| 66 | + if (*p == ',' || *p == ';') |
| 67 | + p++; |
| 68 | + *pp = p; |
| 69 | + return true; |
| 70 | +} |
| 71 | + |
| 72 | +fn bool matchColorName(const char *p, const char *name) { |
| 73 | + while (*p) { |
| 74 | + char c = *p++; |
| 75 | + if (c == 'b' && !strncmp(p, "right", 5)) |
| 76 | + p += 5; |
| 77 | + if (c != *name++) |
| 78 | + return false; |
| 79 | + } |
| 80 | + return *name == '\0'; |
| 81 | +} |
| 82 | + |
| 83 | +fn const char* convertColor(const char *val, const char *def) { |
| 84 | + if (*val == '\0') |
| 85 | + return ""; |
| 86 | + |
| 87 | + for (u32 i = 0; i < elemsof(standardColors); i += 2) { |
| 88 | + if (matchColorName(val, standardColors[i])) |
| 89 | + return standardColors[i + 1]; |
| 90 | + } |
| 91 | + if (!strcasecmp(val, "default")) |
| 92 | + return def; |
| 93 | + |
| 94 | + char[32] buf; |
| 95 | + i32 pal, r, g, b; |
| 96 | + if (sscanf(val, "%*1[pP]%d", &pal) == 1) { |
| 97 | + snprintf(buf, elemsof(buf), "\033[38;5;%dm", pal); |
| 98 | + } else |
| 99 | + if (sscanf(val, "#%2x%2x%2x", &r, &g, &b) == 3) { |
| 100 | + snprintf(buf, elemsof(buf), "\033[38;2;%d;%d;%dm", r, g, b); |
| 101 | + } else { |
| 102 | + // TODO: complain about unknown color |
| 103 | + return def; |
| 104 | + } |
| 105 | + return strdup(buf); |
| 106 | +} |
| 107 | + |
| 108 | +public fn void freeConfigColor(const char* p) { |
| 109 | + if (p && *p) { |
| 110 | + for (u32 i = 0; i < elemsof(standardColors); i++) { |
| 111 | + if (standardColors[i] == p) return; |
| 112 | + } |
| 113 | + free((void*)p); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +public fn const char* getConfigColor(const char* cat, const char* def) { |
| 118 | + const char *c2_colors = getenv("C2_COLORS"); |
| 119 | + if (c2_colors) { |
| 120 | + const char *p = c2_colors; |
| 121 | + char[16] style; |
| 122 | + char[16] val; |
| 123 | + if (!strcmp(p, "none")) |
| 124 | + return ""; |
| 125 | + while (getStyleDef(style, elemsof(style), val, elemsof(val), &p)) { |
| 126 | + if (!strcmp(style, cat)) |
| 127 | + return convertColor(val, def); |
| 128 | + } |
| 129 | + } |
| 130 | + return def; |
| 131 | +} |
0 commit comments