>From caa94f32a0b2434115788f3467203415d406efff Mon Sep 17 00:00:00 2001
From: Justin Lampley <jrlampley@gmail.com>
Date: Thu, 15 Nov 2007 00:19:45 -0500
Subject: [PATCH] Add VerboseListInfo Option

This option replaces ShowSize. it adds the ability
to show the old package version during upgrades as
well as displaying the size of individual packages
during upgrades and searches.



Signed-off-by: Justin Lampley <jrlampley@gmail.com>
---
 doc/pacman.conf.5.txt |   10 +++-
 src/pacman/conf.h     |    3 +-
 src/pacman/pacman.c   |    6 +-
 src/pacman/query.c    |    4 +-
 src/pacman/sync.c     |    2 +-
 src/pacman/util.c     |  107 +++++++++++++++++++++++++++++++++++++++++++-----
 src/pacman/util.h     |    1 +
 7 files changed, 111 insertions(+), 22 deletions(-)

diff --git a/doc/pacman.conf.5.txt b/doc/pacman.conf.5.txt
index 2a0f88a..682a91c 100644
--- a/doc/pacman.conf.5.txt
+++ b/doc/pacman.conf.5.txt
@@ -106,9 +106,6 @@ Options
 	Log action messages through syslog(). This will insert log entries into
 	``/var/log/messages'' or equivalent.
 
-*ShowSize*::
-	Display the size of individual packages for '\--sync' and '\--query' modes.
-
 *UseDelta*::
 	Download delta files instead of complete packages if possible.  Requires
 	the xdelta program to be installed.
@@ -118,6 +115,13 @@ Options
 	and completed percentage of the entire download list list rather
 	than the percent of each individual download target. The progress
 	bar is still based solely on the current file download.
+	
+*VerboseListInfo*::
+	When listing targets to install in '\--sync' and '\--query' upgrade modes, 
+	display the old version as well as the size of the individual packages.
+	When searching in '\--sync' and '\--query' modes, display the size of the 
+	packages.
+	
 
 Repository Sections
 -------------------
