From 11f5f44812ca9498d166d3c345e96c6115a6c9bb Mon Sep 17 00:00:00 2001 From: Richard Woodbury Date: Fri, 14 Jun 2013 19:13:02 -0400 Subject: [PATCH 1/2] Use MAX_FACTORS to convey length of factors[] and scale[] arrays. --- curses.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/curses.c b/curses.c index 0584bb95..90c5fd47 100644 --- a/curses.c +++ b/curses.c @@ -426,9 +426,9 @@ void mtr_curses_hosts(int startstat) move(2, 0); } - -static double factors[] = { 0.02, 0.05, 0.08, 0.15, 0.33, 0.50, 0.80, 1.00 }; -static int scale[8]; +#define NUM_FACTORS 8 +static double factors[NUM_FACTORS] = { 0.02, 0.05, 0.08, 0.15, 0.33, 0.50, 0.80, 1.00 }; +static int scale[NUM_FACTORS]; static int low_ms, high_ms; void mtr_gen_scale(void) @@ -439,7 +439,7 @@ void mtr_gen_scale(void) low_ms = 1000000; high_ms = -1; - for (i = 0; i < 8; i++) { + for (i = 0; i < NUM_FACTORS; i++) { scale[i] = 0; } max = net_max(); @@ -456,19 +456,20 @@ void mtr_gen_scale(void) } } range = high_ms - low_ms; - for (i = 0; i < 8; i++) { + for (i = 0; i < NUM_FACTORS; i++) { scale[i] = low_ms + ((double)range * factors[i]); } } +/* NB: Must have NUM_FACTORS elements. */ static const char* block_map = ".123abc>"; void mtr_print_scaled(int ms) { int i; - for (i = 0; i < 8; i++) { + for (i = 0; i < NUM_FACTORS; i++) { if (ms <= scale[i]) { printw("%c", block_map[i]); return; @@ -494,7 +495,7 @@ void mtr_fill_graph(int at, int cols) } else { if (display_mode == 1) { if (saved[i] > scale[6]) { - printw("%c", block_map[7]); + printw("%c", block_map[NUM_FACTORS-1]); } else { printw("."); } @@ -637,7 +638,7 @@ void mtr_curses_redraw(void) printw("Scale:"); attroff(A_BOLD); - for (i = 0; i < 7; i++) { + for (i = 0; i < NUM_FACTORS-1; i++) { printw(" %c:%d ms", block_map[i], scale[i]/1000); } } From 795925c4a5c329562cb16e70377d22279d075e32 Mon Sep 17 00:00:00 2001 From: Richard Woodbury Date: Sun, 16 Jun 2013 18:36:20 -0400 Subject: [PATCH 2/2] Make block_map dynamically constructed. --- curses.c | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/curses.c b/curses.c index 90c5fd47..a103a435 100644 --- a/curses.c +++ b/curses.c @@ -427,7 +427,7 @@ void mtr_curses_hosts(int startstat) } #define NUM_FACTORS 8 -static double factors[NUM_FACTORS] = { 0.02, 0.05, 0.08, 0.15, 0.33, 0.50, 0.80, 1.00 }; +static double factors[NUM_FACTORS]; static int scale[NUM_FACTORS]; static int low_ms, high_ms; @@ -462,8 +462,33 @@ void mtr_gen_scale(void) } -/* NB: Must have NUM_FACTORS elements. */ -static const char* block_map = ".123abc>"; +static char block_map[NUM_FACTORS]; + +void mtr_curses_init() { + int i; + int block_split; + + /* Initialize factors to a log scale. */ + for (i = 0; i < NUM_FACTORS; i++) { + factors[i] = ((double)1 / NUM_FACTORS) * (i + 1); + factors[i] *= factors[i]; /* Squared. */ + } + + /* Initialize block_map. */ + block_split = (NUM_FACTORS - 2) / 2; + if (block_split > 9) { + block_split = 9; + } + for (i = 1; i <= block_split; i++) { + block_map[i] = '0' + i; + } + for (i = block_split+1; i < NUM_FACTORS-1; i++) { + block_map[i] = 'a' + i - block_split - 1; + } + block_map[0] = '.'; + block_map[NUM_FACTORS-1] = '>'; +} + void mtr_print_scaled(int ms) { @@ -653,6 +678,7 @@ void mtr_curses_open(void) raw(); noecho(); + mtr_curses_init(); mtr_curses_redraw(); }