From d8d82215561305cb7af0a33d7810cbb9cadb2cea Mon Sep 17 00:00:00 2001
From: Aaron Griffin <aaron@archlinux.org>
Date: Fri, 22 Dec 2006 07:11:20 +0000
Subject: Modified front end output routines to accept a "padding' setting,
 which pads any statement with ' ' until the size of the terminal.  The
 rationale is that, when a log message is emitted during progress bar display,
 the terminal is artifacted. This prevents that messiness.

---
 src/pacman/downloadprog.c |  2 ++
 src/pacman/log.c          | 32 +++++++++++++++++++++++++++++---
 src/pacman/log.h          |  2 ++
 src/pacman/trans.c        |  6 ++++++
 4 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/src/pacman/downloadprog.c b/src/pacman/downloadprog.c
index 06345782..16cfd6c9 100644
--- a/src/pacman/downloadprog.c
+++ b/src/pacman/downloadprog.c
@@ -65,6 +65,7 @@ void log_progress(const char *filename, int xfered, int total)
 	float total_timediff, timediff;
 
   if(xfered == 0) {
+		set_output_padding(1); /* we need padding from pm_fprintf output */
 		gettimeofday(&initial_time, NULL);
 		gettimeofday(&last_time, NULL);
 		xfered_last = 0;
@@ -88,6 +89,7 @@ void log_progress(const char *filename, int xfered, int total)
 		/* compute final values */
 		rate = total / (total_timediff * 1024);
 		eta_s = (int)total_timediff;
+		set_output_padding(0); /* shut off padding */
 	} else if(timediff < UPDATE_SPEED_SEC) {
 	/* we avoid computing the ETA on too small periods of time, so that
 		 results are more significant */
diff --git a/src/pacman/log.c b/src/pacman/log.c
index 67b69324..7231545e 100644
--- a/src/pacman/log.c
+++ b/src/pacman/log.c
@@ -38,7 +38,14 @@
 
 extern config_t *config;
 
-int neednl; /* for cleaner message output */
+int neednl = 0; /* for cleaner message output */
+int needpad = 0; /* pad blanks to terminal width */
+
+/* simple helper for needpad */
+void set_output_padding(int on)
+{
+	needpad = on;
+}
 
 /* Callback to handle notifications from the library
  */
@@ -103,6 +110,7 @@ void pm_fprintf(FILE *file, unsigned short line, char *fmt, ...)
 	va_list args;
 
 	char str[LOG_STR_LEN];
+	int len = 0;
 
 	if(neednl == 1 && line == NL) {
 		fprintf(file, "\n");
@@ -113,10 +121,28 @@ void pm_fprintf(FILE *file, unsigned short line, char *fmt, ...)
 	vsnprintf(str, LOG_STR_LEN, fmt, args);
 	va_end(args);
 
+	len = strlen(str);
+
+  if(needpad == 1 && str[len-1] == '\n') {
+		/* we want this removed so we can pad */
+		str[len-1] = ' ';
+		neednl = 1;
+	}
+
 	fprintf(file, str);
+	if(needpad == 1) {
+		unsigned int cols = getcols();
+		for(int i=len; i < cols; ++i) {
+			fprintf(file, " ");
+		}
+		if(neednl == 1) {
+			fprintf(file, "\n");
+			neednl = 0;
+		} else {
+			neednl = 1;
+		}
+	}
 	fflush(file);
-
-	neednl = (str[strlen(str)-1] == 10) ? 0 : 1;
 }
 
 /* Check verbosity option and, if set, print the
diff --git a/src/pacman/log.h b/src/pacman/log.h
index ff7fde50..01a507c3 100644
--- a/src/pacman/log.h
+++ b/src/pacman/log.h
@@ -36,6 +36,8 @@ enum {
 	CL  /* current line */
 };
 
+void set_output_padding(int on);
+
 /* callback to handle messages/notifications from pacman library */
 void cb_log(unsigned short level, char *msg);
 
diff --git a/src/pacman/trans.c b/src/pacman/trans.c
index 1b9295e1..ea6d8cd6 100644
--- a/src/pacman/trans.c
+++ b/src/pacman/trans.c
@@ -295,6 +295,12 @@ void cb_trans_progress(unsigned char event, char *pkgname, int percent, int howm
 		return;
 	}
 
+	if(percent == 0) {
+		set_output_padding(1); /* turn on output padding with ' ' */
+	} else if(percent == 100) {
+		set_output_padding(0); /* shut it off again */
+	}
+
 	if (!pkgname)
 		return;
 	if (percent > 100)
-- 
cgit v1.2.3-70-g09d2