1 /**
2 * I3cstatus prints a configurable status bar for the i3 window manager
3 * Copyright (C) 2021 Aaron Ball <nullspoon@oper.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "config_fs.h"
19
20 void config_fs_init(struct node* n) {
21 n->data = malloc(sizeof(struct config_fs));
22 n->type = CTYPE_FS;
23 n->loadfunc = &config_fs_load;
24 n->loadkey = &load_fs_key;
25 }
26
27 void load_fs_key(struct node* n, char* key, char* val) {
28 struct config_fs* data = (struct config_fs*) n->data;
29
30 if(strcmp(key, "path") == 0) {
31 strcpy(data->path, val);
32 // Provide a default label using the path if one not already specified
33 if(n->label[0] == '\0')
34 sprintf(n->label, "%s", val);
35 } else {
36 printf("ERROR: Unknown fs key %s\n", key);
37 }
38 }
39
40 int config_fs_load(struct node* n) {
41 struct statfs fs;
42 int percent;
43 unsigned long used;
44
45 statfs(((struct config_fs*)n->data)->path, &fs);
46
47 used = (unsigned long) fs.f_blocks - (unsigned long) fs.f_bfree;
48 percent = ((long double) used * 100) / (long double)fs.f_blocks;
49
50 if(percent > 80) {
51 strcpy(n->color, C_RED);
52 } else if(percent > 50) {
53 strcpy(n->color, C_ORANGE);
54 } else if(percent > 30) {
55 strcpy(n->color, C_YELLOW);
56 } else {
57 strcpy(n->color, C_GREEN);
58 }
59
60 if(strcmp(n->display, "bar") == 0) {
61 print_bar(n->width, ((long double) used) / (long double)fs.f_blocks, n->text);
62 } else {
63 sprintf(n->text, "%d%%", percent);
64 }
65 return 0;
66 }
|