[pacman-dev] [PATCH 4/6] Refactor display_targets for readability

Jakob Gruber jakob.gruber at gmail.com
Mon Feb 28 11:40:00 EST 2011


Row handling is moved to its own function in preparation for verbose
package lists.

Signed-off-by: Jakob Gruber <jakob.gruber at gmail.com>
---
 src/pacman/util.c |   48 ++++++++++++++++++++++++++----------------------
 1 files changed, 26 insertions(+), 22 deletions(-)

diff --git a/src/pacman/util.c b/src/pacman/util.c
index 23b95c9..ba64a77 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -498,11 +498,27 @@ void list_display_linebreak(const char *title, const alpm_list_t *list)
 		}
 	}
 }
+
+/* returns package info as a string */
+static char *create_list_element(pmpkg_t *pkg)
+{
+	char *ret;
+	const char *label;
+	double size;
+	const char *pkgstr = config->showsize ? "%s-%s [%.2f %s]" : "%s-%s";
+
+	size = humanize_size(alpm_pkg_get_size(pkg), 0, 1, &label);
+	pm_asprintf(&ret, pkgstr, alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg),
+			size, label);
+
+	return(ret);
+}
+
 /* prepare a list of pkgs to display */
 void display_targets(const alpm_list_t *pkgs, int install)
 {
 	char *str;
-	const char *label;
+	const char *title, *label;
 	double size;
 	const alpm_list_t *i;
 	off_t isize = 0, dlsize = 0;
@@ -512,7 +528,6 @@ void display_targets(const alpm_list_t *pkgs, int install)
 		return;
 	}
 
-	printf("\n");
 	for(i = pkgs; i; i = alpm_list_next(i)) {
 		pmpkg_t *pkg = alpm_list_getdata(i);
 
@@ -521,24 +536,17 @@ void display_targets(const alpm_list_t *pkgs, int install)
 		}
 		isize += alpm_pkg_get_isize(pkg);
 
-		/* print the package size with the output if ShowSize option set */
-		if(config->showsize) {
-			size = humanize_size(alpm_pkg_get_size(pkg), 0, 1, &label);
-			pm_asprintf(&str, "%s-%s [%.2f %s]", alpm_pkg_get_name(pkg),
-					alpm_pkg_get_version(pkg), size, label);
-		} else {
-			pm_asprintf(&str, "%s-%s", alpm_pkg_get_name(pkg),
-					alpm_pkg_get_version(pkg));
-		}
-		targets = alpm_list_add(targets, str);
+		targets = alpm_list_add(targets, create_list_element(pkg));
 	}
 
-	if(install) {
-		pm_asprintf(&str, _("Targets (%d):"), alpm_list_count(targets));
-		list_display(str, targets);
-		free(str);
-		printf("\n");
+	title = install ? _("Targets (%d):") : _("Remove (%d):");
+	pm_asprintf(&str, title, alpm_list_count(pkgs));
+
+	printf("\n");
+	list_display(str, targets);
+	printf("\n");
 
+	if(install) {
 		size = humanize_size(dlsize, 'M', 1, &label);
 		printf(_("Total Download Size:    %.2f %s\n"), size, label);
 		if(!(config->flags & PM_TRANS_FLAG_DOWNLOADONLY)) {
@@ -546,15 +554,11 @@ void display_targets(const alpm_list_t *pkgs, int install)
 			printf(_("Total Installed Size:   %.2f %s\n"), size, label);
 		}
 	} else {
-		pm_asprintf(&str, _("Remove (%d):"), alpm_list_count(targets));
-		list_display(str, targets);
-		free(str);
-		printf("\n");
-
 		size = humanize_size(isize, 'M', 1, &label);
 		printf(_("Total Removed Size:   %.2f %s\n"), size, label);
 	}
 
+	free(str);
 	FREELIST(targets);
 }
 
-- 
1.7.4.1



More information about the pacman-dev mailing list