1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
From 9bfbafa1e98c13c039bea4790941e51b3a8054b4 Mon Sep 17 00:00:00 2001
From: Max Schillinger <maxschillinger@web.de>
Date: Thu, 23 Jun 2022 21:58:37 +0200
Subject: [PATCH] Add multiple color schemes and key bindings to change them
This commits adds these color schemes:
- the default (dark) st color scheme
- the default (dark) alacritty color scheme
- One Half (dark & light)
- Solarized (dark & light)
- Gruvbox (dark & light)
Select one with Alt+1..8.
Select the next one with Alt+0.
Select the previous one with Ctrl+Alt+0.
---
config.def.h | 118 +++++++++++++++++++++++++++++++++++++--------------
st.c | 22 ++++++++++
st.h | 2 +
x.c | 52 ++++++++++++++++++++++-
4 files changed, 160 insertions(+), 34 deletions(-)
diff --git a/config.def.h b/config.def.h
index 91ab8ca..38777fe 100644
--- a/config.def.h
+++ b/config.def.h
@@ -93,46 +93,87 @@ char *termname = "st-256color";
*/
unsigned int tabspaces = 8;
-/* Terminal colors (16 first used in escape sequence) */
-static const char *colorname[] = {
- /* 8 normal colors */
- "black",
- "red3",
- "green3",
- "yellow3",
- "blue2",
- "magenta3",
- "cyan3",
- "gray90",
-
- /* 8 bright colors */
- "gray50",
- "red",
- "green",
- "yellow",
- "#5c5cff",
- "magenta",
- "cyan",
- "white",
-
- [255] = 0,
-
- /* more colors can be added after 255 to use with DefaultXX */
- "#cccccc",
- "#555555",
- "gray90", /* default foreground colour */
- "black", /* default background colour */
+typedef struct {
+ const char* const colors[258]; /* terminal colors */
+ unsigned int fg; /* foreground */
+ unsigned int bg; /* background */
+ unsigned int cs; /* cursor */
+ unsigned int rcs; /* reverse cursor */
+} ColorScheme;
+/*
+ * Terminal colors (16 first used in escape sequence,
+ * 2 last for custom cursor color),
+ * foreground, background, cursor, reverse cursor
+ */
+static const ColorScheme schemes[] = {
+ // st (dark)
+ {{"black", "red3", "green3", "yellow3",
+ "blue2", "magenta3", "cyan3", "gray90",
+ "gray50", "red", "green", "yellow",
+ "#5c5cff", "magenta", "cyan", "white",
+ [256]="#cccccc", "#555555"}, 7, 0, 256, 257},
+
+ // Alacritty (dark)
+ {{"#1d1f21", "#cc6666", "#b5bd68", "#f0c674",
+ "#81a2be", "#b294bb", "#8abeb7", "#c5c8c6",
+ "#666666", "#d54e53", "#b9ca4a", "#e7c547",
+ "#7aa6da", "#c397d8", "#70c0b1", "#eaeaea",
+ [256]="#cccccc", "#555555"}, 7, 0, 256, 257},
+
+ // One Half dark
+ {{"#282c34", "#e06c75", "#98c379", "#e5c07b",
+ "#61afef", "#c678dd", "#56b6c2", "#dcdfe4",
+ "#282c34", "#e06c75", "#98c379", "#e5c07b",
+ "#61afef", "#c678dd", "#56b6c2", "#dcdfe4",
+ [256]="#cccccc", "#555555"}, 7, 0, 256, 257},
+
+ // One Half light
+ {{"#fafafa", "#e45649", "#50a14f", "#c18401",
+ "#0184bc", "#a626a4", "#0997b3", "#383a42",
+ "#fafafa", "#e45649", "#50a14f", "#c18401",
+ "#0184bc", "#a626a4", "#0997b3", "#383a42",
+ [256]="#cccccc", "#555555"}, 7, 0, 256, 257},
+
+ // Solarized dark
+ {{"#073642", "#dc322f", "#859900", "#b58900",
+ "#268bd2", "#d33682", "#2aa198", "#eee8d5",
+ "#002b36", "#cb4b16", "#586e75", "#657b83",
+ "#839496", "#6c71c4", "#93a1a1", "#fdf6e3",
+ [256]="#93a1a1", "#fdf6e3"}, 12, 8, 256, 257},
+
+ // Solarized light
+ {{"#eee8d5", "#dc322f", "#859900", "#b58900",
+ "#268bd2", "#d33682", "#2aa198", "#073642",
+ "#fdf6e3", "#cb4b16", "#93a1a1", "#839496",
+ "#657b83", "#6c71c4", "#586e75", "#002b36",
+ [256]="#586e75", "#002b36"}, 12, 8, 256, 257},
+
+ // Gruvbox dark
+ {{"#282828", "#cc241d", "#98971a", "#d79921",
+ "#458588", "#b16286", "#689d6a", "#a89984",
+ "#928374", "#fb4934", "#b8bb26", "#fabd2f",
+ "#83a598", "#d3869b", "#8ec07c", "#ebdbb2",
+ [256]="#ebdbb2", "#555555"}, 15, 0, 256, 257},
+
+ // Gruvbox light
+ {{"#fbf1c7", "#cc241d", "#98971a", "#d79921",
+ "#458588", "#b16286", "#689d6a", "#7c6f64",
+ "#928374", "#9d0006", "#79740e", "#b57614",
+ "#076678", "#8f3f71", "#427b58", "#3c3836",
+ [256]="#3c3836", "#555555"}, 15, 0, 256, 257},
};
+static const char * const * colorname;
+int colorscheme = 0;
/*
* Default colors (colorname index)
* foreground, background, cursor, reverse cursor
*/
-unsigned int defaultfg = 258;
-unsigned int defaultbg = 259;
-unsigned int defaultcs = 256;
-static unsigned int defaultrcs = 257;
+unsigned int defaultfg;
+unsigned int defaultbg;
+unsigned int defaultcs;
+static unsigned int defaultrcs;
/*
* Default shape of cursor
@@ -201,6 +242,17 @@ static Shortcut shortcuts[] = {
{ TERMMOD, XK_Y, selpaste, {.i = 0} },
{ ShiftMask, XK_Insert, selpaste, {.i = 0} },
{ TERMMOD, XK_Num_Lock, numlock, {.i = 0} },
+ { MODKEY, XK_1, selectscheme, {.i = 0} },
+ { MODKEY, XK_2, selectscheme, {.i = 1} },
+ { MODKEY, XK_3, selectscheme, {.i = 2} },
+ { MODKEY, XK_4, selectscheme, {.i = 3} },
+ { MODKEY, XK_5, selectscheme, {.i = 4} },
+ { MODKEY, XK_6, selectscheme, {.i = 5} },
+ { MODKEY, XK_7, selectscheme, {.i = 6} },
+ { MODKEY, XK_8, selectscheme, {.i = 7} },
+ { MODKEY, XK_9, selectscheme, {.i = 8} },
+ { MODKEY, XK_0, nextscheme, {.i = +1} },
+ { MODKEY|ControlMask, XK_0, nextscheme, {.i = -1} },
};
/*
diff --git a/st.c b/st.c
index 51049ba..3ffe333 100644
--- a/st.c
+++ b/st.c
@@ -2196,6 +2196,28 @@ tstrsequence(uchar c)
term.esc |= ESC_STR;
}
+void
+tupdatebgcolor(int oldbg, int newbg)
+{
+ for (int y = 0; y < term.row; y++) {
+ for (int x = 0; x < term.col; x++) {
+ if (term.line[y][x].bg == oldbg)
+ term.line[y][x].bg = newbg;
+ }
+ }
+}
+
+void
+tupdatefgcolor(int oldfg, int newfg)
+{
+ for (int y = 0; y < term.row; y++) {
+ for (int x = 0; x < term.col; x++) {
+ if (term.line[y][x].fg == oldfg)
+ term.line[y][x].fg = newfg;
+ }
+ }
+}
+
void
tcontrolcode(uchar ascii)
{
diff --git a/st.h b/st.h
index 519b9bd..2700de5 100644
--- a/st.h
+++ b/st.h
@@ -90,6 +90,8 @@ int tattrset(int);
void tnew(int, int);
void tresize(int, int);
void tsetdirtattr(int);
+void tupdatebgcolor(int, int);
+void tupdatefgcolor(int, int);
void ttyhangup(void);
int ttynew(const char *, char *, const char *, char **);
size_t ttyread(void);
diff --git a/x.c b/x.c
index 8a16faa..bc0a48c 100644
--- a/x.c
+++ b/x.c
@@ -59,6 +59,8 @@ static void zoom(const Arg *);
static void zoomabs(const Arg *);
static void zoomreset(const Arg *);
static void ttysend(const Arg *);
+static void nextscheme(const Arg *);
+static void selectscheme(const Arg *);
/* config.h for applying patches and the configuration. */
#include "config.h"
@@ -185,6 +187,7 @@ static void mousesel(XEvent *, int);
static void mousereport(XEvent *);
static char *kmap(KeySym, uint);
static int match(uint, uint);
+static void updatescheme(void);
static void run(void);
static void usage(void);
@@ -785,7 +788,7 @@ xloadcols(void)
for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
} else {
- dc.collen = MAX(LEN(colorname), 256);
+ dc.collen = 258;
dc.col = xmalloc(dc.collen * sizeof(Color));
}
@@ -2008,6 +2011,47 @@ usage(void)
" [stty_args ...]\n", argv0, argv0);
}
+void
+nextscheme(const Arg *arg)
+{
+ colorscheme += arg->i;
+ if (colorscheme >= (int)LEN(schemes))
+ colorscheme = 0;
+ else if (colorscheme < 0)
+ colorscheme = LEN(schemes) - 1;
+ updatescheme();
+}
+
+void
+selectscheme(const Arg *arg)
+{
+ if (BETWEEN(arg->i, 0, LEN(schemes)-1)) {
+ colorscheme = arg->i;
+ updatescheme();
+ }
+}
+
+void
+updatescheme(void)
+{
+ int oldbg, oldfg;
+
+ oldbg = defaultbg;
+ oldfg = defaultfg;
+ colorname = schemes[colorscheme].colors;
+ defaultbg = schemes[colorscheme].bg;
+ defaultfg = schemes[colorscheme].fg;
+ defaultcs = schemes[colorscheme].cs;
+ defaultrcs = schemes[colorscheme].rcs;
+ xloadcols();
+ if (defaultbg != oldbg)
+ tupdatebgcolor(oldbg, defaultbg);
+ if (defaultfg != oldfg)
+ tupdatefgcolor(oldfg, defaultfg);
+ cresize(win.w, win.h);
+ redraw();
+}
+
int
main(int argc, char *argv[])
{
@@ -2060,6 +2104,12 @@ main(int argc, char *argv[])
} ARGEND;
run:
+ colorname = schemes[colorscheme].colors;
+ defaultbg = schemes[colorscheme].bg;
+ defaultfg = schemes[colorscheme].fg;
+ defaultcs = schemes[colorscheme].cs;
+ defaultrcs = schemes[colorscheme].rcs;
+
if (argc > 0) /* eat all remaining arguments */
opt_cmd = argv;
--
2.36.1
|