3 * Configurable terminal colour support
5 * (c) 2024 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU Library General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * mLib is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 * License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib. If not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28 /*----- Header files ------------------------------------------------------*/
37 #include "ttycolour.h"
39 /*----- Main code ---------------------------------------------------------*/
41 int ttycolour_config(ttycolour_attr *attr, const char *user, unsigned f,
42 const struct ttycolour_style *tab)
49 for (i = 0; tab[i].tok; i++) attr[i] = tab[i].dflt;
51 if (f&TCIF_GETENV) p = getenv(user);
55 n = strcspn(p, "=:"); q = p + n;
56 if (!*q || *q == ':') {
57 if (f&TCIF_REPORT) moan("missing colour token in `%s'", user);
58 rc = -1; p = *q ? q + 1 : q; continue;
60 for (i = 0; tab[i].tok; i++)
61 if (STRNCMP(p, ==, tab[i].tok, n) && !tab[i].tok[n])
64 moan("unknown colour token `%.*s' in `%s'", (int)n, p, user);
65 rc = -1; q = p + strcspn(p, ":"); p = *q ? q + 1 : q; continue;
72 { arg = 10*arg + (*q++ - '0'); continue; }
76 case 1: a |= TCAF_BOLD; break;
77 case 39: a &= ~(TCAF_FG | TCAF_FGMASK); break;
78 case 49: a &= ~(TCAF_BG | TCAF_BGMASK); break;
79 case 30: case 31: case 32: case 33:
80 case 34: case 35: case 36: case 37:
81 a = (a&~TCAF_FGMASK) | TCAF_FG | ((arg - 30) << TCAF_FGSHIFT);
83 case 40: case 41: case 42: case 43:
84 case 44: case 45: case 46: case 47:
85 a = (a&~TCAF_BGMASK) | TCAF_BG | ((arg - 40) << TCAF_BGSHIFT);
87 case 90: case 91: case 92: case 93:
88 case 94: case 95: case 96: case 97:
89 a = (a&~TCAF_FGMASK) | TCAF_FG |
90 (((arg - 90) | TCCF_BRIGHT) << TCAF_FGSHIFT);
92 case 100: case 101: case 102: case 103:
93 case 104: case 105: case 106: case 107:
94 a = (a&~TCAF_BGMASK) | TCAF_BG |
95 (((arg - 100) | TCCF_BRIGHT) << TCAF_BGSHIFT);
99 moan("unknown colour code %u in `%.*s' string in `%s'",
100 arg, (int)n, p, user);
104 if (!*q || *q == ':') break;
105 else if (*q != ';') {
107 moan("expected `;' but found `%c' in `%.*s' string in `%s'",
108 *q, (int)n, p, user);
109 rc = -1; q += strcspn(q, ":;"); if (!*q || *q == ':') break;
113 attr[i] = a; p = *q ? q + 1 : q;
119 void ttycolour_init(struct ttycolour_state *tc)
122 /* --- @ttycolour_setattr@ --- *
124 * Arguments: @const struct gprintf_ops *ops@ = formatting operations
125 * @void *go@ = output sink
126 * @struct ttycolour_state *tc
127 * @ttycolour_attr attr@ = attribute code to set
129 * Returns: Zero on success, %$-1$% on error.
131 * Use: Send a control sequence to the output stream so that
132 * subsequent text is printed with the given attributes.
134 * Some effort is taken to avoid unnecessary control sequences.
135 * In particular, if @attr@ matches the current terminal
136 * settings already, then nothing is written.
139 int ttycolour_setattr(const struct gprintf_ops *gops, void *go,
140 struct ttycolour_state *tc, ttycolour_attr attr)
142 unsigned diff = tc->attr ^ attr;
147 #define PUT_SEMI do { \
148 if (!(f&f_semi)) f |= f_semi; \
149 else if (gops->putch(go, ';') < 0) return (-1); \
152 #define SET_COLOUR(norm, bright, colour) do { \
153 unsigned char _col = (colour); \
156 gprintf(gops, go, "%d", \
157 (_col&TCCF_BRIGHT ? (bright) : (norm)) + (_col&TCCF_RGBMASK)); \
160 /* If there's nothing to do, we might as well stop now. */
161 if (!diff) return (0);
163 /* Start on the control command. */
164 if (gops->putm(go, "\x1b[", 2) < 0) return (-1);
166 /* Change the boldness if necessary. */
167 if (diff&TCAF_BOLD) {
169 if (attr&TCAF_BOLD) { if (gops->putch(go, '1') < 0) return (-1); }
170 else { diff = tc->attr; if (gops->putch(go, '0') < 0) return (-1); }
173 /* Change the foreground colour if necessary. */
174 if (diff&(TCAF_FG | TCAF_FGMASK)) {
176 SET_COLOUR(30, 90, (attr&TCAF_FGMASK) >> TCAF_FGSHIFT);
178 { PUT_SEMI; if (gops->putm(go, "39", 2) < 0) return (-1); }
181 /* Change the background colour if necessary. */
182 if (diff&(TCAF_BG | TCAF_BGMASK)) {
184 SET_COLOUR(40, 100, (attr&TCAF_BGMASK) >> TCAF_BGSHIFT);
186 { PUT_SEMI; if (gops->putm(go, "49", 2) < 0) return (-1); }
189 /* Terminate the control command and save the new attributes. */
190 if (gops->putch(go, 'm') < 0) return (-1);
191 tc->attr = attr; return (0);
198 /*----- That's all, folks -------------------------------------------------*/