sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

dwm-sgrstatus-20220223-6.3.diff (6857B)


      1 From dc83e43aad7b8070911e8210b58cc36bdc4214d5 Mon Sep 17 00:00:00 2001
      2 From: Santtu Lakkala <inz@inz.fi>
      3 Date: Wed, 23 Feb 2022 11:55:19 +0200
      4 Subject: [PATCH 1/2] Allow CSI SGR in status bar
      5 
      6 ---
      7  config.def.h |  19 ++++++
      8  dwm.c        | 182 ++++++++++++++++++++++++++++++++++++++++++++++++++-
      9  2 files changed, 198 insertions(+), 3 deletions(-)
     10 
     11 diff --git a/config.def.h b/config.def.h
     12 index a2ac963..3dc34bd 100644
     13 --- a/config.def.h
     14 +++ b/config.def.h
     15 @@ -18,6 +18,25 @@ static const char *colors[][3]      = {
     16  	[SchemeSel]  = { col_gray4, col_cyan,  col_cyan  },
     17  };
     18  
     19 +static const char *barcolors[] = {
     20 +	"#000000",
     21 +	"#7f0000",
     22 +	"#007f00",
     23 +	"#7f7f00",
     24 +	"#00007f",
     25 +	"#7f007f",
     26 +	"#007f7f",
     27 +	"#cccccc",
     28 +	"#333333",
     29 +	"#ff0000",
     30 +	"#00ff00",
     31 +	"#ffff00",
     32 +	"#0000ff",
     33 +	"#ff00ff",
     34 +	"#00ffff",
     35 +	"#ffffff",
     36 +};
     37 +
     38  /* tagging */
     39  static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
     40  
     41 diff --git a/dwm.c b/dwm.c
     42 index a96f33c..cb9484a 100644
     43 --- a/dwm.c
     44 +++ b/dwm.c
     45 @@ -237,7 +237,7 @@ static void zoom(const Arg *arg);
     46  
     47  /* variables */
     48  static const char broken[] = "broken";
     49 -static char stext[256];
     50 +static char stext[512];
     51  static int screen;
     52  static int sw, sh;           /* X display screen geometry width, height */
     53  static int bh, blw = 0;      /* bar geometry */
     54 @@ -264,6 +264,7 @@ static Atom wmatom[WMLast], netatom[NetLast];
     55  static int running = 1;
     56  static Cur *cursor[CurLast];
     57  static Clr **scheme;
     58 +static Clr *barclrs;
     59  static Display *dpy;
     60  static Drw *drw;
     61  static Monitor *mons, *selmon;
     62 @@ -693,6 +694,25 @@ dirtomon(int dir)
     63  	return m;
     64  }
     65  
     66 +void
     67 +resetfntlist(Fnt *orighead, Fnt *curhead)
     68 +{
     69 +	if (orighead != curhead) {
     70 +		Fnt *f;
     71 +		for (f = orighead; f->next; f = f->next);
     72 +		f->next = curhead;
     73 +		for (f = f->next; f->next != orighead; f = f->next);
     74 +		f->next = NULL;
     75 +	}
     76 +}
     77 +
     78 +enum SgrFlags {
     79 +	REVERSE = 1 << 0,
     80 +	UNDERLINE = 1 << 1,
     81 +	STRIKETHROUGH = 1 << 2,
     82 +	OVERLINE = 1 << 3
     83 +};
     84 +
     85  void
     86  drawbar(Monitor *m)
     87  {
     88 @@ -707,9 +727,160 @@ drawbar(Monitor *m)
     89  
     90  	/* draw status first so it can be overdrawn by tags later */
     91  	if (m == selmon) { /* status is only drawn on selected monitor */
     92 +		char buffer[sizeof(stext)];
     93 +		Clr scm[3];
     94 +		int wr, rd;
     95 +		int pw;
     96 +		int fg = 7;
     97 +		int bg = 0;
     98 +		int fmt = 0;
     99 +		int lp = lrpad / 2 - 2;
    100 +		Fnt *fset = drw->fonts;
    101 +
    102 +		memcpy(scm, scheme[SchemeNorm], sizeof(scm));
    103 +
    104 +		drw_setscheme(drw, scm);
    105 +
    106 +		for (tw = 0, wr = 0, rd = 0; stext[rd]; rd++) {
    107 +			if (stext[rd] == '' && stext[rd + 1] == '[') {
    108 +				size_t alen = strspn(stext + rd + 2,
    109 +						     "0123456789;");
    110 +				if (stext[rd + alen + 2] == 'm') {
    111 +					buffer[wr] = '\0';
    112 +					tw += TEXTW(buffer) - lrpad;
    113 +					wr = 0;
    114 +
    115 +					char *ep = stext + rd + 1;
    116 +					while (*ep != 'm') {
    117 +						unsigned v = strtoul(ep + 1, &ep, 10);
    118 +						if (v == 0 || (v >= 10 && v <= 19)) {
    119 +							int fi = v % 10;
    120 +							Fnt *f;
    121 +							Fnt *p;
    122 +							resetfntlist(fset, drw->fonts);
    123 +							for (p = NULL, f = fset; f && fi--; p = f, f = f->next);
    124 +							if (f) {
    125 +								if (p) {
    126 +									p->next = NULL;
    127 +									for (p = f; p->next; p = p->next);
    128 +									p->next = fset;
    129 +								}
    130 +								drw_setfontset(drw, f);
    131 +							} else {
    132 +								drw_setfontset(drw, fset);
    133 +							}
    134 +						}
    135 +					}
    136 +
    137 +					rd += alen + 2;
    138 +					continue;
    139 +				}
    140 +			}
    141 +			buffer[wr++] = stext[rd];
    142 +		}
    143 +		buffer[wr] = '\0';
    144 +
    145 +		tw += TEXTW(buffer) - lrpad / 2 + 2;
    146 +		x = m->ww - tw;
    147 +
    148 +		resetfntlist(fset, drw->fonts);
    149 +		drw_setfontset(drw, fset);
    150 +
    151 +		for (wr = 0, rd = 0; stext[rd]; rd++) {
    152 +			if (stext[rd] == '' && stext[rd + 1] == '[') {
    153 +				size_t alen = strspn(stext + rd + 2,
    154 +						     "0123456789;");
    155 +				if (stext[rd + alen + 2] == 'm') {
    156 +					buffer[wr] = '\0';
    157 +					pw = TEXTW(buffer) - lrpad;
    158 +					drw_text(drw, x, 0, pw + lp, bh, lp, buffer, fmt & REVERSE);
    159 +					if (fmt & UNDERLINE)
    160 +						drw_rect(drw, x, (bh + drw->fonts->h) / 2, pw, 1, 1, fmt & REVERSE); 
    161 +					if (fmt & STRIKETHROUGH)
    162 +						drw_rect(drw, x, bh / 2, pw, 1, 1, fmt & REVERSE); 
    163 +					if (fmt & OVERLINE)
    164 +						drw_rect(drw, x, (bh - drw->fonts->h) / 2, pw, 1, 1, fmt & REVERSE); 
    165 +					x += pw + lp;
    166 +					lp = 0;
    167 +
    168 +					char *ep = stext + rd + 1;
    169 +					while (*ep != 'm') {
    170 +						unsigned v = strtoul(ep + 1, &ep, 10);
    171 +						if (v == 0) {
    172 +							memcpy(scm, scheme[SchemeNorm], sizeof(scm));
    173 +							fg = 7;
    174 +							bg = 0;
    175 +							fmt = 0;
    176 +							resetfntlist(fset, drw->fonts);
    177 +							drw_setfontset(drw, fset);
    178 +						} else if (v == 1) {
    179 +							fg |= 8;
    180 +							scm[0] = barclrs[fg];
    181 +						} else if (v == 4) {
    182 +							fmt |= UNDERLINE;
    183 +						} else if (v == 7) {
    184 +							fmt |= REVERSE;
    185 +						} else if (v == 9) {
    186 +							fmt |= STRIKETHROUGH;
    187 +						} else if (v >= 10 && v <= 19) {
    188 +							int fi = v % 10;
    189 +							Fnt *f;
    190 +							Fnt *p;
    191 +							resetfntlist(fset, drw->fonts);
    192 +							for (p = NULL, f = fset; f && fi--; p = f, f = f->next);
    193 +							if (f) {
    194 +								if (p) {
    195 +									p->next = NULL;
    196 +									for (p = f; p->next; p = p->next);
    197 +									p->next = fset;
    198 +								}
    199 +								drw_setfontset(drw, f);
    200 +							} else {
    201 +								drw_setfontset(drw, fset);
    202 +							}
    203 +						} else if (v == 22) {
    204 +							fg &= ~8;
    205 +							scm[0] = barclrs[fg];
    206 +						} else if (v == 24) {
    207 +							fmt &= ~UNDERLINE;
    208 +						} else if (v == 27) {
    209 +							fmt &= ~REVERSE;
    210 +						} else if (v == 29) {
    211 +							fmt &= ~STRIKETHROUGH;
    212 +						} else if (v >= 30 && v <= 37) {
    213 +							fg = v % 10 | (fg & 8);
    214 +							scm[0] = barclrs[fg];
    215 +						} else if (v >= 40 && v <= 47) {
    216 +							bg = v % 10;
    217 +							scm[1] = barclrs[bg];
    218 +						} else if (v == 53) {
    219 +							fmt |= OVERLINE;
    220 +						} else if (v == 55) {
    221 +							fmt &= ~OVERLINE;
    222 +						}
    223 +					}
    224 +
    225 +					rd += alen + 2;
    226 +					wr = 0;
    227 +
    228 +					drw_setscheme(drw, scm);
    229 +					continue;
    230 +				}
    231 +			}
    232 +			buffer[wr++] = stext[rd];
    233 +		}
    234 +
    235 +		buffer[wr] = '\0';
    236 +		pw = TEXTW(buffer) - lrpad;
    237 +		drw_text(drw, x, 0, pw + lp, bh, lp, buffer, fmt & REVERSE);
    238 +		if (fmt & UNDERLINE)
    239 +			drw_rect(drw, x, (bh + drw->fonts->h) / 2, pw, 1, 1, fmt & REVERSE); 
    240 +		if (fmt & STRIKETHROUGH)
    241 +			drw_rect(drw, x, bh / 2, pw, 1, 1, fmt & REVERSE); 
    242 +		if (fmt & OVERLINE)
    243 +			drw_rect(drw, x, (bh - drw->fonts->h) / 2, pw, 1, 1, fmt & REVERSE); 
    244 +
    245  		drw_setscheme(drw, scheme[SchemeNorm]);
    246 -		tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
    247 -		drw_text(drw, m->ww - tw, 0, tw, bh, 0, stext, 0);
    248  	}
    249  
    250  	for (c = m->clients; c; c = c->next) {
    251 @@ -1574,6 +1745,11 @@ setup(void)
    252  	scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
    253  	for (i = 0; i < LENGTH(colors); i++)
    254  		scheme[i] = drw_scm_create(drw, colors[i], 3);
    255 +
    256 +	barclrs = ecalloc(LENGTH(barcolors), sizeof(Clr));
    257 +	for (i = 0; i < LENGTH(barcolors); i++)
    258 +		drw_clr_create(drw, &barclrs[i], barcolors[i]);
    259 +
    260  	/* init bars */
    261  	updatebars();
    262  	updatestatus();
    263 -- 
    264 2.32.0
    265