From 4d70cf07b82d257d3852d76597e6dc9bc80192f7 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Sun, 4 Oct 2009 15:50:47 +0200 Subject: [PATCH] fs_bar, fs_bar_free: merge init and print routines --- src/conky.c | 58 ++--------------------------------------------------- src/core.c | 29 ++------------------------- src/fs.c | 46 ++++++++++++++++++++++++++++++++++++++++++ src/fs.h | 5 +++++ 4 files changed, 55 insertions(+), 83 deletions(-) diff --git a/src/conky.c b/src/conky.c index 812d191a..5469fa88 100644 --- a/src/conky.c +++ b/src/conky.c @@ -1744,34 +1744,7 @@ static void generate_text_internal(char *p, int p_max_size, } } OBJ(fs_bar) { - if (obj->data.fs != NULL) { - if (obj->data.fs->size == 0) { -#ifdef X11 - if(output_methods & TO_X) { - new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255); - }else{ -#endif /* X11 */ - if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X; - new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w); -#ifdef X11 - } -#endif /* X11 */ - } else { -#ifdef X11 - if(output_methods & TO_X) { - new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, - (int) (255 - obj->data.fsbar.fs->avail * 255 / - obj->data.fs->size)); - }else{ -#endif /* X11 */ - if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X; - new_bar_in_shell(p, p_max_size, - (int) (100 - obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w); -#ifdef X11 - } -#endif /* X11 */ - } - } + print_fs_bar(obj, 0, p, p_max_size); } OBJ(fs_free) { if (obj->data.fs != NULL) { @@ -1805,34 +1778,7 @@ static void generate_text_internal(char *p, int p_max_size, } } OBJ(fs_bar_free) { - if (obj->data.fs != NULL) { - if (obj->data.fs->size == 0) { -#ifdef X11 - if(output_methods & TO_X) { - new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255); - }else{ -#endif /* X11 */ - if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X; - new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w); -#ifdef X11 - } -#endif /* X11 */ - } else { -#ifdef X11 - if(output_methods & TO_X) { - new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, - (int) (obj->data.fsbar.fs->avail * 255 / - obj->data.fs->size)); - }else{ -#endif /* X11 */ - if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X; - new_bar_in_shell(p, p_max_size, - (int) (obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w); -#ifdef X11 - } -#endif /* X11 */ - } - } + print_fs_bar(obj, 1, p, p_max_size); } OBJ(fs_used_perc) { if (obj->data.fs != NULL) { diff --git a/src/core.c b/src/core.c index bbda97e1..7eb84576 100644 --- a/src/core.c +++ b/src/core.c @@ -713,34 +713,9 @@ struct text_object *construct_text_object(const char *s, const char *arg, long obj->data.s = strndup("", text_buffer_size); } END OBJ(fs_bar, &update_fs_stats) - SIZE_DEFAULTS(bar); - arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h); - if (arg) { - while (isspace(*arg)) { - arg++; - } - if (*arg == '\0') { - arg = "/"; - } - } else { - arg = "/"; - } - obj->data.fsbar.fs = prepare_fs_stat(arg); + init_fs_bar(obj, arg); END OBJ(fs_bar_free, &update_fs_stats) - SIZE_DEFAULTS(bar); - arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h); - if (arg) { - while (isspace(*arg)) { - arg++; - } - if (*arg == '\0') { - arg = "/"; - } - } else { - arg = "/"; - } - - obj->data.fsbar.fs = prepare_fs_stat(arg); + init_fs_bar(obj, arg); END OBJ(fs_free, &update_fs_stats) if (!arg) { arg = "/"; diff --git a/src/fs.c b/src/fs.c index 8766a12e..c330f612 100644 --- a/src/fs.c +++ b/src/fs.c @@ -31,6 +31,9 @@ #include "conky.h" #include "logging.h" #include "fs.h" +#include "specials.h" +#include "text_object.h" +#include #include #include #include @@ -191,3 +194,46 @@ void get_fs_type(const char *path, char *result) strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE); } + +void init_fs_bar(struct text_object *obj, const char *arg) +{ + SIZE_DEFAULTS(bar); + arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h); + if (arg) { + while (isspace(*arg)) { + arg++; + } + if (*arg == '\0') { + arg = "/"; + } + } else { + arg = "/"; + } + obj->data.fsbar.fs = prepare_fs_stat(arg); +} + +void print_fs_bar(struct text_object *obj, int be_free_bar, char *p, int p_max_size) +{ + double val; + + if (!obj->data.fsbar.fs) + return; + + if (!obj->data.fsbar.fs->size) + val = 1.0; + else + val = (double)obj->data.fsbar.fs->avail / (double)obj->data.fsbar.fs->size; + + if (!be_free_bar) + val = 1.0 - val; + +#ifdef X11 + if(output_methods & TO_X) { + new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, (int)(255 * val)); + }else +#endif /* X11 */ + { + if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X; + new_bar_in_shell(p, p_max_size, (int)(100 * val), obj->data.fsbar.w); + } +} diff --git a/src/fs.h b/src/fs.h index 26bf38ba..d8e1bf8e 100644 --- a/src/fs.h +++ b/src/fs.h @@ -15,6 +15,11 @@ struct fs_stat { char set; }; +/* forward declare to make gcc happy (fs.h <-> text_object.h include) */ +struct text_object; + +void init_fs_bar(struct text_object *, const char *); +void print_fs_bar(struct text_object *, int, char *, int); void update_fs_stats(void); struct fs_stat *prepare_fs_stat(const char *path); void clear_fs_stats(void);