From 2bab29e7f53457063c889fd06f20e86e44e630c5 Mon Sep 17 00:00:00 2001 From: Benjamin Tayehanpour Date: Thu, 14 Apr 2016 13:18:18 +0200 Subject: [PATCH] Added support for coloured output. --- doc/fping.pod | 4 ++++ src/fping.c | 24 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/doc/fping.pod b/doc/fping.pod index fc8cc4ec..0a8d1841 100644 --- a/doc/fping.pod +++ b/doc/fping.pod @@ -118,6 +118,10 @@ Print usage message. The minimum amount of time (in milliseconds) between sending a ping packet to any target (default is 25). +=item B<-k> + +Colourise the output. + =item B<-l> Loop sending packets to each target indefinitely. Can be interrupted with diff --git a/src/fping.c b/src/fping.c index a7af5ed9..0aea56ed 100644 --- a/src/fping.c +++ b/src/fping.c @@ -109,7 +109,10 @@ extern int h_errno; /*** Constants ***/ -#define EMAIL "david@schweikert.ch" +#define EMAIL "david@schweikert.ch" +#define ANSI_COLOUR_RED "\x1b[31m" +#define ANSI_COLOUR_GREEN "\x1b[32m" +#define ANSI_COLOUR_RESET "\x1b[0m" /*** Ping packet defines ***/ @@ -301,6 +304,7 @@ int per_recv_flag, report_all_rtts_flag, name_flag, addr_flag, backoff_flag; int multif_flag; int timestamp_flag = 0; int random_data_flag = 0; +int colour_flag = 0; #if defined( DEBUG ) || defined( _DEBUG ) int randomly_lose_flag, sent_times_flag, trace_flag, print_per_system_flag; int lose_factor; @@ -377,7 +381,7 @@ int main( int argc, char **argv ) /* get command line options */ - while( ( c = getopt( argc, argv, "gedhlmnqusaAvDRz:t:H:i:p:f:r:c:b:C:Q:B:S:I:T:O:" ) ) != EOF ) + while( ( c = getopt( argc, argv, "gedhklmnqusaAvDRz:t:H:i:p:f:r:c:b:C:Q:B:S:I:T:O:" ) ) != EOF ) { switch( c ) { @@ -429,6 +433,10 @@ int main( int argc, char **argv ) usage(0); break; + case 'k': + colour_flag = 1; + break; + case 'q': verbose_flag = 0; quiet_flag = 1; @@ -1113,11 +1121,17 @@ void finish() if( verbose_flag || unreachable_flag ) { + if( colour_flag ) + printf( ANSI_COLOUR_RED ); + printf( "%s", h->host ); if( verbose_flag ) printf( " is unreachable" ); + if( colour_flag ) + printf( ANSI_COLOUR_RESET ); + printf( "\n" ); }/* IF */ @@ -1622,6 +1636,9 @@ int wait_for_reply(long wait_time) num_alive++; if( verbose_flag || alive_flag ) { + if( colour_flag ) + printf( ANSI_COLOUR_GREEN ); + printf( "%s", h->host ); if( verbose_flag ) @@ -1636,6 +1653,9 @@ int wait_for_reply(long wait_time) fprintf( stderr, " [<- %s]", buf); } + if( colour_flag ) + printf( ANSI_COLOUR_RESET ); + printf( "\n" ); }/* IF */