diff options
| author | Federico Angelilli <code@fedang.net> | 2024-09-08 17:55:18 +0200 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2024-09-08 17:55:18 +0200 |
| commit | 4d1abb6d350a3d898a5aedfcb912bc28eef46d45 (patch) | |
| tree | b178627ed5e80d22171c0dbf7edd39d01eb502ba | |
| parent | 5e7f66b34826697537bcdcb60c81f56da956a32b (diff) | |
Start working on custom formatting
| -rw-r--r-- | comet.conf | 2 | ||||
| -rw-r--r-- | src/blocks/ram.c | 88 | ||||
| -rw-r--r-- | src/util.c | 42 | ||||
| -rw-r--r-- | src/util.h | 10 |
4 files changed, 79 insertions, 63 deletions
@@ -17,7 +17,7 @@ [block.ram] type = ram - text = RAM: %{ram} + text = RAM: %{used-percentage} % text-color = #fff color = #f0b 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; } @@ -108,7 +108,45 @@ char *strcopy(const char *string) return strslice(string, 0, strlen(string)); } -void unreachable(void) +bool strfind(const char *string, const char *cases[]) { - log_panic("Unreachable"); + for (int i = 0; cases[i] != NULL; i++) { + if (strstr(string, cases[i]) != NULL) + return true; + } + return false; +} + +size_t strprefix(const char *string, const char *prefix) +{ + size_t i = 0; + for ( ; prefix[i] != '\0'; i++) { + if (string[i] != prefix[i]) + return 0; + } + return i; +} + +size_t strcount(const char *string, const char *subs[]) +{ + size_t count = 0; + for (int i = 0; string[i] != '\0'; ) { +next: + for (int j = 0; subs[j] != NULL; j++) { + size_t pl = strprefix(string + i, subs[j]); + if (pl) { + i += pl; + count++; + goto next; + } + } + ++i; + } + return count; +} + +char *strformat(const char *string, const char *keys[], const char *values[]) +{ + // TODO + return NULL; } @@ -6,6 +6,8 @@ #include <stdbool.h> #include <stdio.h> +#define unreachable() log_panic("Unreachable code"); + typedef struct { char *key; char *value; @@ -55,6 +57,12 @@ char *strslice(const char *string, size_t start, size_t end); char *strcopy(const char *string); -void unreachable(void); +bool strfind(const char *string, const char *cases[]); + +size_t strprefix(const char *string, const char *prefix); + +size_t strcount(const char *string, const char *subs[]); + +char *strformat(const char *string, const char *keys[], const char *values[]); #endif |
