waybar version

This commit is contained in:
Noah Swerhun 2024-05-24 18:44:00 -05:00
parent 893de78a69
commit 8b4721dcf5
5 changed files with 116 additions and 48 deletions

View file

@ -24,6 +24,26 @@ build $builddir/compile_commands.json: regen_compile_commands ngen.toml || $buil
build build.ninja: regen_ninjafile ngen.toml || $builddir/compile_commands.json
pool = console
# BEGIN TARGET main
rule cc_main
deps = gcc
depfile = $dep
command = cc -I. -MD -MF $dep -o $out -c $in
description = Building $in -> $out
rule link_main
command = cc -o $out $in
description = Linking $out
build $builddir/main/obj: mkdir
build $builddir/main/dep: mkdir
build $builddir/main/obj/src-main.c.o: cc_main src/main.c
dep = $builddir/main/dep/src-main.c.o.d
build $builddir/main/pss-total-waybar: link_main $builddir/main/obj/src-main.c.o | || $builddir/main/obj $builddir/main/dep
build main: phony $builddir/main/pss-total-waybar
# END TARGET main
# BEGIN TARGET debug
rule cc_debug
deps = gcc
@ -40,8 +60,8 @@ build $builddir/debug/dep: mkdir
build $builddir/debug/obj/src-main.c.o: cc_debug src/main.c
dep = $builddir/debug/dep/src-main.c.o.d
build $builddir/debug/pss-total: link_debug $builddir/debug/obj/src-main.c.o | || $builddir/debug/obj $builddir/debug/dep
build debug: phony $builddir/debug/pss-total
build $builddir/debug/pss-total-waybar: link_debug $builddir/debug/obj/src-main.c.o | || $builddir/debug/obj $builddir/debug/dep
build debug: phony $builddir/debug/pss-total-waybar
# END TARGET debug
# BEGIN TARGET release
@ -60,26 +80,6 @@ build $builddir/release/dep: mkdir
build $builddir/release/obj/src-main.c.o: cc_release src/main.c
dep = $builddir/release/dep/src-main.c.o.d
build $builddir/release/pss-total: link_release $builddir/release/obj/src-main.c.o | || $builddir/release/obj $builddir/release/dep
build release: phony $builddir/release/pss-total
build $builddir/release/pss-total-waybar: link_release $builddir/release/obj/src-main.c.o | || $builddir/release/obj $builddir/release/dep
build release: phony $builddir/release/pss-total-waybar
# END TARGET release
# BEGIN TARGET main
rule cc_main
deps = gcc
depfile = $dep
command = cc -I. -MD -MF $dep -o $out -c $in
description = Building $in -> $out
rule link_main
command = cc -o $out $in
description = Linking $out
build $builddir/main/obj: mkdir
build $builddir/main/dep: mkdir
build $builddir/main/obj/src-main.c.o: cc_main src/main.c
dep = $builddir/main/dep/src-main.c.o.d
build $builddir/main/pss-total: link_main $builddir/main/obj/src-main.c.o | || $builddir/main/obj $builddir/main/dep
build main: phony $builddir/main/pss-total
# END TARGET main

View file

@ -1,2 +1,2 @@
mkdir -p /usr/local/bin
cp build/release/pss-total /usr/local/bin
cp build/release/pss-total-waybar /usr/local/bin

View file

