+# if HAVE_NCURSESW_NCURSES_H
+ if (!opt_utf8)
+ {
+ /* Automatically determine */
+ if (strcasecmp ("UTF-8", nl_langinfo (CODESET)) == 0)
+ opt_utf8 = 2;
+ else
+ opt_utf8 = 1;
+ }
+ return ((_Bool) (opt_utf8 - 1));
+# else
+ return (0);
+# endif
+} /* }}} _Bool has_utf8 */
+
+static int update_graph_boxplot (ping_context_t *ctx) /* {{{ */
+{
+ uint32_t *counters;
+ double *ratios;
+ size_t i;
+ size_t x_max;
+ size_t x;
+
+ clean_history (ctx);
+
+ if (ctx->history_received == 0)
+ return (ENOENT);
+
+ x_max = (size_t) getmaxx (ctx->window);
+ if (x_max <= 8)
+ return (EINVAL);
+ x_max -= 4;
+
+ counters = calloc (x_max, sizeof (*counters));
+ ratios = calloc (x_max, sizeof (*ratios));
+
+ /* Bucketize */
+ for (i = 0; i < ctx->history_received; i++)
+ {
+ double latency = ctx->history_by_value[i] / 1000.0;
+ size_t index = (size_t) (((double) x_max) * latency / opt_interval);
+
+ if (index >= x_max)
+ index = x_max - 1;
+
+ counters[index]++;
+ }
+
+ /* Sum and calc ratios */
+ ratios[0] = ((double) counters[0]) / ((double) ctx->history_received);
+ for (x = 1; x < x_max; x++)
+ {
+ counters[x] += counters[x - 1];
+ ratios[x] = ((double) counters[x]) / ((double) ctx->history_received);
+ }
+
+ for (x = 0; x < x_max; x++)
+ {
+ int symbol = ' ';
+ _Bool reverse = 0;
+
+ if (x == 0)
+ {
+ if (ratios[x] >= 0.5)
+ {
+ symbol = BOXPLOT_MEDIAN;
+ reverse = 1;
+ }
+ else if (ratios[x] > 0.25)
+ {
+ symbol = BOXPLOT_BOX;
+ reverse = 1;
+ }
+ else if (ratios[x] > 0.025)
+ symbol = BOXPLOT_WHISKER_BAR;
+ else
+ symbol = ' '; /* NOP */
+ }
+ else /* (x != 0) */
+ {
+ if ((ratios[x - 1] < 0.5) && (ratios[x] >= 0.5))
+ {
+ symbol = BOXPLOT_MEDIAN;
+ reverse = 1;
+ }
+ else if (((ratios[x] >= 0.25) && (ratios[x] <= 0.75))
+ || ((ratios[x - 1] < 0.75) && (ratios[x] > 0.75)))
+ {
+ symbol = BOXPLOT_BOX;
+ reverse = 1;
+ }
+ else if ((ratios[x] < 0.5) && (ratios[x] >= 0.025))
+ {
+ if (ratios[x - 1] < 0.025)
+ symbol = BOXPLOT_WHISKER_LEFT_END;
+ else
+ symbol = BOXPLOT_WHISKER_BAR;
+ }
+ else if ((ratios[x] > .5) && (ratios[x] < 0.975))
+ {
+ symbol = BOXPLOT_WHISKER_BAR;
+ }
+ else if ((ratios[x] >= 0.975) && (ratios[x - 1] < 0.975))
+ symbol = BOXPLOT_WHISKER_RIGHT_END;
+ }
+
+ if (reverse)
+ wattron (ctx->window, A_REVERSE);
+ mvwaddch (ctx->window, /* y = */ 3, /* x = */ (int) (x + 2), symbol);
+ // mvwprintw (ctx->window, /* y = */ 3, /* x = */ (int) (x + 2), symbol);
+ if (reverse)
+ wattroff (ctx->window, A_REVERSE);
+ }
+
+ free (counters);
+ free (ratios);
+ return (0);
+} /* }}} int update_graph_boxplot */
+
+static int update_graph_prettyping (ping_context_t *ctx, /* {{{ */
+ double latency, unsigned int sequence)
+{
+ size_t x;
+ size_t x_max;
+ size_t history_offset;
+
+ x_max = (size_t) getmaxx (ctx->window);
+ if (x_max <= 4)
+ return (EINVAL);
+ x_max -= 4;
+
+ /* Determine the first index in the history we need to draw
+ * the graph. */
+ history_offset = 0;
+ if (((size_t) x_max) < ctx->history_size) /* window is smaller than history */
+ {
+ if (ctx->history_index > x_max)
+ history_offset = ctx->history_index - x_max;
+ else /* wrap around */
+ history_offset = ctx->history_index + ctx->history_size - x_max;
+ }
+ else /* window is larger than history */
+ {
+ if (ctx->history_index != ctx->history_size) /* no longer growing. */
+ history_offset = ctx->history_index;
+ else /* start-up */
+ history_offset = 0;
+ }
+
+ for (x = 0; x < x_max; x++)
+ {
+ size_t index;
+ double latency;
+
+ int color = OPING_RED;
+ char const *symbol = "!";
+ int symbolc = '!';
+
+ if (x >= ctx->history_size)
+ {
+ mvwaddch (ctx->window, /* y = */ 3, /* x = */ x + 2, ' ');
+ continue;
+ }
+
+ index = (history_offset + x) % ctx->history_size;
+ latency = ctx->history_by_time[index];
+
+ if (latency >= 0.0)
+ {
+ double ratio;
+
+ size_t symbols_num = hist_symbols_acs_num;
+ size_t colors_num = 1;
+
+ size_t index_symbols;
+ size_t index_colors;
+ size_t intensity;
+
+ /* latency is in milliseconds, opt_interval is in seconds. */
+ ratio = (latency * 0.001) / opt_interval;
+ if (ratio > 1) {
+ ratio = 1.0;
+ }
+
+ if (has_utf8 ())
+ symbols_num = hist_symbols_utf8_num;
+
+ if (has_colors () == TRUE)
+ colors_num = hist_colors_num;
+
+ intensity = (size_t) (ratio * ((double) (symbols_num * colors_num)));
+ if (intensity >= (symbols_num * colors_num))
+ intensity = (symbols_num * colors_num) - 1;
+
+ index_symbols = intensity % symbols_num;
+ assert (index_symbols < symbols_num);
+
+ index_colors = intensity / symbols_num;
+ assert (index_colors < colors_num);
+
+ if (has_utf8())
+ {
+ color = hist_colors_utf8[index_colors];
+ symbol = hist_symbols_utf8[index_symbols];
+ }
+ else
+ {
+ color = hist_colors_acs[index_colors];
+ symbolc = hist_symbols_acs[index_symbols] | A_ALTCHARSET;
+ }
+ }
+ else /* if (!(latency >= 0.0)) */
+ wattron (ctx->window, A_BOLD);
+
+ if (has_colors () == TRUE)
+ wattron (ctx->window, COLOR_PAIR(color));
+
+ if (has_utf8())
+ mvwprintw (ctx->window, /* y = */ 3, /* x = */ x + 2, symbol);
+ else
+ mvwaddch (ctx->window, /* y = */ 3, /* x = */ x + 2, symbolc);
+
+ if (has_colors () == TRUE)
+ wattroff (ctx->window, COLOR_PAIR(color));
+
+ /* Use negation here to handle NaN correctly. */
+ if (!(latency >= 0.0))
+ wattroff (ctx->window, A_BOLD);
+ } /* for (x) */
+
+ return (0);
+} /* }}} int update_graph_prettyping */
+
+static int update_graph_histogram (ping_context_t *ctx) /* {{{ */
+{
+ uint32_t *counters;
+ uint32_t *accumulated;
+ uint32_t max;
+ size_t i;
+ size_t x_max;
+ size_t x;
+
+ size_t symbols_num = hist_symbols_acs_num;
+
+ clean_history (ctx);
+
+ if (ctx->history_received == 0)
+ return (ENOENT);
+
+ if (has_utf8 ())
+ symbols_num = hist_symbols_utf8_num;
+
+ x_max = (size_t) getmaxx (ctx->window);
+ if (x_max <= 4)
+ return (EINVAL);
+ x_max -= 4;
+
+ counters = calloc (x_max, sizeof (*counters));
+ accumulated = calloc (x_max, sizeof (*accumulated));
+
+ /* Bucketize */
+ max = 0;
+ for (i = 0; i < ctx->history_received; i++)
+ {
+ double latency = ctx->history_by_value[i] / 1000.0;
+ size_t index = (size_t) (((double) x_max) * latency / opt_interval);
+
+ if (index >= x_max)
+ index = x_max - 1;
+
+ counters[index]++;
+ if (max < counters[index])
+ max = counters[index];
+ }
+
+ /* Sum */
+ accumulated[0] = counters[0];
+ for (x = 1; x < x_max; x++)
+ accumulated[x] = counters[x] + accumulated[x - 1];
+
+ /* Calculate ratios */
+ for (x = 0; x < x_max; x++)
+ {
+ double height = ((double) counters[x]) / ((double) max);
+ double ratio_this = ((double) accumulated[x]) / ((double) ctx->history_received);
+ double ratio_prev = 0.0;
+ size_t index;
+ int color = 0;
+
+ index = (size_t) (height * ((double) symbols_num));
+ if (index >= symbols_num)
+ index = symbols_num - 1;
+
+ if (x > 0)
+ ratio_prev = ((double) accumulated[x - 1]) / ((double) ctx->history_received);
+
+ if (has_colors () == TRUE)
+ {
+ if ((ratio_this <= threshold_green)
+ || ((ratio_prev < threshold_green)
+ && (ratio_this > threshold_green)))
+ color = OPING_GREEN;
+ else if ((ratio_this <= threshold_yellow)
+ || ((ratio_prev < threshold_yellow)
+ && (ratio_this > threshold_yellow)))
+ color = OPING_YELLOW;
+ else
+ color = OPING_RED;
+
+ wattron (ctx->window, COLOR_PAIR(color));
+ }
+
+ if (counters[x] == 0)
+ mvwaddch (ctx->window, /* y = */ 3, /* x = */ x + 2, ' ');
+ else if (has_utf8 ())
+ mvwprintw (ctx->window, /* y = */ 3, /* x = */ x + 2,
+ hist_symbols_utf8[index]);
+ else
+ mvwaddch (ctx->window, /* y = */ 3, /* x = */ x + 2,
+ hist_symbols_acs[index] | A_ALTCHARSET);
+
+ if (has_colors () == TRUE)
+ wattroff (ctx->window, COLOR_PAIR(color));
+
+ }
+
+ free (accumulated);
+ return (0);
+} /* }}} int update_graph_histogram */
+
+static int update_stats_from_context (ping_context_t *ctx, pingobj_iter_t *iter) /* {{{ */
+{
+ double latency = -1.0;
+ size_t buffer_len = sizeof (latency);
+
+ ping_iterator_get_info (iter, PING_INFO_LATENCY,
+ &latency, &buffer_len);
+
+ unsigned int sequence = 0;
+ buffer_len = sizeof (sequence);
+ ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
+ &sequence, &buffer_len);
+
+