From 8d1640f30ba35c5687d11347834620fbd3f8cbb6 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Sun, 4 Oct 2009 16:53:21 +0200 Subject: [PATCH] fs_*: convert to generic object payload --- src/conky.c | 42 ++++++++---------------------------------- src/conky.h | 3 +++ src/core.c | 30 ++++++------------------------ src/fs.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/fs.h | 8 ++++++++ src/text_object.h | 1 - 6 files changed, 67 insertions(+), 59 deletions(-) diff --git a/src/conky.c b/src/conky.c index 7360a41a..f68e7b02 100644 --- a/src/conky.c +++ b/src/conky.c @@ -563,7 +563,7 @@ int spaced_print(char *buf, int size, const char *format, int width, ...) * * - i.e., unsigned values between 0 and 100 * - respect the value of pad_percents */ -static int percent_print(char *buf, int size, unsigned value) +int percent_print(char *buf, int size, unsigned value) { return spaced_print(buf, size, "%u", pad_percents, value); } @@ -573,7 +573,7 @@ static int percent_print(char *buf, int size, unsigned value) * The algorithm always divides by 1024, as unit-conversion of byte * counts suggests. But for output length determination we need to * compare with 1000 here, as we print in decimal form. */ -static void human_readable(long long num, char *buf, int size) +void human_readable(long long num, char *buf, int size) { const char **suffix = suffixes; float fnum; @@ -1308,51 +1308,25 @@ void generate_text_internal(char *p, int p_max_size, print_fs_bar(obj, 0, p, p_max_size); } OBJ(fs_free) { - if (obj->data.fs != NULL) { - human_readable(obj->data.fs->avail, p, 255); - } + print_fs_free(obj, p, p_max_size); } OBJ(fs_free_perc) { - if (obj->data.fs != NULL) { - int val = 0; - - if (obj->data.fs->size) { - val = obj->data.fs->avail * 100 / obj->data.fs->size; - } - - percent_print(p, p_max_size, val); - } + print_fs_perc(obj, 1, p, p_max_size); } OBJ(fs_size) { - if (obj->data.fs != NULL) { - human_readable(obj->data.fs->size, p, 255); - } + print_fs_size(obj, p, p_max_size); } OBJ(fs_type) { - if (obj->data.fs != NULL) - snprintf(p, p_max_size, "%s", obj->data.fs->type); + print_fs_type(obj, p, p_max_size); } OBJ(fs_used) { - if (obj->data.fs != NULL) { - human_readable(obj->data.fs->size - obj->data.fs->free, p, - 255); - } + print_fs_used(obj, p, p_max_size); } OBJ(fs_bar_free) { print_fs_bar(obj, 1, p, p_max_size); } OBJ(fs_used_perc) { - if (obj->data.fs != NULL) { - int val = 0; - - if (obj->data.fs->size) { - val = obj->data.fs->free - * 100 / - obj->data.fs->size; - } - - percent_print(p, p_max_size, 100 - val); - } + print_fs_perc(obj, 0, p, p_max_size); } OBJ(loadavg) { float *v = info.loadavg; diff --git a/src/conky.h b/src/conky.h index d60d4616..1de76e5e 100644 --- a/src/conky.h +++ b/src/conky.h @@ -177,6 +177,9 @@ long get_current_text_color(void); void set_updatereset(int); int get_updatereset(void); +int percent_print(char *, int, unsigned); +void human_readable(long long, char *, int); + struct conftree { char* string; struct conftree* horz_next; diff --git a/src/core.c b/src/core.c index d9d77491..03c3507e 100644 --- a/src/core.c +++ b/src/core.c @@ -552,35 +552,17 @@ struct text_object *construct_text_object(const char *s, const char *arg, long END OBJ(fs_bar_free, &update_fs_stats) init_fs_bar(obj, arg); END OBJ(fs_free, &update_fs_stats) - if (!arg) { - arg = "/"; - } - obj->data.fs = prepare_fs_stat(arg); + init_fs(obj, arg); END OBJ(fs_used_perc, &update_fs_stats) - if (!arg) { - arg = "/"; - } - obj->data.fs = prepare_fs_stat(arg); + init_fs(obj, arg); END OBJ(fs_free_perc, &update_fs_stats) - if (!arg) { - arg = "/"; - } - obj->data.fs = prepare_fs_stat(arg); + init_fs(obj, arg); END OBJ(fs_size, &update_fs_stats) - if (!arg) { - arg = "/"; - } - obj->data.fs = prepare_fs_stat(arg); + init_fs(obj, arg); END OBJ(fs_type, &update_fs_stats) - if (!arg) { - arg = "/"; - } - obj->data.fs = prepare_fs_stat(arg); + init_fs(obj, arg); END OBJ(fs_used, &update_fs_stats) - if (!arg) { - arg = "/"; - } - obj->data.fs = prepare_fs_stat(arg); + init_fs(obj, arg); END OBJ(hr, 0) obj->data.i = arg ? atoi(arg) : 1; END OBJ(nameserver, &update_dns_data) diff --git a/src/fs.c b/src/fs.c index 4575c96a..fefda8b6 100644 --- a/src/fs.c +++ b/src/fs.c @@ -249,3 +249,45 @@ void print_fs_bar(struct text_object *obj, int be_free_bar, char *p, int p_max_s new_bar_in_shell(p, p_max_size, (int)(100 * val), fb->w); } } + +void init_fs(struct text_object *obj, const char *arg) +{ + obj->data.opaque = prepare_fs_stat(arg ? arg : "/"); +} + +void print_fs_perc(struct text_object *obj, int be_free, char *p, int p_max_size) +{ + struct fs_stat *fs = obj->data.opaque; + int val = 100; + + if (!fs) + return; + + if (fs->size) + val = fs->avail * 100 / fs->size; + + if (!be_free) + val = 100 - val; + + percent_print(p, p_max_size, val); +} + +#define HUMAN_PRINT_FS_GENERATOR(name, expr) \ +void print_fs_##name(struct text_object *obj, char *p, int p_max_size) \ +{ \ + struct fs_stat *fs = obj->data.opaque; \ + if (fs) \ + human_readable(expr, p, p_max_size); \ +} + +HUMAN_PRINT_FS_GENERATOR(free, fs->avail) +HUMAN_PRINT_FS_GENERATOR(size, fs->size) +HUMAN_PRINT_FS_GENERATOR(used, fs->size - fs->free) + +void print_fs_type(struct text_object *obj, char *p, int p_max_size) +{ + struct fs_stat *fs = obj->data.opaque; + + if (fs) + snprintf(p, p_max_size, "%s", fs->type); +} diff --git a/src/fs.h b/src/fs.h index d8e1bf8e..0c93c8ec 100644 --- a/src/fs.h +++ b/src/fs.h @@ -20,6 +20,14 @@ struct text_object; void init_fs_bar(struct text_object *, const char *); void print_fs_bar(struct text_object *, int, char *, int); + +void init_fs(struct text_object *, const char *); +void print_fs_perc(struct text_object *, int, char *, int); +void print_fs_free(struct text_object *, char *, int); +void print_fs_size(struct text_object *, char *, int); +void print_fs_used(struct text_object *, char *, int); +void print_fs_type(struct text_object *, char *, int); + void update_fs_stats(void); struct fs_stat *prepare_fs_stat(const char *path); void clear_fs_stats(void); diff --git a/src/text_object.h b/src/text_object.h index 4b38ed5d..b685b4ab 100644 --- a/src/text_object.h +++ b/src/text_object.h @@ -444,7 +444,6 @@ struct text_object { long l; /* some other integer */ unsigned int sensor; struct net_stat *net; - struct fs_stat *fs; struct diskio_stat *diskio; unsigned char loadavg[3]; unsigned int cpu_index;