diff --git a/src/pacman/conf.h b/src/pacman/conf.h
index b7844d7..f3ef5b6 100644
--- a/src/pacman/conf.h
+++ b/src/pacman/conf.h
@@ -64,7 +64,8 @@ typedef struct __config_t {
 	/* conf file options */
 	unsigned short chomp; /* I Love Candy! */
 	unsigned short usecolor; /* enable colorful output */
-	unsigned short showsize; /* show individual package sizes */
+	unsigned short verboselistinfo; /* show old package version as well as size
+										 when displaying the target list*/
 	unsigned short totaldownload; /* When downloading, display the amount
 	                                 downloaded, rate, ETA, and percent
 	                                 downloaded of the total download list */
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index 31d31da..3b333b6 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -583,9 +583,9 @@ static int _parseconfig(const char *file, const char *givensection,
 				} else if(strcmp(key, "UseColor") == 0 || strcmp(upperkey, "USECOLOR") == 0) {
 					config->usecolor = 1;
 					pm_printf(PM_LOG_DEBUG, "config: usecolor\n");
-				} else if(strcmp(key, "ShowSize") == 0 || strcmp(upperkey, "SHOWSIZE") == 0) {
-					config->showsize = 1;
-					pm_printf(PM_LOG_DEBUG, "config: showsize\n");
+				} else if(strcmp(key, "VerboseListInfo") == 0 || strcmp(upperkey, "VERBOSELISTINFO") == 0) {
+					config->verboselistinfo = 1;
+					pm_printf(PM_LOG_DEBUG, "config: verboselistinfo\n");
 				} else if(strcmp(key, "UseDelta") == 0 || strcmp(upperkey, "USEDELTA") == 0) {
 					alpm_option_set_usedelta(1);
 					pm_printf(PM_LOG_DEBUG, "config: usedelta\n");
diff --git a/src/pacman/query.c b/src/pacman/query.c
index 9ede00a..3ea740e 100644
--- a/src/pacman/query.c
+++ b/src/pacman/query.c
@@ -148,8 +148,8 @@ static int query_search(alpm_list_t *targets)
 
 		printf("local/%s %s", alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg));
 
-		/* print the package size with the output if ShowSize option set */
-		if(config->showsize) {
+		/* print the package size with the output if VerboseTargetList option set */
+		if(config->verboselistinfo) {
 			/* Convert byte size to MB */
 			double mbsize = (double)alpm_pkg_get_size(pkg) / (1024.0 * 1024.0);
 
diff --git a/src/pacman/sync.c b/src/pacman/sync.c
index 5f1072f..cf7635c 100644
--- a/src/pacman/sync.c
+++ b/src/pacman/sync.c
@@ -196,7 +196,7 @@ static int sync_search(alpm_list_t *syncs, alpm_list_t *targets)
 					alpm_pkg_get_version(pkg));
 
 			/* print the package size with the output if ShowSize option set */
-			if(config->showsize) {
+			if(config->verboselistinfo) {
 				/* Convert byte size to MB */
 				double mbsize = alpm_pkg_get_size(pkg) / (1024.0 * 1024.0);
 
diff --git a/src/pacman/util.c b/src/pacman/util.c
index a0402ab..8011e48 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -357,6 +357,45 @@ void list_display(const char *title, const alpm_list_t *list)
 	}
 }
 
+/** Displays the formated list
+ *
+ * @param title  the title of the list
+ * @param rows  a list of all the rows, with columns seperated by commas
+ * @param formatstring  the format that the rows should be printed in, with the formats for 
+ * for each column seperate by commas.
+ *
+ */
+void formatted_list_display(const char *title, const alpm_list_t *rows, const char *formatstring) {
+	alpm_list_t *currow;
+	alpm_list_t *formats = strsplit(formatstring,',');
+	currow = alpm_list_first(rows);
+	printf("%s ", title);
+	if(rows && formats) {
+		printf("\n\n");
+		do {
+			alpm_list_t *columns = strsplit(alpm_list_getdata(currow),',');
+			alpm_list_t *curformat = alpm_list_first(formats);
+			alpm_list_t *curcolumn = alpm_list_first(columns);
+	
+			do {
+				printf(alpm_list_getdata(curformat),alpm_list_getdata(curcolumn));
+	
+				curcolumn = alpm_list_next(curcolumn);
+				curformat = alpm_list_next(curformat);
+			} while (curcolumn != alpm_list_first(columns) && curcolumn != NULL
+						&& curformat != alpm_list_first(formats) && curformat != NULL);
+			printf("\n");
+			FREELIST(columns);
+			currow = alpm_list_next(currow);
+		} while (currow != alpm_list_first(rows) && currow != NULL);
+	}
+	else {
+		printf(_("None\n"));
+	}
+	
+	FREELIST(formats);
+}
+
 /* Display a list of transaction targets.
  * `pkgs` should be a list of pmsyncpkg_t's,
  * retrieved from a transaction object
@@ -364,9 +403,12 @@ void list_display(const char *title, const alpm_list_t *list)
 /* TODO move to output.c? or just combine util and output */
 void display_targets(const alpm_list_t *syncpkgs, pmdb_t *db_local)
 {
-	char *str;
+	char *str, *format, *oldpkgver, *pkgsize;
 	const alpm_list_t *i, *j;
 	alpm_list_t *targets = NULL, *to_remove = NULL;
+	int lenpkgname = 0, lenpkgver = 0, lenoldpkgver = 0, lenpkgsize = 0;
+	/* padding to use for columns when verboselistinfo is set */
+	int padding = 5;
 	/* TODO these are some messy variable names */
 	unsigned long isize = 0, rsize = 0, dispsize = 0, dlsize = 0;
 	double mbisize = 0.0, mbrsize = 0.0, mbdispsize = 0.0, mbdlsize = 0.0;
@@ -374,7 +416,8 @@ void display_targets(const alpm_list_t *syncpkgs, pmdb_t *db_local)
 	for(i = syncpkgs; i; i = alpm_list_next(i)) {
 		pmsyncpkg_t *sync = alpm_list_getdata(i);
 		pmpkg_t *pkg = alpm_sync_get_pkg(sync);
-
+		pmpkg_t *oldpkg = alpm_sync_get_data(sync);
+		
 		/* If this sync record is a replacement, the data member contains
 		 * a list of packages to be removed due to the package that is being
 		 * installed. */
@@ -396,17 +439,45 @@ void display_targets(const alpm_list_t *syncpkgs, pmdb_t *db_local)
 		dlsize += alpm_pkg_download_size(pkg, db_local);
 		isize += alpm_pkg_get_isize(pkg);
 
-		/* print the package size with the output if ShowSize option set */
-		if(config->showsize) {
-			/* Convert byte size to MB */
+		/* if we want verbose target list information create the rows necessary to
+		 *  pass to formatted_list_display */
+		if(config->verboselistinfo) {
+			/* if it is an upgrade, print the old version of the package, else we 
+			 * assume it is a new package */
+			if(alpm_sync_get_type(sync) == PM_SYNC_TYPE_UPGRADE) {
+					asprintf(&oldpkgver, "%s",alpm_pkg_get_version(oldpkg));
+			}
+			else
+				asprintf(&oldpkgver, "%s",_("New"));
+			
+			/* get the size of the package */
 			mbdispsize = dispsize / (1024.0 * 1024.0);
-
-			asprintf(&str, "%s-%s [%.2f MB]", alpm_pkg_get_name(pkg),
-					alpm_pkg_get_version(pkg), mbdispsize);
-		} else {
-			asprintf(&str, "%s-%s", alpm_pkg_get_name(pkg),
-					alpm_pkg_get_version(pkg));
+			asprintf(&pkgsize,"%.2f MB",mbdispsize);
+			
+			asprintf(&str,"%s,%s,%s,%s",alpm_pkg_get_name(pkg),oldpkgver,
+							alpm_pkg_get_version(pkg), pkgsize);
+							
+			/* check the column sizes of the newly added row */
+			if(strlen(alpm_pkg_get_name(pkg)) > lenpkgname) {
+				lenpkgname = strlen(alpm_pkg_get_name(pkg));
+			}
+			if(strlen(oldpkgver) > lenoldpkgver) {
+				lenoldpkgver = strlen(oldpkgver);
+			}
+			if(strlen(alpm_pkg_get_version(pkg)) > lenpkgver) {
+				lenpkgver = strlen(alpm_pkg_get_version(pkg));
+			}
+			if(strlen(pkgsize) > lenpkgsize) {
+				lenpkgsize = strlen(pkgsize);
+			}
+			
+			free(oldpkgver);
+			free(pkgsize);
 		}
+		else{
+			asprintf(&str, "%s-%s", alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg));
+		}
+ 		
 		targets = alpm_list_add(targets, str);
 	}
 
@@ -427,7 +498,19 @@ void display_targets(const alpm_list_t *syncpkgs, pmdb_t *db_local)
 		printf("\n");
 	}
 
-	list_display(_("Targets:"), targets);
+	if(config->verboselistinfo) {
+		/* create the format for the list */
+		asprintf(&format,"%%-%ds,%%-%ds,%%-%ds,%%%ds", lenpkgname+padding, 
+						lenoldpkgver+padding, lenpkgver+padding, lenpkgsize);
+		
+		formatted_list_display(_("Targets:"),targets,format);
+		
+		free(format);
+	}
+	else {
+		list_display(_("Targets:"), targets);
+	}
+		
 	printf("\n");
 
 	printf(_("Total Download Size:    %.2f MB\n"), mbdlsize);
diff --git a/src/pacman/util.h b/src/pacman/util.h
index 931b456..b94ba43 100644
--- a/src/pacman/util.h
+++ b/src/pacman/util.h
@@ -48,6 +48,7 @@ char *strtrim(char *str);
 char *strreplace(const char *str, const char *needle, const char *replace);
 alpm_list_t *strsplit(const char *str, const char splitchar);
 void list_display(const char *title, const alpm_list_t *list);
+void formatted_list_display(const char *title, const alpm_list_t *rows, const char *formatstring);
 void display_targets(const alpm_list_t *syncpkgs, pmdb_t *db_local);
 int yesno(char *fmt, ...);
 int pm_printf(pmloglevel_t level, const char *format, ...) __attribute__((format(printf,2,3)));
-- 
1.5.3.5

