From d20b1b93f83944de1b59e529ca6898121b5f2939 Mon Sep 17 00:00:00 2001 From: Aaron Ball Date: Sun, 30 May 2021 15:29:07 -0600 Subject: Make bar widths configurable Previously, each module defined its own width for the bar (typically 12 wide), should the output be bar format. This introduces a config_node level configuration "width". This configuration is ignored unless display == bar in the config for the given module. This width configurations allows the width of each module bar to be configured by the user without requiring modification of source and recompiling of the app. --- src/config.c | 1 + src/config_bat.c | 2 +- src/config_cpu.c | 2 +- src/config_fs.c | 2 +- src/config_mem.c | 2 +- src/config_node.c | 3 +++ src/config_node.h | 1 + src/config_swap.c | 5 ++--- 8 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/config.c b/src/config.c index ab7bf0a..314b6d5 100644 --- a/src/config.c +++ b/src/config.c @@ -118,6 +118,7 @@ struct node* config_load(char* path) { || strcmp(key, "color") == 0 || strcmp(key, "label_color") == 0 || strcmp(key, "display") == 0 + || strcmp(key, "width") == 0 || strcmp(key, "interval") == 0) { _load_node_key(cur, key, val); continue; diff --git a/src/config_bat.c b/src/config_bat.c index 5052bbc..1b1e581 100644 --- a/src/config_bat.c +++ b/src/config_bat.c @@ -74,7 +74,7 @@ int config_bat_load(struct node* n) { } if(strcmp(n->display, "bar") == 0) { - print_bar(12, (float) level / 100, &n->text[strlen(n->text)]); + print_bar(n->width, (float) level / 100, &n->text[strlen(n->text)]); } else { sprintf(n->text, "%02d%%", (int)(level / 100)); } diff --git a/src/config_cpu.c b/src/config_cpu.c index 486af10..216c553 100644 --- a/src/config_cpu.c +++ b/src/config_cpu.c @@ -57,7 +57,7 @@ int config_cpu_load(struct node* n) { } if(strcmp(n->display, "bar") == 0) { - print_bar(12, (float) level, n->text); + print_bar(n->width, (float) level, n->text); } else { // Default to percent display sprintf(n->text, "%02d%%", (int)(level * 100)); diff --git a/src/config_fs.c b/src/config_fs.c index 1d87a70..fdaa85a 100644 --- a/src/config_fs.c +++ b/src/config_fs.c @@ -58,7 +58,7 @@ int config_fs_load(struct node* n) { } if(strcmp(n->display, "bar") == 0) { - print_bar(10, ((long double) used) / (long double)fs.f_blocks, n->text); + print_bar(n->width, ((long double) used) / (long double)fs.f_blocks, n->text); } else { sprintf(n->text, "%d%%", percent); } diff --git a/src/config_mem.c b/src/config_mem.c index 09dde81..c85e86f 100644 --- a/src/config_mem.c +++ b/src/config_mem.c @@ -57,7 +57,7 @@ int config_mem_load(struct node* n) { strcpy(n->label_color, C_LGREY); if(strcmp(n->display, "bar") == 0) { - print_bar(12, (double)used / total, n->text); + print_bar(n->width, (double)used / total, n->text); } else { sprintf(n->text, "%02d%%", percent); } diff --git a/src/config_node.c b/src/config_node.c index d4fc81d..412914f 100644 --- a/src/config_node.c +++ b/src/config_node.c @@ -27,6 +27,7 @@ struct node* node_new(void (*init_callback)(struct node*)) { out->label[0] = '\0'; out->interval = 0; out->lastrun = 0; + out->width = 12; strcpy(out->color, "#eeeeee"); strcpy(out->label_color, "#eeeeee"); init_callback(out); @@ -62,6 +63,8 @@ void _load_node_key(struct node* n, char* key, char* val) { strcpy(n->display, val); else if(strcmp(key, "interval") == 0) n->interval = strtol(val, NULL, 10); + else if(strcmp(key, "width") == 0) + n->width = strtol(val, NULL, 10); else printf("ERROR: Unknown node key %s\n", key); } diff --git a/src/config_node.h b/src/config_node.h index b4638b5..739ea41 100644 --- a/src/config_node.h +++ b/src/config_node.h @@ -34,6 +34,7 @@ struct node { char label[256]; // Label for the node char text[256]; // Value for full_text char display[64]; // How to display the data (eg: bar, percent, etc) + long width; // Width of the element (only used when display == bar) size_t interval; // How frequently the load function should be called time_t lastrun; // Last time the node's load function was called }; diff --git a/src/config_swap.c b/src/config_swap.c index 9568591..c8469d5 100644 --- a/src/config_swap.c +++ b/src/config_swap.c @@ -22,6 +22,7 @@ void config_swap_init(struct node* n) { n->type = CTYPE_SWAP; n->loadfunc = &config_swap_load; n->loadkey = &load_swap_key; + n->width = 12; } void load_swap_key(struct node* n, char* key, char* val) { @@ -51,12 +52,10 @@ int config_swap_load(struct node* n) { strcpy(n->label_color, C_LGREY); if(strcmp(n->display, "bar") == 0) { - print_bar(12, (double)(total - free) / total, n->text); + print_bar(n->width, (double)(total - free) / total, n->text); } else { sprintf(n->text, "%02d%%", percent); } return 0; } - - -- cgit v1.2.3