fs_*: convert to generic object payload
authorPhil Sutter <phil@nwl.cc>
Sun, 4 Oct 2009 14:53:21 +0000 (16:53 +0200)
committerPhil Sutter <phil@nwl.cc>
Tue, 3 Nov 2009 22:23:22 +0000 (23:23 +0100)
src/conky.c
src/conky.h
src/core.c
src/fs.c
src/fs.h
src/text_object.h

index 7360a41..f68e7b0 100644 (file)
@@ -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;
index d60d461..1de76e5 100644 (file)
@@ -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;
index d9d7749..03c3507 100644 (file)
@@ -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)
index 4575c96..fefda8b 100644 (file)
--- 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);
+}
index d8e1bf8..0c93c8e 100644 (file)
--- 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);
index 4b38ed5..b685b4a 100644 (file)
@@ -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;