commit 52c0b905a20dd9d28afa542efb660e4884ae80e1
Author: thing1 <thing1@seacrossedlovers.xyz>
Date: Thu, 25 Sep 2025 09:39:55 +0100
init commit
Diffstat:
| A | Makefile | | | 15 | +++++++++++++++ |
| A | bar | | | 0 | |
| A | bar.c | | | 25 | +++++++++++++++++++++++++ |
| A | bar.sh | | | 6 | ++++++ |
| A | config.h | | | 73 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 files changed, 119 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,15 @@
+CLFAGS=-Os
+
+all: bar
+
+bar: bar.c config.h
+ cc bar.c -o bar ${CFLAGS}
+clean:
+ rm -rf bar
+install: bar
+ cp bar /usr/local/bin/
+ cp bar.sh /usr/local/bin/
+uninstall: bar
+ rm /usr/local/bin/bar
+ rm /usr/local/bin/bar.sh
+
diff --git a/bar b/bar
Binary files differ.
diff --git a/bar.c b/bar.c
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "config.h"
+#define LEN(x) sizeof(x) / sizeof(x[0])
+
+char *
+update_bar() {
+ static char bar[MAXBAR] = { 0 };
+ int off = 0;
+ for (int i = 0; i < LEN(entrys); i++)
+ off += snprintf(bar + off, MAXBAR - off, "%s%s%s",
+ (i == 0) ? "" : SEP,
+ entrys[i].func(),
+ (i != LEN(entrys)) ? "" : SEP
+ );
+ return bar;
+}
+
+int
+main() {
+ printf("%s\n", update_bar());
+}
diff --git a/bar.sh b/bar.sh
@@ -0,0 +1,6 @@
+#!/bin/sh
+
+while [ 1 ]; do
+ xsetroot -name "$(bar)"
+ sleep 30
+done
diff --git a/config.h b/config.h
@@ -0,0 +1,73 @@
+#include <time.h>
+#include <stdio.h>
+#include <assert.h>
+
+#define MAXBAR 256
+#define SEP " | "
+
+enum units {
+ KB = 0,
+ MB = 1,
+ GB = 2,
+} unit;
+
+float
+human(float num) {
+ unit = 0;
+ while ((num / 1000) > 1) {
+ num /= 1000;
+ unit++;
+ }
+ return num;
+}
+
+char *
+unitstr() {
+ switch (unit) {
+ case KB: return "kb";
+ case MB: return "mb";
+ case GB: return "gb";
+ }
+ return "";
+}
+
+char *
+get_time() {
+ time_t t = time(NULL);
+ struct tm *tval = localtime(&t);
+ static char timestr[32];
+ strftime(timestr, 32, "date: %m/%d %H:%M", tval);
+ return timestr;
+}
+
+char *
+get_mem() {
+ static char memstr[32];
+ float total, available, used;
+ char line[128] = {0}, *tok, *usedstr, *totalstr;
+ FILE *f = fopen("/proc/meminfo", "r"); /* only works on linux systems */
+
+ while (fgets(line, 128, f)) {
+ assert((tok = strtok(line, ":")));
+ if (strcmp(tok, "MemTotal") == 0)
+ total = atoi(strtok(NULL, ""));
+ else if (strcmp(tok, "MemAvailable") == 0)
+ available = atoi(strtok(NULL, ""));
+ }
+ fclose(f);
+
+ used = human(total - available);
+ usedstr = unitstr();
+ total = human(total);
+ totalstr = unitstr();
+
+ snprintf(memstr, 32, "mem: %.1f%s/%.1f%s", used, usedstr, total, totalstr);
+ return memstr;
+}
+
+struct entry {
+ char *(*func)();
+} entrys[] = {
+ {get_mem},
+ {get_time},
+};