Extend commit 25680305095bfcedaa46cb017182544183ab743b to the whole cpu object.
[monky] / src / specials.c
index 101a0a6..3fa6e0b 100644 (file)
@@ -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.
  *
@@ -48,8 +48,8 @@ int special_count;
 int default_bar_width = 0, default_bar_height = 6;
 #ifdef X11
 int default_graph_width = 0, default_graph_height = 25;
-int default_gauge_width = 40, default_gauge_height = 25;
 #endif /* X11 */
+int default_gauge_width = 40, default_gauge_height = 25;
 
 /*
  * Special data typedefs
@@ -82,7 +82,6 @@ struct tab {
  * Scanning arguments to various special text objects
  */
 
-#ifdef X11
 const char *scan_gauge(struct text_object *obj, const char *args)
 {
        struct gauge *g;
@@ -109,7 +108,6 @@ const char *scan_gauge(struct text_object *obj, const char *args)
        obj->special_data = g;
        return args;
 }
-#endif /* X11 */
 
 const char *scan_bar(struct text_object *obj, const char *args)
 {
@@ -245,8 +243,16 @@ static struct special_t *new_special(char *buf, enum special_types t)
        return &specials[special_count++];
 }
 
+void new_gauge_in_shell(struct text_object *obj, char *p, int p_max_size, int usage)
+{
+       static const char *gaugevals[] = { "_. ", "\\. ", " | ", " ./", " ._" };
+       (void)obj;
+
+       snprintf(p, p_max_size, "%s", gaugevals[round_to_int((double)usage * 4 / 255)]);
+}
+
 #ifdef X11
-void new_gauge(struct text_object *obj, char *buf, int usage)
+void new_gauge_in_x11(struct text_object *obj, char *buf, int usage)
 {
        struct special_t *s = 0;
        struct gauge *g = obj->special_data;
@@ -259,11 +265,28 @@ void new_gauge(struct text_object *obj, char *buf, int usage)
 
        s = new_special(buf, GAUGE);
 
-       s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
+       s->arg = usage;
        s->width = g->width;
        s->height = g->height;
 }
+#endif /* X11 */
+
+void new_gauge(struct text_object *obj, char *p, int p_max_size, int usage)
+{
+       if (!p_max_size)
+               return;
+
+       usage = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
+
+#ifdef X11
+       if (output_methods & TO_X)
+               new_gauge_in_x11(obj, p, usage);
+       else
+#endif /* X11 */
+               new_gauge_in_shell(obj, p, p_max_size, usage);
+}
 
+#ifdef X11
 void new_font(char *buf, char *args)
 {
        if ((output_methods & TO_X) == 0)
@@ -301,7 +324,6 @@ static void graph_append(struct special_t *graph, double f, char showaslog)
                f = graph->graph_scale;
        }
 
-       graph->graph[0] = f;    /* add new data */
        /* shift all the data by 1 */
        for (i = graph->graph_width - 1; i > 0; i--) {
                graph->graph[i] = graph->graph[i - 1];
@@ -310,13 +332,14 @@ static void graph_append(struct special_t *graph, double f, char showaslog)
                        graph->graph_scale = graph->graph[i - 1];
                }
        }
-       if (graph->scaled && graph->graph[graph->graph_width] > graph->graph_scale) {
+       graph->graph[0] = f;    /* add new data */
+       if (graph->scaled && graph->graph[0] > graph->graph_scale) {
                /* check if we need to update the scale */
-               graph->graph_scale = graph->graph[graph->graph_width];
+               graph->graph_scale = graph->graph[0];
        }
 }
 
-void new_graph(struct text_object *obj, char *buf, double val)
+void new_graph(struct text_object *obj, char *buf, int buf_max_size, double val)
 {
        struct special_t *s = 0;
        struct graph *g = obj->special_data;
@@ -324,7 +347,7 @@ void new_graph(struct text_object *obj, char *buf, double val)
        if ((output_methods & TO_X) == 0)
                return;
 
-       if (!g)
+       if (!g || !buf_max_size)
                return;
 
        s = new_special(buf, GRAPH);
@@ -468,17 +491,11 @@ static void new_bar_in_x11(struct text_object *obj, char *buf, int usage)
        struct special_t *s = 0;
        struct bar *b = obj->special_data;
 
-       if ((output_methods & TO_X) == 0)
-               return;
-
-       if (!b)
-               return;
-
        s = new_special(buf, BAR);
 
        s->arg = usage;
-       s->width = b->width;
-       s->height = b->height;
+       s->width = b ?  b->width : default_bar_width;
+       s->height = b ? b->height : default_bar_height;
 }
 #endif /* X11 */