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