Make $fs_used_perc and $fs_bar report used space correctly
[monky] / src / fs.c
index 92552e9..2152552 100644 (file)
--- a/src/fs.c
+++ b/src/fs.c
@@ -10,7 +10,7 @@
  * Please see COPYING for details
  *
  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
- * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
+ * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
  *     (see AUTHORS)
  * All rights reserved.
  *
@@ -65,13 +65,13 @@ static void update_fs_stat(struct fs_stat *fs);
 
 void get_fs_type(const char *path, char *result);
 
-void update_fs_stats(void)
+int update_fs_stats(void)
 {
        unsigned i;
        static double last_fs_update = 0.0;
 
        if (current_update_time - last_fs_update < 13)
-               return;
+               return 0;
 
        for (i = 0; i < MAX_FS_STATS; ++i) {
                if (fs_stats[i].set) {
@@ -79,6 +79,7 @@ void update_fs_stats(void)
                }
        }
        last_fs_update = current_update_time;
+       return 0;
 }
 
 void clear_fs_stats(void)
@@ -211,26 +212,24 @@ void init_fs_bar(struct text_object *obj, const char *arg)
        obj->data.opaque = prepare_fs_stat(arg);
 }
 
-void print_fs_bar(struct text_object *obj, int be_free_bar, char *p, int p_max_size)
+static double get_fs_perc(struct fs_stat *fs, int get_free)
 {
-       double val = 1.0;
-       struct fs_stat *fs = obj->data.opaque;
+       double ret = 0.0;
 
-       if (!fs)
-               return;
-
-       if (fs->size)
-               val = (double)fs->avail / (double)fs->size;
+       if(fs && fs->size) {
+               if(get_free)
+                       ret = fs->avail;
+               else
+                       ret = fs->size - fs->free;
+               ret /= fs->size;
+       }
 
-       if (!be_free_bar)
-               val = 1.0 - val;
+       return ret;
+}
 
-#ifdef X11
-               if(output_methods & TO_X) {
-                       new_bar(obj, p, (int)(255 * val));
-               }else
-#endif /* X11 */
-                       new_bar_in_shell(obj, p, p_max_size, (int)(100 * val));
+void print_fs_bar(struct text_object *obj, int be_free_bar, char *p, int p_max_size)
+{
+       new_bar(obj, p, p_max_size, (int)(get_fs_perc(obj->data.opaque, be_free_bar) * 255) );
 }
 
 void init_fs(struct text_object *obj, const char *arg)
@@ -240,19 +239,7 @@ void init_fs(struct text_object *obj, const char *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);
+       percent_print(p, p_max_size, (int)(get_fs_perc(obj->data.opaque, be_free) * 100) );
 }
 
 #define HUMAN_PRINT_FS_GENERATOR(name, expr)                           \