diff options
Diffstat (limited to 'src/config_mem.c')
-rw-r--r-- | src/config_mem.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/config_mem.c b/src/config_mem.c new file mode 100644 index 0000000..699e22e --- /dev/null +++ b/src/config_mem.c @@ -0,0 +1,68 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "config_mem.h" + +void config_mem_init(struct node* n) { + n->data = NULL; + n->type = CTYPE_MEM; + n->loadfunc = &config_mem_load; + n->loadkey = &load_mem_key; +} + +void load_mem_key(struct node* n, char* key, char* val) { +} + +int config_mem_load(struct node* n) { + long total; + long free; + long available; + + int percent; + long used; + + // See https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773 + FILE* fd = fopen("/proc/meminfo", "r"); + fscanf(fd, "MemTotal: %ld kB ", &total); + fscanf(fd, "MemFree: %ld kB ", &free); + fscanf(fd, "MemAvailable: %ld kB ", &available); + fclose(fd); + + used = total - available; + percent = used * 100 / total; + + if(percent < 20) { + strcpy(n->color, C_GREEN); + } else if(percent < 40) { + strcpy(n->color, C_YELLOW); + } else if(percent < 60) { + strcpy(n->color, C_ORANGE); + } else { + strcpy(n->color, C_RED); + } + strcpy(n->label_color, C_LGREY); + + if(strcmp(n->display, "bar") == 0) { + print_bar(12, (double)used / total, n->text); + } else { + sprintf(n->text, "%02d%%", percent); + } + + return 0; +} + + |