[Iftop-users] [PATCH] Add colored output to iftop

David Heidelberger david.heidelberger at ixit.cz
Wed, 19 Mar 2014 18:05:44 +0100


Add colored output to iftop.

Based on https://github.com/srix/iftopcolor

Signed-off-by: David Heidelberger <david.heidelberger@ixit.cz>
---
diff -Naur a/ui.c b/ui.c
--- a/ui.c	2014-01-08 14:00:48.776905827 +0100
+++ b/ui.c	2014-01-08 14:07:51.064903944 +0100
@@ -51,6 +51,9 @@
  "\n"\
  "iftop, version " IFTOP_VERSION

+#define COLOR_SENT 1
+#define COLOR_RECV 2
+#define COLOR_BOTH 3

  extern hash_type* history;
  extern int history_pos;
@@ -199,12 +202,13 @@
      }
  }

-void draw_bar(float n, int y) {
+void draw_bar(float n, int y, short colorpair) {
      int L;
+    colorpair = has_colors() ? colorpair : 0; /* set 0 if terminal is 
not color capable*/
      mvchgat(y, 0, -1, A_NORMAL, 0, NULL);
      L = get_bar_length(8 * n);
      if (L > 0)
-        mvchgat(y, 0, L + 1, A_REVERSE, 0, NULL);
+        mvchgat(y, 0, L + 1, A_REVERSE, colorpair, NULL);
  }

  void draw_line_totals(int y, host_pair_line* line, option_linedisplay_t 
linedisplay) {
@@ -219,17 +223,17 @@
      if(options.showbars) {
        switch(linedisplay) {
          case OPTION_LINEDISPLAY_TWO_LINE:
-          draw_bar(line->sent[options.bar_interval],y);
-          draw_bar(line->recv[options.bar_interval],y+1);
+          draw_bar(line->sent[options.bar_interval],y,  COLOR_SENT);
+          draw_bar(line->recv[options.bar_interval],y+1, COLOR_RECV);
            break;
          case OPTION_LINEDISPLAY_ONE_LINE_SENT:
-          draw_bar(line->sent[options.bar_interval],y);
+          draw_bar(line->sent[options.bar_interval],y,COLOR_SENT);
            break;
          case OPTION_LINEDISPLAY_ONE_LINE_RECV:
-          draw_bar(line->recv[options.bar_interval],y);
+          draw_bar(line->recv[options.bar_interval],y,COLOR_RECV);
            break;
          case OPTION_LINEDISPLAY_ONE_LINE_BOTH:
-          draw_bar(line->recv[options.bar_interval] + 
line->sent[options.bar_interval],y);
+          draw_bar(line->recv[options.bar_interval] + 
line->sent[options.bar_interval],y, COLOR_BOTH);
            break;
        }
      }
@@ -428,6 +432,13 @@

  void ui_curses_init() {
      (void) initscr();      /* initialize the curses library */
+    if (has_colors()) {
+	start_color();
+	use_default_colors();
+	init_pair(COLOR_RECV, COLOR_GREEN, -1);
+	init_pair(COLOR_SENT, COLOR_BLUE, -1);
+	init_pair(COLOR_BOTH, COLOR_MAGENTA, -1);
+    }
      keypad(stdscr, TRUE);  /* enable keyboard mapping */
      (void) nonl();         /* tell curses not to do NL->CR/NL on output 
*/
      (void) cbreak();       /* take input chars one at a time, no wait 
for \n */