s2dblocks

statusbar blocks for dwm
Log | Files | Refs

commit e29f19386aca78fce2b96b5475a053594eb66aa3
parent 08dff375521b915f8c11c948e3c0393ee370ca2f
Author: hhvn <dev@hhvn.uk>
Date:   Thu, 14 Apr 2022 12:03:47 +0100

Memory handler

Diffstat:
Ahandlers/mem.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 54 insertions(+), 0 deletions(-)

diff --git a/handlers/mem.c b/handlers/mem.c @@ -0,0 +1,54 @@ +#include <stdio.h> +#include <ctype.h> +#include <string.h> +#include <stdlib.h> +#include <sys/sysinfo.h> +#include "../dwmbar.h" + +#define CONSTLEN(str) ((size_t)((sizeof(str) - sizeof(str[0])) / sizeof(str[0]))) + +int +main(void) { + long double total = 0, used = 0, percent = 0; +#ifdef __linux__ + long double avail = 0; + char buf[1024], *p; + FILE *f; + + if (!(f = fopen("/proc/meminfo", "r"))) { + perror("fopen()"); + return 1; + } + + while (fgets(buf, sizeof(buf), f) && (!avail || !total)) { + for (p = buf; !isdigit(*p) && *p; p++); + if (strncmp(buf, "MemTotal:", CONSTLEN("MemTotal:")) == 0) { + total = strtold(p, NULL); + } else if (strncmp(buf, "MemAvailable:", CONSTLEN("MemAvailable:")) == 0) { + avail = strtold(p, NULL); + } + } + fclose(f); + + used = total - avail; +#else + s2d_print("no handler for this OS"); + s2d_finish(); +#endif + percent = (used / total) * 100; + s2d_bg(BRBG); + if (percent >= 75) + s2d_fg(RED); + else if (percent >= 60) + s2d_fg(ORANGE); + else if (percent >= 50) + s2d_fg(YELLOW); + else + s2d_fg(GREEN); + s2d_bar(0, 2, 3, bar_height - 4, 1, percent); + s2d_reset(0, 1); + s2d_forward(-1); + s2d_forward(2); + s2d_print("%.0LF%%", percent); + s2d_finish(); +}