|
| 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 | + [Color.Black] = "black", |
| 25 | + [Color.Red] = "red", |
| 26 | + [Color.Green] = "green", |
| 27 | + [Color.Yellow] = "yellow", |
| 28 | + [Color.Blue] = "blue", |
| 29 | + [Color.Magenta] = "magenta", |
| 30 | + [Color.Cyan] = "cyan", |
| 31 | + [Color.Grey] = "grey", |
| 32 | + [Color.Darkgrey] = "darkgrey", |
| 33 | + [Color.Bred] = "bred", |
| 34 | + [Color.Bgreen] = "bgreen", |
| 35 | + [Color.Byellow] = "byellow", |
| 36 | + [Color.Bblue] = "bblue", |
| 37 | + [Color.Bmagenta] = "bmagenta", |
| 38 | + [Color.Bcyan] = "bcyan", |
| 39 | + [Color.White] = "white", |
| 40 | + [Color.Normal] = "normal", |
| 41 | +} |
| 42 | + |
| 43 | +fn Color findStandardColor(const char* name) { |
| 44 | + for (u32 i = 0; i < elemsof(standardColors); i++) { |
| 45 | + if (standardColors[i] && !strcmp(standardColors[i], name)) |
| 46 | + return (Color)i; |
| 47 | + } |
| 48 | + if (!strncmp(name, "custom", 6)) { |
| 49 | + i32 n = atoi(name + 6); |
| 50 | + if (n > 0 && n <= Color.CustomEnd - Color.CustomStart + 1) |
| 51 | + return Color.CustomStart + n - 1; |
| 52 | + } |
| 53 | + return Color.None; |
| 54 | +} |
| 55 | + |
| 56 | +fn bool getStyleDef(char* buf1, u32 size1, char* buf2, u32 size2, const char** pp) { |
| 57 | + const char *p = *pp; |
| 58 | + while (isspace(*p)) |
| 59 | + p++; |
| 60 | + if (!*p) |
| 61 | + return false; |
| 62 | + u32 i = 0; |
| 63 | + while (isalpha(*p) || *p == '.' || *p == '_') { |
| 64 | + char c = (char)tolower(*p++); |
| 65 | + if (i + 1 < size1) |
| 66 | + buf1[i++] = c; |
| 67 | + } |
| 68 | + buf1[i] = '\0'; |
| 69 | + if (*p != '=' && *p != ':') |
| 70 | + return false; |
| 71 | + p++; |
| 72 | + i = 0; |
| 73 | + while (*p && *p != ' ' && *p != ',' && *p != ';') { |
| 74 | + char c = (char)tolower(*p++); |
| 75 | + if (i + 1 < size2 && c != '-' && c != '_') |
| 76 | + buf2[i++] = c; |
| 77 | + } |
| 78 | + buf2[i] = '\0'; |
| 79 | + if (*p == ',' || *p == ';') |
| 80 | + p++; |
| 81 | + *pp = p; |
| 82 | + return true; |
| 83 | +} |
| 84 | + |
| 85 | +fn bool matchColorName(const char *p, const char *name) { |
| 86 | + if (!name) return false; |
| 87 | + while (*p) { |
| 88 | + char c = *p++; |
| 89 | + if (c == 'b' && !strncmp(p, "right", 5)) |
| 90 | + p += 5; |
| 91 | + if (c != *name++) |
| 92 | + return false; |
| 93 | + } |
| 94 | + return *name == '\0'; |
| 95 | +} |
| 96 | + |
| 97 | +fn const char* makeColorString(const char *val) { |
| 98 | + if (!val || *val == '\0' || !strcasecmp(val, "default")) |
| 99 | + return nil; |
| 100 | + |
| 101 | + for (u32 i = 0; i < elemsof(standardColors); i++) { |
| 102 | + if (matchColorName(val, standardColors[i])) |
| 103 | + return defaultColors[i]; |
| 104 | + } |
| 105 | + |
| 106 | + char[32] buf; |
| 107 | + i32 pal, r, g, b; |
| 108 | + if (sscanf(val, "%*1[pP]%d", &pal) == 1) { |
| 109 | + snprintf(buf, elemsof(buf), "\033[38;5;%dm", pal); |
| 110 | + return strdup(buf); |
| 111 | + } else |
| 112 | + if (sscanf(val, "#%2x%2x%2x", &r, &g, &b) == 3) { |
| 113 | + snprintf(buf, elemsof(buf), "\033[38;2;%d;%d;%dm", r, g, b); |
| 114 | + return strdup(buf); |
| 115 | + } else { |
| 116 | + // TODO: complain about unknown color |
| 117 | + return nil; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +fn Color convertColor(const char *val, Color def) { |
| 122 | + if (!val || *val == '\0') |
| 123 | + return None; |
| 124 | + |
| 125 | + for (u32 i = 0; i < elemsof(standardColors); i++) { |
| 126 | + if (matchColorName(val, standardColors[i])) |
| 127 | + return (Color)i; |
| 128 | + } |
| 129 | + if (!strcasecmp(val, "default")) |
| 130 | + return def; |
| 131 | + |
| 132 | + char[32] buf; |
| 133 | + i32 pal, r, g, b; |
| 134 | + if (sscanf(val, "%*1[pP]%d", &pal) == 1) { |
| 135 | + snprintf(buf, elemsof(buf), "\033[38;5;%dm", pal); |
| 136 | + } else |
| 137 | + if (sscanf(val, "#%2x%2x%2x", &r, &g, &b) == 3) { |
| 138 | + snprintf(buf, elemsof(buf), "\033[38;2;%d;%d;%dm", r, g, b); |
| 139 | + } else { |
| 140 | + // TODO: complain about unknown color |
| 141 | + return def; |
| 142 | + } |
| 143 | + |
| 144 | + for (u32 i = Color.CustomStart; i <= Color.CustomEnd; i++) { |
| 145 | + if (!defaultColors[i]) { |
| 146 | + defaultColors[i] = strdup(buf); |
| 147 | + return (Color)i; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // TODO: complain about out of custom colors |
| 152 | + return def; |
| 153 | +} |
| 154 | + |
| 155 | +public fn void freeCustomColors(Color start, Color end) { |
| 156 | +#if 0 |
| 157 | + if (start < Color.CustomStart) |
| 158 | + start = Color.CustomStart; |
| 159 | + if (end > Color.CustomEnd) |
| 160 | + end = Color.CustomEnd; |
| 161 | + for (u32 i = start; i <= end; i++) { |
| 162 | + if (standardColors[i]) { |
| 163 | + free((char *)standardColors[i]); |
| 164 | + standardColors[i] = nil; |
| 165 | + } |
| 166 | + } |
| 167 | +#endif |
| 168 | +} |
| 169 | + |
| 170 | +public fn Color getConfigColor(const char* varname, const char* cat, Color def) { |
| 171 | + const char *c2_colors = getenv(varname); |
| 172 | + if (c2_colors) { |
| 173 | + const char *p = c2_colors; |
| 174 | + char[16] style; |
| 175 | + char[16] val; |
| 176 | + if (!strcmp(p, "none")) |
| 177 | + return None; |
| 178 | + while (getStyleDef(style, elemsof(style), val, elemsof(val), &p)) { |
| 179 | + if (!strcmp(style, cat)) |
| 180 | + return convertColor(val, def); |
| 181 | + } |
| 182 | + } |
| 183 | + return def; |
| 184 | +} |
| 185 | + |
| 186 | +public fn void getConfigColors(const char *p, Color* styles, const char** names, u32 n) { |
| 187 | + if (!p) return; |
| 188 | + |
| 189 | + char[16] name; |
| 190 | + char[16] definition; |
| 191 | + if (!strcmp(p, "none")) { |
| 192 | + for (u32 i = 0; i < n; i++) styles[i] = None; |
| 193 | + return; |
| 194 | + } |
| 195 | + while (getStyleDef(name, elemsof(name), definition, elemsof(definition), &p)) { |
| 196 | + // check for standard color customisation |
| 197 | + if (Color col = findStandardColor(name)) { |
| 198 | + const char* s = makeColorString(definition); |
| 199 | + if (s) defaultColors[col] = s; |
| 200 | + continue; |
| 201 | + } |
| 202 | + // check for style definition |
| 203 | + for (u32 i = 0; i < n; i++) { |
| 204 | + if (names[i] && !strcmp(names[i], name)) { |
| 205 | + styles[i] = convertColor(definition, styles[i]); |
| 206 | + break; |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments