Patch to allow custom command for bar center text

This commit is contained in:
Alex Selimov 2025-05-25 21:36:41 -04:00
parent 56ddeea9aa
commit 0d3cf443d9
2 changed files with 52 additions and 12 deletions

View File

@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
/* appearance */ /* appearance */
static const char center_command[] = "/home/aselimov/bin/title_text.sh";
static const unsigned int borderpx = 2; /* border pixel of windows */ static const unsigned int borderpx = 2; /* border pixel of windows */
static const unsigned int snap = 32; /* snap pixel */ static const unsigned int snap = 32; /* snap pixel */
static const unsigned int gappih = 15; /* horiz inner gap between windows */ static const unsigned int gappih = 15; /* horiz inner gap between windows */

63
dwm.c
View File

@ -51,6 +51,7 @@
#include "util.h" #include "util.h"
/* macros */ /* macros */
#define MAX_LINE_LENGTH 1024
#define BUTTONMASK (ButtonPressMask | ButtonReleaseMask) #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
#define CLEANMASK(mask) \ #define CLEANMASK(mask) \
(mask & ~(numlockmask | LockMask) & \ (mask & ~(numlockmask | LockMask) & \
@ -198,6 +199,7 @@ typedef struct {
} Rule; } Rule;
/* function declarations */ /* function declarations */
char *exec_command_last_line(const char *command);
static void applyrules(Client *c); static void applyrules(Client *c);
static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int *bw, static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int *bw,
int interact); int interact);
@ -899,20 +901,20 @@ void drawbar(Monitor *m) {
} }
w = blw = TEXTW(m->ltsymbol); w = blw = TEXTW(m->ltsymbol);
drw_setscheme(drw, scheme[SchemeTagsNorm]); drw_setscheme(drw, scheme[SchemeTagsNorm]);
x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0); static char text[MAX_LINE_LENGTH];
strcpy(text, exec_command_last_line(center_command));
int center_length = TEXTW(text);
int center_x = (m->ww - center_length) / 2;
x = drw_text(drw, x, 0, center_x - x, bh, lrpad / 2, m->ltsymbol, 0);
x = center_x;
if ((w = m->ww - tw - x) > bh) { if ((w = m->ww - tw - x) > bh) {
if (m->sel) { if (m->sel) {
// drw_setscheme(drw, scheme[m == selmon ? drw_setscheme(drw, scheme[SchemeNorm]);
// SchemeInfoSel : SchemeInfoNorm]); drw_text(drw, x, 0, w, bh, lrpad / 2, text, 0);
drw_text(drw, x, 0, w, bh, lrpad / 2, " ", 0);
if (m->sel->isfloating)
drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
} else { } else {
drw_setscheme(drw, scheme[SchemeInfoNorm]);
drw_rect(drw, x, 0, w, bh, 1, 1);
} }
} }
drw_map(drw, m->barwin, 0, 0, m->ww, bh); drw_map(drw, m->barwin, 0, 0, m->ww, bh);
} }
@ -1373,8 +1375,8 @@ void moveresize(const Arg *arg) {
&wAbs, &h, &hAbs) != 8) &wAbs, &h, &hAbs) != 8)
return; return;
/* compute new window position; prevent window from be positioned outside the /* compute new window position; prevent window from be positioned outside
* current monitor */ * the current monitor */
nw = c->w + w; nw = c->w + w;
if (wAbs == 'W') if (wAbs == 'W')
nw = w < selmon->mw - 2 * c->bw ? w : selmon->mw - 2 * c->bw; nw = w < selmon->mw - 2 * c->bw ? w : selmon->mw - 2 * c->bw;
@ -1603,9 +1605,11 @@ void run(void) {
XEvent ev; XEvent ev;
/* main event loop */ /* main event loop */
XSync(dpy, False); XSync(dpy, False);
while (running && !XNextEvent(dpy, &ev)) int ticks = 0;
while (running && !XNextEvent(dpy, &ev)) {
if (handler[ev.type]) if (handler[ev.type])
handler[ev.type](&ev); /* call handler */ handler[ev.type](&ev); /* call handler */
}
} }
void scan(void) { void scan(void) {
@ -2601,3 +2605,38 @@ void runAutostart(void) {
system("cd ~/.dwm; ./autostart_blocking.sh"); system("cd ~/.dwm; ./autostart_blocking.sh");
system("cd ~/.dwm; ./autostart.sh &"); system("cd ~/.dwm; ./autostart.sh &");
} }
char *exec_command_last_line(const char *command) {
static char last_line[MAX_LINE_LENGTH];
if (!command) {
return NULL;
}
FILE *pipe = popen(command, "r");
if (!pipe) {
return NULL;
}
char buffer[MAX_LINE_LENGTH];
int found_line = 0;
// Read all lines and keep track of the last non-empty one
while (fgets(buffer, sizeof(buffer), pipe) != NULL) {
// Remove trailing newline if present
size_t len = strlen(buffer);
if (len > 0 && buffer[len - 1] == '\n') {
buffer[len - 1] = '\0';
len--;
}
// Skip empty lines
if (len > 0) {
strcpy(last_line, buffer);
found_line = 1;
}
}
pclose(pipe);
return found_line ? last_line : NULL;
}