aboutsummaryrefslogtreecommitdiff
path: root/src/blocks/ram.c
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-09-08 17:55:18 +0200
committerFederico Angelilli <code@fedang.net>2024-09-08 17:55:18 +0200
commit4d1abb6d350a3d898a5aedfcb912bc28eef46d45 (patch)
treeb178627ed5e80d22171c0dbf7edd39d01eb502ba /src/blocks/ram.c
parent5e7f66b34826697537bcdcb60c81f56da956a32b (diff)
Start working on custom formatting
Diffstat (limited to 'src/blocks/ram.c')
-rw-r--r--src/blocks/ram.c88
1 files changed, 29 insertions, 59 deletions
diff --git a/src/blocks/ram.c b/src/blocks/ram.c
index 44dd1a3..4400a59 100644
--- a/src/blocks/ram.c
+++ b/src/blocks/ram.c
@@ -7,75 +7,39 @@
#include "../any_log.h"
-char *str_replace(char *orig, char *rep, char *with) {
- char *result; // the return string
- char *ins; // the next insert point
- char *tmp; // varies
- int len_rep; // length of rep (the string to remove)
- int len_with; // length of with (the string to replace rep with)
- int len_front; // distance between rep and end of last rep
- int count; // number of replacements
-
- // sanity checks and initialization
- if (!orig || !rep)
- return NULL;
- len_rep = strlen(rep);
- if (len_rep == 0)
- return NULL; // empty rep causes infinite loop during count
- if (!with)
- with = "";
- len_with = strlen(with);
-
- // count the number of replacements needed
- ins = orig;
- for (count = 0; (tmp = strstr(ins, rep)); ++count) {
- ins = tmp + len_rep;
- }
-
- tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);
-
- if (!result)
- return NULL;
-
- // first time through the loop, all the variable are set correctly
- // from here on,
- // tmp points to the end of the result string
- // ins points to the next occurrence of rep in orig
- // orig points to the remainder of orig after "end of rep"
- while (count--) {
- ins = strstr(orig, rep);
- len_front = ins - orig;
- tmp = strncpy(tmp, orig, len_front) + len_front;
- tmp = strcpy(tmp, with) + len_with;
- orig += len_front + len_rep; // move to next "end of rep"
- }
- strcpy(tmp, orig);
- return result;
-}
+static const char *ram_formats[] = {
+ "%{total}",
+ "%{free}",
+ "%{used}",
+ "%{free-percentage}",
+ "%{used-percentage}",
+ NULL,
+};
static void block_ram_update(block_t *block)
{
FILE *meminfo = fopen("/proc/meminfo", "rb");
assert(meminfo != NULL);
- uintmax_t total, unused, buffers, cached;
+ uintmax_t total, unused, available, buffers, cached;
assert(5 == fscanf(meminfo,
"MemTotal: %ju kB\n"
"MemFree: %ju kB\n"
"MemAvailable: %ju kB\n"
"Buffers: %ju kB\n"
"Cached: %ju kB\n",
- &total, &unused, &buffers, &buffers, &cached));
-
- int usage = 100 * (total - unused - buffers - cached) / total;
+ &total, &unused, &available, &buffers, &cached));
fclose(meminfo);
- free(block->text.text);
+ char ram_values[6][32] = { 0 };
- char str[100];
- sprintf(str, "%d", usage);
+ snprintf(ram_values[0], 32, "%ld", total);
+ snprintf(ram_values[1], 32, "%ld", available);
+ snprintf(ram_values[2], 32, "%ld", total - available);
+ snprintf(ram_values[3], 32, "%ld", 100 * available / total);
+ snprintf(ram_values[4], 32, "%ld", 100 * (total - available) / total);
- block->text.text = str_replace(block->state, "%{ram}", str);
+ block->text.text = strformat(block->state, ram_formats, (const char **)ram_values);
assert(block->text.text != NULL);
}
@@ -86,16 +50,22 @@ static void block_ram_finalize(block_t *block)
static bool block_ram_validate(block_t *block, const block_scheme_t *scheme)
{
- block->state = block->text.text;
- block->text.text = NULL;
-
- if (block->state == NULL) {
+ if (block->text.text == NULL) {
log_error("Block '%s' requires key '%s'", block->label, "text");
return false;
}
- if (strstr(block->state, "%{ram}") != NULL)
- log_warn("Block '%s' does not have a ram format", block->label);
+ block->state = block->text.text;
+ block->text.text = NULL;
+
+ size_t count = strcount(block->state, ram_formats);
+ if (count == 0) {
+ log_warn("Block '%s' does not use any ram variable", block->label);
+
+ block->update_cb = NULL;
+ log_debug("Disabled updates for block '%s'", block->label);
+ return true;
+ }
return true;
}