cleanup safety checks
[liboping.git] / src / oping.c
index 9958616..a630b8d 100644 (file)
@@ -85,6 +85,9 @@
 # define OPING_GREEN 1
 # define OPING_YELLOW 2
 # define OPING_RED 3
+# define OPING_GREEN_HIST 4
+# define OPING_YELLOW_HIST 5
+# define OPING_RED_HIST 6
 #endif
 
 #include "oping.h"
@@ -607,6 +610,9 @@ static int update_stats_from_context (ping_context_t *ctx, pingobj_iter_t *iter)
 {
        double latency = -1.0;
        size_t buffer_len = sizeof (latency);
+        int maxx;
+        getmaxyx(ctx->window, maxx, maxx);
+
        ping_iterator_get_info (iter, PING_INFO_LATENCY,
                        &latency, &buffer_len);
 
@@ -642,7 +648,7 @@ static int update_stats_from_context (ping_context_t *ctx, pingobj_iter_t *iter)
                deviation = context_get_stddev (ctx);
                        
                mvwprintw (ctx->window, /* y = */ 2, /* x = */ 2,
-                               "rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms\n",
+                               "rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms",
                                ctx->latency_min,
                                average,
                                ctx->latency_max,
@@ -653,27 +659,34 @@ static int update_stats_from_context (ping_context_t *ctx, pingobj_iter_t *iter)
        {
                if (has_colors () == TRUE)
                {
-                       int color = OPING_GREEN;
+                       int color = OPING_GREEN_HIST;
                         float ratio = 0;
                         int index = 0;
 
                         ratio = latency / PING_DEF_TTL;
+                        if (ratio > 1) {
+                          ratio = 1;
+                        }
                         if (ratio > 2/3.0) {
-                          color = OPING_RED;
+                          color = OPING_RED_HIST;
                         }
                         else if (ratio > 1/3.0) {
-                          color = OPING_YELLOW;
+                          color = OPING_YELLOW_HIST;
                         }
                         index = (int) (ratio * BARS_LEN * 3); /* 3 colors */
                         /* HOST_PRINTF ("%%r%f-ia%d-", ratio, index); */
                         index = index % (BARS_LEN-1);
                         /* HOST_PRINTF ("im%d-", index); */
-                        if (index < 0 || index >= BARS_LEN) {
+                        if (index < 0) {
                           index = 0; /* safety check */
                         }
+                        if (index >= BARS_LEN) {
+                          index = BARS_LEN -1; /* safety check */
+                        }
                         wattron (ctx->window, COLOR_PAIR(color));
                         mvwprintw (ctx->window,
-                                   /* y = */ 3, /* x = */ 1 + sequence, 
+                                   /* y = */ 3,
+                                   /* x = */ (1 + sequence) % maxx,
                                    bars[index]);
                        wattroff (ctx->window, COLOR_PAIR(color));
                }
@@ -684,7 +697,8 @@ static int update_stats_from_context (ping_context_t *ctx, pingobj_iter_t *iter)
         else {
                 wattron (ctx->window, COLOR_PAIR(OPING_RED) | A_BOLD);
                 mvwprintw (ctx->window,
-                           /* y = */ 3, /* x = */ 1 + sequence, 
+                           /* y = */ 3,
+                           /* x = */ (1 + sequence) % maxx,
                            "!");
                 wattroff (ctx->window, COLOR_PAIR(OPING_RED) | A_BOLD);
         }
@@ -778,6 +792,9 @@ static int pre_loop_hook (pingobj_t *ping) /* {{{ */
                init_pair (OPING_GREEN,  COLOR_GREEN,  /* default = */ 0);
                init_pair (OPING_YELLOW, COLOR_YELLOW, /* default = */ 0);
                init_pair (OPING_RED,    COLOR_RED,    /* default = */ 0);
+               init_pair (OPING_GREEN_HIST,  COLOR_GREEN,  COLOR_BLACK);
+               init_pair (OPING_YELLOW_HIST, COLOR_YELLOW, COLOR_GREEN);
+               init_pair (OPING_RED_HIST,    COLOR_RED,    COLOR_YELLOW);
        }
 
        main_win_height = height - (5 * host_num);