dmenu-numbers-20220512-28fb3e2.diff (2804B)
1 From c4cd209c2e322563750d09a3b64194d11cc12a10 Mon Sep 17 00:00:00 2001 2 From: Ehsan Ghorbannezhad <ehsan@disroot.org> 3 Date: Thu, 12 May 2022 22:32:47 +0430 4 Subject: [PATCH] the numbers patch, updated to fix segfault in some conditions 5 6 --- 7 dmenu.c | 27 ++++++++++++++++++++++++--- 8 1 file changed, 24 insertions(+), 3 deletions(-) 9 10 diff --git a/dmenu.c b/dmenu.c 11 index 571bc35..70004e7 100644 12 --- a/dmenu.c 13 +++ b/dmenu.c 14 @@ -24,6 +24,8 @@ 15 * MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org))) 16 #define LENGTH(X) (sizeof X / sizeof X[0]) 17 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) 18 +#define NUMBERSMAXDIGITS 100 19 +#define NUMBERSBUFSIZE (NUMBERSMAXDIGITS * 2) + 1 20 21 /* enums */ 22 enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */ 23 @@ -34,6 +36,7 @@ struct item { 24 int out; 25 }; 26 27 +static char numbers[NUMBERSBUFSIZE] = ""; 28 static char text[BUFSIZ] = ""; 29 static char *embed; 30 static int bh, mw, mh; 31 @@ -86,7 +89,7 @@ calcoffsets(void) 32 if (lines > 0) 33 n = lines * bh; 34 else 35 - n = mw - (promptw + inputw + TEXTW("<") + TEXTW(">")); 36 + n = mw - (promptw + inputw + TEXTW("<") + TEXTW(">") + TEXTW(numbers)); 37 /* calculate which items will begin the next page and previous page */ 38 for (i = 0, next = curr; next; next = next->right) 39 if ((i += (lines > 0) ? bh : textw_clamp(next->text, n)) > n) 40 @@ -143,6 +146,21 @@ drawitem(struct item *item, int x, int y, int w) 41 return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0); 42 } 43 44 +static void 45 +recalculatenumbers() 46 +{ 47 + unsigned int numer = 0, denom = 0; 48 + struct item *item; 49 + if (matchend) { 50 + numer++; 51 + for (item = matchend; item && item->left; item = item->left) 52 + numer++; 53 + } 54 + for (item = items; item && item->text; item++) 55 + denom++; 56 + snprintf(numbers, NUMBERSBUFSIZE, "%d/%d", numer, denom); 57 +} 58 + 59 static void 60 drawmenu(void) 61 { 62 @@ -168,6 +186,7 @@ drawmenu(void) 63 drw_rect(drw, x + curpos, 2, 2, bh - 4, 1, 0); 64 } 65 66 + recalculatenumbers(); 67 if (lines > 0) { 68 /* draw vertical list */ 69 for (item = curr; item != next; item = item->right) 70 @@ -182,13 +201,15 @@ drawmenu(void) 71 } 72 x += w; 73 for (item = curr; item != next; item = item->right) 74 - x = drawitem(item, x, 0, textw_clamp(item->text, mw - x - TEXTW(">"))); 75 + x = drawitem(item, x, 0, textw_clamp(item->text, mw - x - TEXTW(">") - TEXTW(numbers))); 76 if (next) { 77 w = TEXTW(">"); 78 drw_setscheme(drw, scheme[SchemeNorm]); 79 - drw_text(drw, mw - w, 0, w, bh, lrpad / 2, ">", 0); 80 + drw_text(drw, mw - w - TEXTW(numbers), 0, w, bh, lrpad / 2, ">", 0); 81 } 82 } 83 + drw_setscheme(drw, scheme[SchemeNorm]); 84 + drw_text(drw, mw - TEXTW(numbers), 0, TEXTW(numbers), bh, lrpad / 2, numbers, 0); 85 drw_map(drw, win, 0, 0, mw, mh); 86 } 87 88 -- 89 2.36.1