Subject: prefixing progress bar (patch)
To: None <lukem@netbsd.org, tech-userlevel@netbsd.org>
From: Hubert Feyrer <hubert@feyrer.de>
List: tech-userlevel
Date: 03/05/2004 12:31:21
Hi,

I'd like to have some custom text printed before (left of) the progress
bar from progress(1). E.g.

	miyu# cat openoffice-linux-1.1.0.tgz | progress -z -p 'Bytes written: ' dd of=/dev/null bs=1m
	Bytes written:    193 MB   13.83 MB/s 0+195211 records in
	^^^^^^^^^^^^^^
and:
	miyu# progress -f openoffice-linux-1.1.0.tgz -z -p 'Bytes written: ' dd of=/dev/null bs=1m
	Bytes written:  28% |******                | 57919 KB   14.12 MB/s    00:09 ETA
	^^^^^^^^^^^^^^^

Can you please review the patch below and let me know what to do with it
(send-pr, commit to -current, ...)? I've tested this with ftp(1), and
progress(1) for both reading from stdin and from file. Thanks!


 - Hubert


Index: progress/progress.1
===================================================================
RCS file: /cvsroot/src/usr.bin/progress/progress.1,v
retrieving revision 1.5
diff -u -r1.5 progress.1
--- progress/progress.1	14 Feb 2003 15:59:18 -0000	1.5
+++ progress/progress.1	5 Mar 2004 11:19:42 -0000
@@ -30,7 +30,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd January 21, 2003
+.Dd March 5, 2004
 .Dt PROGRESS 1
 .Os
 .Sh NAME
@@ -41,6 +41,7 @@
 .Op Fl z
 .Op Fl f Ar file
 .Op Fl l Ar length
+.Op Fl p Ar prefix
 .Ar cmd
 .Op Ar args ...
 .Sh DESCRIPTION
@@ -72,6 +73,10 @@
 Use the specified length for the time estimate, rather than attempting to
 .Xr fstat 2
 the input.
+.It Fl p Ar prefix
+Print the given
+.Dq prefix
+text before (left of) the progress bar.
 .It Fl z
 Filter the input through
 .Xr gzip 1 .
Index: progress/progress.c
===================================================================
RCS file: /cvsroot/src/usr.bin/progress/progress.c,v
retrieving revision 1.7
diff -u -r1.7 progress.c
--- progress/progress.c	12 Feb 2003 00:58:34 -0000	1.7
+++ progress/progress.c	5 Mar 2004 11:19:42 -0000
@@ -73,7 +73,7 @@
 usage(void)
 {
 	fprintf(stderr,
-	    "usage: %s [-z] [-f file] [-l length] cmd [args...]\n",
+	    "usage: %s [-z] [-f file] [-l length] [-p prefix] cmd [args...]\n",
 	    getprogname());
 	exit(1);
 }
@@ -96,8 +96,9 @@
 	/* defaults: Read from stdin, 0 filesize (no completion estimate) */
 	fd = STDIN_FILENO;
 	filesize = 0;
+	prefix=NULL;

-	while ((ch = getopt(argc, argv, "f:l:z")) != -1)
+	while ((ch = getopt(argc, argv, "f:l:p:z")) != -1)
 		switch (ch) {
 		case 'f':
 			infile = optarg;
@@ -106,6 +107,9 @@
 			lflag++;
 			filesize = strtoull(optarg, NULL, 0);
 			break;
+		case 'p':
+			prefix = optarg;
+			break;
 		case 'z':
 			zflag++;
 			break;
Index: ftp/progressbar.c
===================================================================
RCS file: /cvsroot/src/usr.bin/ftp/progressbar.c,v
retrieving revision 1.4
diff -u -r1.4 progressbar.c
--- ftp/progressbar.c	17 Jul 2003 12:06:18 -0000	1.4
+++ ftp/progressbar.c	5 Mar 2004 11:19:42 -0000
@@ -52,6 +52,7 @@
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <time.h>
 #include <tzfile.h>
 #include <unistd.h>
@@ -200,6 +201,8 @@
 		return;

 	len += snprintf(buf + len, BUFLEFT, "\r");
+	if (prefix)
+	  len += snprintf(buf + len, BUFLEFT, "%s", prefix);
 	if (filesize > 0) {
 		ratio = (int)((double)cursize * 100.0 / (double)filesize);
 		ratio = MAX(ratio, 0);
@@ -211,6 +214,8 @@
 			 * the number of stars won't exceed the buffer size
 			 */
 		barlength = MIN(sizeof(buf) - 1, ttywidth) - BAROVERHEAD;
+		if (prefix)
+		  	barlength -= strlen(prefix);
 		if (barlength > 0) {
 			i = barlength * ratio / 100;
 			len += snprintf(buf + len, BUFLEFT,
Index: ftp/progressbar.h
===================================================================
RCS file: /cvsroot/src/usr.bin/ftp/progressbar.h,v
retrieving revision 1.3
diff -u -r1.3 progressbar.h
--- ftp/progressbar.h	28 Feb 2003 09:53:49 -0000	1.3
+++ ftp/progressbar.h	5 Mar 2004 11:19:42 -0000
@@ -58,6 +58,7 @@
 GLOBAL	off_t	bytes;		/* current # of bytes read */
 GLOBAL	off_t	filesize;	/* size of file being transferred */
 GLOBAL	off_t	restart_point;	/* offset to restart transfer */
+GLOBAL	char   *prefix;		/* Text written left of progress bar */


 #ifndef	STANDALONE_PROGRESS

-- 
Hubert Feyrer <hubert@feyrer.de>