aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-11-18 23:19:32 +0100
committerFederico Angelilli <code@fedang.net>2024-11-18 23:19:32 +0100
commit38637de243c569c0d6aaff455752e617c30c5866 (patch)
tree079da5bf606b4fa6f5be2c4afff1b09af4dc79e6
parentad0577c0a50013b5cb8105380cba1be06f50df50 (diff)
Fix format and add it to ram
-rw-r--r--src/blocks/ram.c111
-rw-r--r--src/format.c7
2 files changed, 77 insertions, 41 deletions
diff --git a/src/blocks/ram.c b/src/blocks/ram.c
index da56baa..2a2f24a 100644
--- a/src/blocks/ram.c
+++ b/src/blocks/ram.c
@@ -4,14 +4,32 @@
#include <stdlib.h>
#include "../block.h"
-
+#include "../format.h"
#include "../any_log.h"
typedef struct {
block_text_t block;
- char *format;
+ format_t format;
} block_ram_t;
+typedef enum {
+ RAM_STRING = 0,
+ RAM_TOTAL,
+ RAM_FREE,
+ RAM_USED,
+ RAM_FREE_PERC,
+ RAM_USED_PERC,
+} block_ram_mark_t;
+
+static const format_pair_t block_ram_pairs[] = {
+ { "total", RAM_TOTAL },
+ { "free", RAM_FREE },
+ { "used", RAM_USED },
+ { "free-percentage", RAM_FREE_PERC},
+ { "used-percentage", RAM_USED_PERC},
+ { NULL },
+};
+
static const struct timespec block_ram_interval = {
.tv_sec = 1,
.tv_nsec = 0,
@@ -47,35 +65,46 @@ static void block_ram_update(block_t *block)
return;
}
- static const char *ram_formats[] = {
- "total",
- "free",
- "used",
- "free-percentage",
- "used-percentage",
- NULL,
- };
-
- char buffer[32][5] = { 0 };
- snprintf(buffer[0], 32, "%ld", total);
- snprintf(buffer[1], 32, "%ld", available);
- snprintf(buffer[2], 32, "%ld", total - available);
- snprintf(buffer[3], 32, "%ld", 100 * available / total);
- snprintf(buffer[4], 32, "%ld", 100 * (total - available) / total);
-
- const char *ram_values[] = {
- buffer[0],
- buffer[1],
- buffer[2],
- buffer[3],
- buffer[4],
- NULL,
- };
-
block_ram_t *ram = (block_ram_t *)block;
- free(ram->block.text);
- ram->block.text = strformat(ram->format, '%', ram_formats, ram_values);
+ char buffer[256] = { 0 };
+ size_t start = 0, size = sizeof(buffer);
+
+ for (size_t i = 0; i < ram->format.length; i++) {
+ size_t rest = start >= size ? 0 : size - start;
+ if (rest == 0) break;
+
+ switch (ram->format.marks[i]) {
+ case RAM_STRING:
+ start += snprintf(buffer + start, rest, "%s", ram->format.parts[i]);
+ break;
+ case RAM_TOTAL:
+ start += snprintf(buffer + start, rest, "%ld", total);
+ break;
+
+ case RAM_FREE:
+ start += snprintf(buffer + start, rest, "%ld", available);
+ break;
+
+ case RAM_USED:
+ start += snprintf(buffer + start, rest, "%ld", total - available);
+ break;
+
+ case RAM_FREE_PERC:
+ start += snprintf(buffer + start, rest, "%ld", 100 * available / total);
+ break;
+
+ case RAM_USED_PERC:
+ start += snprintf(buffer + start, rest, "%ld", 100 * (total - available) / total);
+ break;
+
+ default:
+ unreachable();
+ }
+ }
+
+ free(ram->block.text);
+ ram->block.text = strcopy(buffer);
assert(ram->block.text != NULL);
}
@@ -91,7 +120,7 @@ static block_t *block_ram_alloc(const block_scheme_t *scheme)
static void block_ram_clean(block_t *block)
{
block_ram_t *ram = (block_ram_t *)block;
- free(ram->format);
+ format_free(&ram->format);
free(ram->block.text);
}
@@ -104,16 +133,22 @@ static bool block_ram_validate(block_t *block, const block_scheme_t *scheme)
return false;
}
- if (strstr(ram->block.text, "%{") == NULL) {
- log_warn("Block '%s' does not use any ram variable", block->label);
-
- block->update_fn = NULL;
- log_debug("Disabled updates for block '%s'", block->label);
- return true;
+ if (!format_init(&ram->format, ram->block.text, '%')) {
+ log_error("Block '%s' has an invalid format", block->label);
+ return false;
}
- ram->format = ram->block.text;
- ram->block.text = strcopy("?");
+ if (!format_remark(&ram->format, block->label, block_ram_pairs))
+ return false;
+
+ //if (ram->format.length < 2) {
+ // log_warn("Block '%s' does not use any ram variable", block->label);
+
+ // block->update_fn = NULL;
+ // log_debug("Disabled updates for block '%s'", block->label);
+ // return true;
+ //}
+
return true;
}
diff --git a/src/format.c b/src/format.c
index 47426ec..4657359 100644
--- a/src/format.c
+++ b/src/format.c
@@ -31,8 +31,9 @@ bool format_init(format_t *format, const char *string, char delim)
// TODO: Escape formatting
if (string[end + 1] == '{') {
+ format_grow();
+
if (start != end) {
- format_grow();
parts[n] = strslice(string, start, end);
marks[n] = false;
n++;
@@ -70,7 +71,7 @@ bool format_init(format_t *format, const char *string, char delim)
next:
}
- if (string[end] != '\0') {
+ if (start != end || string[end] != '\0') {
while (string[end] != '\0') end++;
format_grow();
@@ -108,7 +109,7 @@ bool format_remark(format_t *format, const char *label, const format_pair_t *pai
errors++;
if (label != NULL)
- log_error("Unknown format option '%s' for %s", format->parts[i], label);
+ log_error("Unknown format option '%s' for block '%s'", format->parts[i], label);
next:
}