review load* code
authorPhil Sutter <phil@nwl.cc>
Sun, 25 Oct 2009 13:13:26 +0000 (14:13 +0100)
committerPhil Sutter <phil@nwl.cc>
Tue, 3 Nov 2009 22:23:23 +0000 (23:23 +0100)
ChangeLog
doc/variables.xml
src/common.c
src/text_object.h

index bd1c51c..98c86b7 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2009-10-25
+       * Fixed loadavg and loadgraph argument parsing and corrected it's
+       documentation.
+
 2009-09-11
        * Fixed configure test failure. Tolua test program couldn't find lua
        libraries (even though they existed) and failed to compile.
index 18dc481..82a97d0 100644 (file)
             <option>(1|2|3)</option>
         </term>
         <listitem>System load average, 1 is for past 1 minute, 2
-        for past 5 minutes and 3 for past 15 minutes. 
+       for past 5 minutes and 3 for past 15 minutes. Without argument, prints
+       all three values separated by whitespace.
         <para /></listitem>
     </varlistentry>
     <varlistentry>
             <command>
                 <option>loadgraph</option>
             </command>
-            <option>(1|2|3) (height),(width) (gradient colour 1)
+            <option>(height),(width) (gradient colour 1)
             (gradient colour 2) (scale) (-t) (-l)</option>
         </term>
         <listitem>Load1 average graph, similar to xload, with
index ae84b56..e87dda6 100644 (file)
@@ -420,41 +420,26 @@ unsigned int round_to_int(float f)
 
 void scan_loadavg_arg(struct text_object *obj, const char *arg)
 {
-       int a = 1, b = 2, c = 3, r = 3;
-
-       if (arg) {
-               r = sscanf(arg, "%d %d %d", &a, &b, &c);
-               if (r >= 3 && (c < 1 || c > 3)) {
-                       r--;
-               }
-               if (r >= 2 && (b < 1 || b > 3)) {
-                       r--, b = c;
-               }
-               if (r >= 1 && (a < 1 || a > 3)) {
-                       r--, a = b, b = c;
+       obj->data.i = 0;
+       if (arg && !arg[1] && isdigit(arg[0])) {
+               obj->data.i = atoi(arg);
+               if (obj->data.i > 3 || obj->data.i < 1) {
+                       NORM_ERR("loadavg arg needs to be in range (1,3)");
+                       obj->data.i = 0;
                }
        }
-       obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
-       obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
-       obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
+       /* convert to array index (or the default (-1)) */
+       obj->data.i--;
 }
 
 void print_loadavg(struct text_object *obj, char *p, int p_max_size)
 {
        float *v = info.loadavg;
 
-       if (obj->data.loadavg[2]) {
-               snprintf(p, p_max_size, "%.2f %.2f %.2f",
-                               v[obj->data.loadavg[0] - 1],
-                               v[obj->data.loadavg[1] - 1],
-                               v[obj->data.loadavg[2] - 1]);
-       } else if (obj->data.loadavg[1]) {
-               snprintf(p, p_max_size, "%.2f %.2f",
-                               v[obj->data.loadavg[0] - 1],
-                               v[obj->data.loadavg[1] - 1]);
-       } else if (obj->data.loadavg[0]) {
-               snprintf(p, p_max_size, "%.2f",
-                               v[obj->data.loadavg[0] - 1]);
+       if (obj->data.i < 0) {
+               snprintf(p, p_max_size, "%.2f %.2f %.2f", v[0], v[1], v[2]);
+       } else {
+               snprintf(p, p_max_size, "%.2f", v[obj->data.i]);
        }
 }
 
@@ -462,17 +447,12 @@ void print_loadavg(struct text_object *obj, char *p, int p_max_size)
 void scan_loadgraph_arg(struct text_object *obj, const char *arg)
 {
        char *buf = 0;
+
        SIZE_DEFAULTS(graph);
        buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
                        &obj->e, &obj->char_a, &obj->char_b);
-       if (buf) {
-               int a = 1, r = 3;
-               if (arg) {
-                       r = sscanf(arg, "%d", &a);
-               }
-               obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
+       if (buf)
                free(buf);
-       }
 }
 
 void print_loadgraph(struct text_object *obj, char *p)
index c250c8d..c7af24a 100644 (file)
@@ -434,7 +434,6 @@ struct text_object {
                char *s;                /* some string */
                int i;                  /* some integer */
                long l;                 /* some other integer */
-               unsigned char loadavg[3];
 
                struct {
                        void *opaque;   /* temporary workaround to not blow stuff */