@ -2,7 +2,7 @@
compile_commands = true
[targets.main]
outfile = "pss-total"
outfile = "pss-total-waybar"
compiler_flags = ["-I."]
sources = [
"src/main.c",

View file

@ -27,7 +27,7 @@ double read_file(const char *filename) {
return 0;
break;
default:
perror("cannot open file pointer");
perror("cannot open file pointer to smaps");
assert(fp != NULL);
break;
}
@ -41,7 +41,7 @@ double read_file(const char *filename) {
if (status == EOF) {
if (errno != 0) {
perror("error reading file");
perror("error reading smaps file");
ns_str_destroy(&line);
return 0;
} else {
@ -67,7 +67,7 @@ double read_file(const char *filename) {
return sum;
}
int main(int argc, char **argv) {
double get_total_KiB(void) {
DIR *proc_dp;
struct dirent *proc_ent;
Str smaps_path = {0};
@ -108,40 +108,108 @@ int main(int argc, char **argv) {
closedir(proc_dp);
if (argc == 2) {
if ((strcmp(argv[1], "--pretty") == 0) || (strcmp(argv[1], "-p") == 0)) {
const char *prefixes[] = {"KiB", "MiB", "GiB"};
size_t i = 0;
while (total_KiB >= 1024 && i < 2) {
total_KiB /= 1024;
i++;
return total_KiB;
}
size_t get_memtotal(void) {
FILE *meminfo_fp;
Str line = {0};
int status;
size_t memtotal;
errno = 0;
meminfo_fp = fopen("/proc/meminfo", "r");
if (meminfo_fp == NULL) {
perror("cannot open file pointer to /proc/meminfo");
assert(meminfo_fp != NULL);
}
printf("%0.2f %s\n", total_KiB, prefixes[i]);
exit(0);
} else if ((strcmp(argv[1], "--help") == 0) ||
(strcmp(argv[1], "-h") == 0)) {
ns_str_init_empty(&line, 256);
errno = 0;
status = ns_fget_line(meminfo_fp, &line);
if (status == EOF) {
if (errno != 0) {
perror("error reading meminfo");
ns_str_destroy(&line);
assert(errno == 0);
} else {
fprintf(stderr, "/proc/meminfo is empty... something is wrong.\n");
ns_str_destroy(&line);
exit(1);
}
}
status = sscanf(line.arr, "MemTotal: %lu", &memtotal);
if (status != 1) {
fprintf(stderr, "MemTotal is not on the first line of /proc/meminfo??? \n");
ns_str_destroy(&line);
exit(1);
}
ns_str_destroy(&line);
return memtotal;
}
int main(int argc, char **argv) {
const char *prefixes[] = {"KiB", "MiB", "GiB"};
const char *classes[] = {"low", "quarter", "half", "three_quarters",
"nine_tenths"};
char mem_text[16] = {0};
size_t prefix_index = 0, classes_index = 0;
double total_KiB = 0, pretty_total = 0, percentage_use = 0;
if (argc == 2) {
if ((strcmp(argv[1], "--help") == 0) || (strcmp(argv[1], "-h") == 0)) {
printf("DESCRIPTION\n");
printf(
" pss-total: sum proportional set size of every process for "
"accurate memory\n");
printf(
" usage statistics. In non-pretty mode, the number is given in "
"kibibytes.\n");
" usage statistics. This branch outputs json for use in waybar.\n");
printf("\n");
printf("SUMMARY\n");
printf(" pss-total [OPTIONS]\n");
printf("\n");
printf("OPTIONS\n");
printf(" -p, --pretty\n");
printf(" format output using sensible units: KiB, MiB, or GiB.\n");
printf("\n");
printf(" -h, --help\n");
printf(" print this help message.\n");
exit(0);
}
}
printf("%.0f\n", total_KiB);
total_KiB = get_total_KiB();
pretty_total = total_KiB;
while (pretty_total >= 1024 && prefix_index < 2) {
pretty_total /= 1024;
prefix_index++;
}
snprintf((char *restrict)&mem_text, 16, "%0.2f %s", pretty_total,
prefixes[prefix_index]);
percentage_use = total_KiB / get_memtotal();
classes_index = 0;
if (percentage_use >= 0.9) {
classes_index = 4;
} else if (percentage_use >= 0.75) {
classes_index = 3;
} else if (percentage_use >= 0.50) {
classes_index = 2;
} else if (percentage_use >= 0.25) {
classes_index = 1;
}
printf("{\"text\":\"%s\",\"class\":\"%s\",\"percentage\":%f}\n", mem_text,
classes[classes_index], percentage_use * 100);
fflush(stdout);
return 0;
}

View file

@ -1 +1 @@
rm /usr/local/bin/pss-total
rm /usr/local/bin/pss-total-waybar