[axel-devel] [PATCH] prevent -a from thrashing the terminal

Aron Griffis agriffis at n01se.net
Thu Dec 17 05:24:19 UTC 2009


Presently alternate output mode writes a new string to the
terminal every time any data arrives on any connection. This
redraws the output line even when there's no change from the last
output.

This patch causes the redraw to occur only when the string
changes. Patch is against trunk and also applies cleanly to 2.4
in Debian. Tested and demonstrated effective using 2.4 on Debian.

Signed-off-by: Aron Griffis <agriffis at n01se.net>

Index: text.c
===================================================================
--- text.c	(revision 115)
+++ text.c	(working copy)
@@ -486,35 +486,38 @@
 	long long int total=axel->size;
 	int i,j=0;
 	double now = gettime();
+	static char str[MAX_STRING] = {0};
+	static char prev[MAX_STRING] = {0};
+	char *s = str;
 	
-	printf("\r[%3ld%%] [", min(100,(long)(done*100./total+.5) ) );
-		
+	s += sprintf(s, "\r[%3ld%%] [", min(100,(long)(done*100./total+.5) ) );
+
 	for(i=0;i<axel->conf->num_connections;i++)
 	{
 		for(;j<((double)axel->conn[i].currentbyte/(total+1)*50)-1;j++)
-			putchar('.');
+			*s++ = '.';
 
 		if(axel->conn[i].currentbyte<axel->conn[i].lastbyte)
 		{
 			if(now <= axel->conn[i].last_transfer + axel->conf->connection_timeout/2 )
-				putchar(i+'0');
+				*s++ = '0'+i;
 			else
-				putchar('#');
+				*s++ = '#';
 		} else 
-			putchar('.');
+			*s++ = '.';
 
 		j++;
 		
 		for(;j<((double)axel->conn[i].lastbyte/(total+1)*50);j++)
-			putchar(' ');
+			*s++ = ' ';
 	}
 	
 	if(axel->bytes_per_second > 1048576)
-		printf( "] [%6.1fMB/s]", (double) axel->bytes_per_second / (1024*1024) );
+		s += sprintf(s, "] [%6.1fMB/s]", (double) axel->bytes_per_second / (1024*1024) );
 	else if(axel->bytes_per_second > 1024)
-		printf( "] [%6.1fKB/s]", (double) axel->bytes_per_second / 1024 );
+		s += sprintf(s, "] [%6.1fKB/s]", (double) axel->bytes_per_second / 1024 );
 	else
-		printf( "] [%6.1fB/s]", (double) axel->bytes_per_second );
+		s += sprintf(s, "] [%6.1fB/s]", (double) axel->bytes_per_second );
 	
 	if(done<total)
 	{
@@ -524,14 +527,18 @@
 		hours=minutes/60;minutes-=hours*60;
 		days=hours/24;hours-=days*24;
 		if(days)
-			printf(" [%2dd%2d]",days,hours);
+			s += sprintf(s, " [%2dd%2d]",days,hours);
 		else if(hours)
-			printf(" [%2dh%02d]",hours,minutes);
+			s += sprintf(s, " [%2dh%02d]",hours,minutes);
 		else
-			printf(" [%02d:%02d]",minutes,seconds);
+			s += sprintf(s, " [%02d:%02d]",minutes,seconds);
 	}
-	
-	fflush( stdout );
+
+	if(strcmp(str, prev)) {
+		strcpy(prev, str);
+		fputs(str, stdout);
+		fflush(stdout);
+	}
 }
 
 void print_help()



More information about the axel-devel mailing list