summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 6d493d2)
raw | patch | inline | side by side (parent: 6d493d2)
author | Antoine Beaupré <anarcat@koumbit.org> | |
Tue, 3 Dec 2013 01:45:58 +0000 (20:45 -0500) | ||
committer | Antoine Beaupré <anarcat@koumbit.org> | |
Tue, 3 Dec 2013 01:46:21 +0000 (20:46 -0500) |
this is a first stab at porting "prettyping.sh" into noping. the idea
is to show a histogram of ping times instead of numerical values. with
some work, we could actually show both, but this focuses on
implementing the hard part (the histogram) properly.
it is very rudimentary for now:
* the math may be wrong for the size of the bar, I was mostly in a
rush to make unicode work and have something pretty quickly.
* all hosts are on the same line: each should have its own line
* the histogram should be displayed in a separate window
* it should use background colors as well
* it should fallback when the terminal is not unicode-capable
* the scaling logic is fully automatic, which necessarily gives weird
results at first
this requires switching to the ncursesw library and other unicode
ncurses hackery described in this post:
http://newsgroups.derkeiler.com/Archive/Rec/rec.games.roguelike.development/2010-09/msg00050.html
pretty ping is a awk/bash script, MIT-licensed, available here:
https://bitbucket.org/denilsonsa/small_scripts/src/tip/prettyping.sh
is to show a histogram of ping times instead of numerical values. with
some work, we could actually show both, but this focuses on
implementing the hard part (the histogram) properly.
it is very rudimentary for now:
* the math may be wrong for the size of the bar, I was mostly in a
rush to make unicode work and have something pretty quickly.
* all hosts are on the same line: each should have its own line
* the histogram should be displayed in a separate window
* it should use background colors as well
* it should fallback when the terminal is not unicode-capable
* the scaling logic is fully automatic, which necessarily gives weird
results at first
this requires switching to the ncursesw library and other unicode
ncurses hackery described in this post:
http://newsgroups.derkeiler.com/Archive/Rec/rec.games.roguelike.development/2010-09/msg00050.html
pretty ping is a awk/bash script, MIT-licensed, available here:
https://bitbucket.org/denilsonsa/small_scripts/src/tip/prettyping.sh
src/Makefile.am | patch | blob | history | |
src/oping.c | patch | blob | history | |
src/oping.h | patch | blob | history |
diff --git a/src/Makefile.am b/src/Makefile.am
index 13267836f714d3e4686d01bcac44830cf9daea12..c26688395091ba84e99ed3e8cccd67b401505b4c 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
noping_SOURCES = oping.c
noping_CPPFLAGS = $(AM_CPPFLAGS) -DUSE_NCURSES=1
-noping_LDADD = liboping.la -lm -lncurses
+noping_LDADD = liboping.la -lm -lncursesw
if BUILD_WITH_LIBRT
noping_LDADD += -lrt
endif
diff --git a/src/oping.c b/src/oping.c
index 79d7569db279b8a1d04eeb8b2ced4e3669179188..b3a191e274904870a5b468756c7ae6ea896ae2c7 100644 (file)
--- a/src/oping.c
+++ b/src/oping.c
#include <sys/types.h>
#endif
+#include <locale.h>
+
#if USE_NCURSES
# define NCURSES_OPAQUE 1
-# include <ncurses.h>
+/* http://newsgroups.derkeiler.com/Archive/Rec/rec.games.roguelike.development/2010-09/msg00050.html */
+# define _X_OPEN_SOURCE_EXTENDED
+# include <ncursesw/ncurses.h>
# define OPING_GREEN 1
# define OPING_YELLOW 2
#include "oping.h"
+const char *bars[BARS_LEN] = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" };
+
#ifndef _POSIX_SAVED_IDS
# define _POSIX_SAVED_IDS 0
#endif
if (has_colors () == TRUE)
{
int color = OPING_GREEN;
- double average = context_get_average (context);
- double stddev = context_get_stddev (context);
-
- if ((latency < (average - (2 * stddev)))
- || (latency > (average + (2 * stddev))))
- color = OPING_RED;
- else if ((latency < (average - stddev))
- || (latency > (average + stddev)))
- color = OPING_YELLOW;
-
- HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i ",
- data_len, context->host, context->addr,
- sequence, recv_ttl,
- format_qos (recv_qos, recv_qos_str, sizeof (recv_qos_str)));
- if ((recv_qos != 0) || (opt_send_qos != 0))
- {
- HOST_PRINTF ("qos=%s ",
- format_qos (recv_qos, recv_qos_str, sizeof (recv_qos_str)));
- }
- HOST_PRINTF ("time=");
- wattron (main_win, COLOR_PAIR(color));
- HOST_PRINTF ("%.2f", latency);
+ float ratio = 0;
+ int index = 0;
+
+ ratio = ( latency - context->latency_min ) / ( context->latency_max - context->latency_min );
+ if (ratio > 2/3.0) {
+ color = OPING_RED;
+ }
+ else if (ratio > 1/3.0) {
+ color = OPING_YELLOW;
+ }
+ 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) {
+ index = 0; /* safety check */
+ }
+ wattron (main_win, COLOR_PAIR(color));
+ HOST_PRINTF (bars[index]);
wattroff (main_win, COLOR_PAIR(color));
- HOST_PRINTF (" ms\n");
}
else
{
#if USE_NCURSES
if (has_colors () == TRUE)
{
- HOST_PRINTF ("echo reply from %s (%s): icmp_seq=%u ",
- context->host, context->addr,
- sequence);
wattron (main_win, COLOR_PAIR(OPING_RED) | A_BOLD);
- HOST_PRINTF ("timeout");
+ HOST_PRINTF ("!");
wattroff (main_win, COLOR_PAIR(OPING_RED) | A_BOLD);
- HOST_PRINTF ("\n");
}
else
{
}
#endif
+ setlocale(LC_ALL, "");
optind = read_options (argc, argv);
#if !_POSIX_SAVED_IDS
diff --git a/src/oping.h b/src/oping.h
index cd7d62f9589b8098137d11b08e4a8cf6a7d23099..2a5bc3a31b9eacd0facab60e54b8d5425b7fb5fe 100644 (file)
--- a/src/oping.h
+++ b/src/oping.h
#define PING_DEF_AF AF_UNSPEC
#define PING_DEF_DATA "liboping -- ICMP ping library <http://octo.it/liboping/>"
+#define BARS_LEN 8
+extern const char *bars[BARS_LEN];
+
/*
* Method definitions
*/