aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--comet.conf7
-rw-r--r--src/blocks/fs.c100
-rw-r--r--src/blocks/ram.c3
-rw-r--r--src/blocks/scheme.c2
-rw-r--r--src/config.c6
5 files changed, 114 insertions, 4 deletions
diff --git a/comet.conf b/comet.conf
index 130c2d2..73163dc 100644
--- a/comet.conf
+++ b/comet.conf
@@ -21,6 +21,13 @@
text-color = #fff
color = #f0b
+[block.disk]
+ type = fs
+ text = SSD %{used} / %{total}
+ color = #18baf2
+ text-color = #fff
+ path = /
+
;[block.main]
; type = 0
diff --git a/src/blocks/fs.c b/src/blocks/fs.c
new file mode 100644
index 0000000..a47eb9c
--- /dev/null
+++ b/src/blocks/fs.c
@@ -0,0 +1,100 @@
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+#include <stdlib.h>
+
+#include <sys/statvfs.h>
+#include <sys/stat.h>
+
+#include "scheme.h"
+
+#include "../any_log.h"
+
+static void block_fs_update(block_t *block)
+{
+ const char *path = ((char **)block->state)[0];
+ const char *text = ((char **)block->state)[1];
+
+ struct statvfs sbuf;
+ assert(statvfs(path, &sbuf) == 0);
+
+ static const char *fs_formats[] = {
+ "total",
+ "free",
+ "used",
+ NULL,
+ };
+
+ char buffer[32][3] = { 0 };
+ snprintf(buffer[0], 32, "%ld", sbuf.f_blocks * sbuf.f_frsize);
+ snprintf(buffer[1], 32, "%ld", sbuf.f_bavail * sbuf.f_bsize);
+ snprintf(buffer[2], 32, "%ld", (sbuf.f_blocks - sbuf.f_bavail) * sbuf.f_bsize);
+
+ const char *fs_values[] = {
+ buffer[0],
+ buffer[1],
+ buffer[2],
+ NULL,
+ };
+
+ free(block->text.text);
+ block->text.text = strformat(text, '%', fs_formats, fs_values);
+ assert(block->text.text != NULL);
+}
+
+static void block_fs_finalize(block_t *block)
+{
+ free(block->state);
+}
+
+static bool block_fs_validate(block_t *block, const block_scheme_t *scheme)
+{
+ if (block->text.text == NULL) {
+ log_error("Block '%s' requires key '%s'", block->label, "text");
+ return false;
+ }
+
+ if (strstr(block->text.text, "%{") == NULL) {
+ log_warn("Block '%s' does not use any fs variable", block->label);
+
+ block->update_cb = NULL;
+ log_debug("Disabled updates for block '%s'", block->label);
+ return true;
+ }
+
+ const char *path = ((char **)block->state)[0];
+ struct stat sbuf;
+
+ if (path == NULL || stat(path, &sbuf) < 0) {
+ log_error("Block '%s' was given an invalid path '%s'", block->label, path);
+ return false;
+ }
+
+ ((char **)block->state)[1] = block->text.text;
+ block->text.text = NULL;
+
+ return true;
+}
+
+static const config_entry_t block_fs_entries[] = {
+ { "path", CONFIG_STRING, NULL, 0 },
+ { 0 },
+};
+
+const block_scheme_t block_fs_scheme = {
+ .name = "fs",
+ .block = {
+ .type = BLOCK_TEXT,
+ .update_interval = {
+ .tv_sec = 20,
+ .tv_nsec = 0,
+ },
+ .update_cb = block_fs_update,
+ .finalize_cb = block_fs_finalize,
+ },
+ .size = 2 * sizeof(char *),
+ .entries = block_fs_entries,
+ .validate = block_fs_validate,
+};
+
+
diff --git a/src/blocks/ram.c b/src/blocks/ram.c
index 780ff9e..c6ed727 100644
--- a/src/blocks/ram.c
+++ b/src/blocks/ram.c
@@ -22,7 +22,6 @@ static void block_ram_update(block_t *block)
&total, &unused, &available, &buffers, &cached));
fclose(meminfo);
-
static const char *ram_formats[] = {
"total",
"free",
@@ -48,7 +47,7 @@ static void block_ram_update(block_t *block)
NULL,
};
-
+ free(block->text.text);
block->text.text = strformat(block->state, '%', ram_formats, ram_values);
assert(block->text.text != NULL);
}
diff --git a/src/blocks/scheme.c b/src/blocks/scheme.c
index c474de3..a17ef3a 100644
--- a/src/blocks/scheme.c
+++ b/src/blocks/scheme.c
@@ -3,10 +3,12 @@
extern const block_scheme_t block_text_scheme;
extern const block_scheme_t block_group_scheme;
extern const block_scheme_t block_ram_scheme;
+extern const block_scheme_t block_fs_scheme;
const block_scheme_t *block_schemes[] = {
&block_text_scheme,
&block_group_scheme,
&block_ram_scheme,
+ &block_fs_scheme,
NULL,
};
diff --git a/src/config.c b/src/config.c
index cde4af1..81932a0 100644
--- a/src/config.c
+++ b/src/config.c
@@ -301,7 +301,7 @@ static config_status_t config_read_block(const block_scheme_t *scheme, block_t *
status = config_read_entry(block->type == BLOCK_GROUP ? block_group_entries : block_text_entries,
block, type, section, key, value);
- return status != CONFIG_UNKNOWN || scheme->entries != NULL
+ return status != CONFIG_UNKNOWN || scheme->entries == NULL
? status
: config_read_entry(scheme->entries, block->state, type, section, key, value);
}
@@ -379,8 +379,10 @@ void config_read(config_t *config, FILE *file)
free(block->label);
block->label = label;
- if (scheme->size != 0)
+ if (scheme->size != 0) {
block->state = malloc(scheme->size);
+ memset(block->state, 0, scheme->size);
+ }
goto skip_pair;
}