From 494264f3d1dd1ee517d0b03929362facdf1c8301 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Sat, 12 Jun 2021 14:30:23 +0200 Subject: cleaned up dwmstatus --- dwmstatus/custom.diff | 135 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 100 insertions(+), 35 deletions(-) (limited to 'dwmstatus') diff --git a/dwmstatus/custom.diff b/dwmstatus/custom.diff index d5080f3..3d283b4 100644 --- a/dwmstatus/custom.diff +++ b/dwmstatus/custom.diff @@ -1,5 +1,5 @@ diff --git a/dwmstatus.c b/dwmstatus.c -index d2a4b03..4f09dde 100644 +index d2a4b03..2df2e0a 100644 --- a/dwmstatus.c +++ b/dwmstatus.c @@ -17,8 +17,6 @@ @@ -11,7 +11,7 @@ index d2a4b03..4f09dde 100644 char *tzberlin = "Europe/Berlin"; static Display *dpy; -@@ -89,7 +87,7 @@ loadavg(void) +@@ -89,134 +87,84 @@ loadavg(void) if (getloadavg(avgs, 3) < 0) return smprintf(""); @@ -20,58 +20,114 @@ index d2a4b03..4f09dde 100644 } char * -@@ -155,7 +153,7 @@ getbattery(char *base) - } else if(!strncmp(co, "Charging", 8)) { - status = '+'; - } else { -- status = '?'; -+ status = '\0'; - } - - if (remcap < 0 || descap < 0) -@@ -175,47 +173,69 @@ gettemperature(char *base, char *sensor) - return smprintf("%02.0f°C", atof(co) / 1000); - } - -+char * +-readfile(char *base, char *file) +-{ +- char *path, line[513]; +- FILE *fd; +- +- memset(line, 0, sizeof(line)); +- +- path = smprintf("%s/%s", base, file); +- fd = fopen(path, "r"); +- free(path); +getresult(char *command) { + FILE *fd = popen(command, "r"); + char ret[100]; -+ if (fd == NULL) + if (fd == NULL) +- return NULL; + strcpy(ret, "invalid"); -+ + +- if (fgets(line, sizeof(line)-1, fd) == NULL) +- return NULL; +- fclose(fd); + if (fgets(ret, 100, fd) == NULL) + strcpy(ret, "invalid"); -+ + +- return smprintf("%s", line); + if (pclose(fd) == -1) + strcpy(ret, "invalid"); + + return smprintf("%s", ret); -+} -+ -+char * + } + + char * +-getbattery(char *base) +-{ +- char *co, status; +- int descap, remcap; +- +- descap = -1; +- remcap = -1; +- +- co = readfile(base, "present"); +- if (co == NULL) +- return smprintf(""); +- if (co[0] != '1') { +- free(co); +- return smprintf("not present"); +- } +- free(co); +- +- co = readfile(base, "charge_full_design"); +- if (co == NULL) { +- co = readfile(base, "energy_full_design"); +- if (co == NULL) +- return smprintf(""); +- } +- sscanf(co, "%d", &descap); +- free(co); +- +- co = readfile(base, "charge_now"); +- if (co == NULL) { +- co = readfile(base, "energy_now"); +- if (co == NULL) +- return smprintf(""); +- } +- sscanf(co, "%d", &remcap); +- free(co); +- +- co = readfile(base, "status"); +- if (!strncmp(co, "Discharging", 11)) { +- status = '-'; +- } else if(!strncmp(co, "Charging", 8)) { +- status = '+'; +- } else { +- status = '?'; +- } +- +- if (remcap < 0 || descap < 0) +- return smprintf("invalid"); +getpartition(char *partition) { + char *co = smprintf("df -lh | grep %s | tr -s ' ' | cut -d ' ' -f 4 | tr -d '\n'", partition); + return getresult(co); +} -+ + +- return smprintf("%.0f%%%c", ((float)remcap / (float)descap) * 100, status); +char * +gettemp() { + char *co = smprintf("sensors amdgpu-pci-0400 | grep edge | cut -c 16-22 | tr -d '\n'"); + return getresult(co); -+} -+ -+char * + } + + char * +-gettemperature(char *base, char *sensor) +-{ +- char *co; +gettrack() { + char *co = smprintf("mpc -h core-a.lan | head -n 1 | tr -d '\n'"); + return getresult(co); +} -+ + +- co = readfile(base, sensor); +- if (co == NULL) +- return smprintf(""); +- return smprintf("%02.0f°C", atof(co) / 1000); +char * +getips() { -+ char *co = smprintf("ip a | grep 'inet ' | grep -v 127.0.0.1 | tr -s ' ' | cut -f 3 -d ' ' | tr '\n' ' '"); -+ return getresult(co); -+} ++ char *co = smprintf("ip a | grep 'inet ' | grep -v 127.0.0.1 | tr -s ' ' | cut -f 3 -d ' ' | tr '\n' ' '"); ++ return getresult(co); + } + + int main(void) @@ -109,26 +165,35 @@ index d2a4b03..4f09dde 100644 - status = smprintf("T:%s|%s|%s L:%s B:%s|%s A:%s U:%s %s", - t0, t1, t2, avgs, bat, bat1, tmar, tmutc, - tmbln); -+ partition = getpartition("/$"); + tmbln = mktimes("%a %d %b %H:%M", tzberlin); ++ partition = getpartition("/$"); + temp = gettemp(); + track = gettrack(); + ips = getips(); + -+ status = smprintf(" %s | %s | %s | %s | %s | %s", ++ status = smprintf(" %s | %s | %s | %s | %s", + track, avgs, temp, partition, tmbln); setstatus(status); - free(t0); - free(t1); - free(t2); -+ free(track); -+ free(temp); - free(avgs); +- free(avgs); - free(bat); - free(bat1); - free(tmar); - free(tmutc); ++ free(ips); ++ free(track); ++ free(temp); ++ free(partition); free(tmbln); ++ free(avgs); free(status); } + +@@ -224,4 +172,3 @@ main(void) + + return 0; + } +- -- cgit v1.2.3