1 /**
2 * I3cstatus prints a configurable status bar for the i3 window manager
3 * Copyright (C) 2020 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 else
33 printf("ERROR: Unknown fs key %s\n", key);
34 }
35
36 int config_fs_load(struct node* n) {
37 struct statfs fs;
38 int percent;
39 unsigned long used;
40
41 statfs(((struct config_fs*)n->data)->path, &fs);
42
43 used = (unsigned long) fs.f_blocks - (unsigned long) fs.f_bfree;
44 percent = ((long double) used * 100) / (long double)fs.f_blocks;
45
46 if(percent > 80) {
47 strcpy(n->color, C_RED);
48 } else if(percent > 50) {
49 strcpy(n->color, C_ORANGE);
50 } else if(percent > 30) {
51 strcpy(n->color, C_YELLOW);
52 } else {
53 strcpy(n->color, C_GREEN);
54 }
55
56 if(strcmp(n->display, "bar") == 0) {
57 print_bar(10, ((long double) used) / (long double)fs.f_blocks, n->text);
58 } else {
59 sprintf(n->text, "%d%%", percent);
60 }
61 sprintf(n->label, "%s:", ((struct config_fs*)n->data)->path);
62 return 0;
63 }
|