Commits

Denilson Sá committed a07828e

prettyping.sh: Add "Last message repeated %d times." feature.

Comments (0)

Files changed (1)

 # TODO: Test the behavior of this script upon receiving out-of-order packets, like these:
 #   http://www.blug.linux.no/rfc1149/pinglogg.txt
 #
-# TODO: Implement "msg repeating", like...
-#   ping: sendmsg: Network is unreachable
-#   (repeated xx times)
-# where xx gets updated for every repeated line
-#
 # TODO: Implement audible ping.
 #
 # TODO: Autodetect the width of printf numbers, so they will always line up correctly.
 	CURR_COL = 0
 }
 
+# Function called whenever a non-dotted line is repeated.
+function other_line_is_repeated() {
+	if (other_line_times < 2) {
+		return
+	}
+	if( '"${IS_TERMINAL}"' ) {
+		printf( ESC_DEFAULT ESC_ERASELINE "\r" )
+	}
+	printf( "Last message repeated %d times.", other_line_times )
+	if( ! '"${IS_TERMINAL}"' ) {
+		printf( "\n" )
+	}
+}
+
 # Prints the newlines required for the live statistics.
 #
 # I need to print some newlines and then return the cursor back to its position
 	# This is needed to keep track of lost packets
 	last_seq = 0
 
+	# The previously printed non-ping-response line
+	other_line = ""
+	other_line_times = 0
+
 	# Variables to keep the screen clean
 	IS_PRINTING_DOTS = 0
 	CURR_COL = 0
 	# Sample line:
 	# 64 bytes from 8.8.8.8: icmp_seq=1 ttl=49 time=184 ms
 	if( $0 ~ /^[0-9]+ bytes from .*: icmp_[rs]eq=[0-9]+ ttl=[0-9]+ time=[0-9.]+ *ms/ ) {
+		if( other_line_times >= 2 ) {
+			if( '"${IS_TERMINAL}"' ) {
+				printf( "\n" )
+			} else {
+				other_line_is_repeated()
+			}
+		}
+		other_line = ""
+		other_line_times = 0
+
 		# $1 = useless prefix string
 		# $2 = icmp_seq
 		# $3 = ttl
 		if( '"${IS_TERMINAL}"' ) {
 			print_statistics_bar()
 		}
+	} else if ( $0 == "" ) {
+		# Do nothing on blank lines.
 	} else {
 		other_line_is_printed()
-		printf( "%s\n", $0 )
+		if ( $0 == other_line ) {
+			other_line_times++
+			if( '"${IS_TERMINAL}"' ) {
+				other_line_is_repeated()
+			}
+		} else {
+			other_line = $0
+			other_line_times = 1
+			printf( "%s\n", $0 )
+		}
 	}
 
 	# Not needed when the output is a terminal, but does not hurt either.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.