[pacman-dev] [PATCH] Add linebreak parameter to list_display

Nagy Gabor ngaba at bibl.u-szeged.hu
Thu Jul 31 10:13:19 EDT 2008


From e63ea25813e65cbb6ad1f925d71720527cf7ea19 Mon Sep 17 00:00:00 2001
From: Nagy Gabor <ngaba at bibl.u-szeged.hu>
Date: Thu, 31 Jul 2008 15:58:25 +0200
Subject: [PATCH] Add linebreak parameter to list_display

If linebreak is not set, list_display is identical to the old one:
Targets: foo  bar  baz

If linebreak is set, list_display put every list member to its own line:
Optdepends: foo
            bar
            baz

Since optdepends are expected in "foo: feature" format, Optdepends are
listed with linebreak set.

Signed-off-by: Nagy Gabor <ngaba at bibl.u-szeged.hu>
---
 src/pacman/package.c |   16 ++++++------
 src/pacman/pacman.c  |    2 +-
 src/pacman/remove.c  |    2 +-
 src/pacman/sync.c    |    4 +-
 src/pacman/util.c    |   61 +++++++++++++++++++++++++++++++------------------
 src/pacman/util.h    |    2 +-
 6 files changed, 51 insertions(+), 36 deletions(-)

diff --git a/src/pacman/package.c b/src/pacman/package.c
index 87ffd98..17887e7 100644
--- a/src/pacman/package.c
+++ b/src/pacman/package.c
@@ -91,18 +91,18 @@ void dump_pkg_full(pmpkg_t *pkg, int level)
 	string_display(_("Name           :"), alpm_pkg_get_name(pkg));
 	string_display(_("Version        :"), alpm_pkg_get_version(pkg));
 	string_display(_("URL            :"), alpm_pkg_get_url(pkg));
-	list_display(_("Licenses       :"), alpm_pkg_get_licenses(pkg));
-	list_display(_("Groups         :"), alpm_pkg_get_groups(pkg));
-	list_display(_("Provides       :"), alpm_pkg_get_provides(pkg));
-	list_display(_("Depends On     :"), depstrings);
-	list_display(_("Optional Deps  :"), alpm_pkg_get_optdepends(pkg));
+	list_display(_("Licenses       :"), alpm_pkg_get_licenses(pkg), 0);
+	list_display(_("Groups         :"), alpm_pkg_get_groups(pkg), 0);
+	list_display(_("Provides       :"), alpm_pkg_get_provides(pkg), 0);
+	list_display(_("Depends On     :"), depstrings, 0);
+	list_display(_("Optional Deps  :"), alpm_pkg_get_optdepends(pkg), 1);
 	/* Only applicable if installed */
 	if(level > 0) {
-		list_display(_("Required By    :"), requiredby);
+		list_display(_("Required By    :"), requiredby, 0);
 		FREELIST(requiredby);
 	}
-	list_display(_("Conflicts With :"), alpm_pkg_get_conflicts(pkg));
-	list_display(_("Replaces       :"), alpm_pkg_get_replaces(pkg));
+	list_display(_("Conflicts With :"), alpm_pkg_get_conflicts(pkg), 0);
+	list_display(_("Replaces       :"), alpm_pkg_get_replaces(pkg), 0);
 	if(level < 0) {
 		printf(_("Download Size  : %6.2f K\n"),
 			(float)alpm_pkg_get_size(pkg) / 1024.0);
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index 445365e..8b8698b 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -872,7 +872,7 @@ int main(int argc, char *argv[])
 		printf("\n");
 		printf("Lock File : %s\n", alpm_option_get_lockfile());
 		printf("Log File  : %s\n", alpm_option_get_logfile());
-		list_display("Targets   :", pm_targets);
+		list_display("Targets   :", pm_targets, 0);
 	}
 
 	/* Opening local database */
diff --git a/src/pacman/remove.c b/src/pacman/remove.c
index f091fa4..f56ac2e 100644
--- a/src/pacman/remove.c
+++ b/src/pacman/remove.c
@@ -74,7 +74,7 @@ int pacman_remove(alpm_list_t *targets)
 						pkgnames = alpm_list_add(pkgnames, (void *)alpm_pkg_get_name(pkg));
 					}
 					printf(_(":: group %s:\n"), targ);
-					list_display("   ", pkgnames);
+					list_display("   ", pkgnames, 0);
 					int all = yesno(1, _("    Remove whole content?"));
 					for(p = pkgnames; p; p = alpm_list_next(p)) {
 						char *pkgn = alpm_list_getdata(p);
diff --git a/src/pacman/sync.c b/src/pacman/sync.c
index 8d0c529..ae3f523 100644
--- a/src/pacman/sync.c
+++ b/src/pacman/sync.c
@@ -603,7 +603,7 @@ static int sync_trans(alpm_list_t *targets)
 							pkgnames = alpm_list_add(pkgnames,
 									(char*)alpm_pkg_get_name(k->data));
 						}
-						list_display("   ", pkgnames);
+						list_display("   ", pkgnames, 0);
 						if(yesno(1, _(":: Install whole content?"))) {
 							for(k = pkgnames; k; k = alpm_list_next(k)) {
 								targets = alpm_list_add(targets, strdup(alpm_list_getdata(k)));
@@ -816,7 +816,7 @@ int pacman_sync(alpm_list_t *targets)
 		alpm_list_t *packages = syncfirst();
 		if(packages) {
 			printf(_(":: The following packages should be upgraded first :\n"));
-			list_display("   ", packages);
+			list_display("   ", packages, 0);
 			if(yesno(1, _(":: Do you want to cancel the current operation\n"
 							":: and upgrade these packages now?"))) {
 				FREELIST(targs);
diff --git a/src/pacman/util.c b/src/pacman/util.c
index ff5429e..17aaa1f 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -442,7 +442,7 @@ void string_display(const char *title, const char *string)
 	printf("\n");
 }
 
-void list_display(const char *title, const alpm_list_t *list)
+void list_display(const char *title, const alpm_list_t *list, int linebreak)
 {
 	const alpm_list_t *i;
 	int cols, len;
@@ -453,7 +453,7 @@ void list_display(const char *title, const alpm_list_t *list)
 		len = strlen(title) + 1;
 		wcstr = calloc(len, sizeof(wchar_t));
 		len = mbstowcs(wcstr, title, len);
-		len = wcswidth(wcstr, len);
+		len = wcswidth(wcstr, len) + 1;
 		free(wcstr);
 		printf("%s ", title);
 	} else {
@@ -461,29 +461,44 @@ void list_display(const char *title, const alpm_list_t *list)
 	}
 
 	if(list) {
-		for(i = list, cols = len; i; i = alpm_list_next(i)) {
-			char *str = alpm_list_getdata(i);
-			/* s goes from # bytes -> # chars -> # cols */
-			int s = strlen(str) + 1;
-			wcstr = calloc(s, sizeof(wchar_t));
-			s = mbstowcs(wcstr, str, s);
-			s = wcswidth(wcstr, s);
-			free(wcstr);
-			/* two additional spaces are added to the length */
-			s += 2;
-			int maxcols = getcols();
-			if(s + cols >= maxcols) {
-				int i;
-				cols = len;
-				printf("\n");
-				for (i = 0; i <= len; ++i) {
+		if(linebreak) {
+			/* Print the first element */
+			indentprint((const char *) alpm_list_getdata(list), len);
+			printf("\n");
+			/* Print the rest */
+			for(i = alpm_list_next(list); i; i = alpm_list_next(i)) {
+				int j;
+				for(j = 1; j <= len; j++) {
 					printf(" ");
 				}
+				indentprint((const char *) alpm_list_getdata(i), len);
+				printf("\n");
+			}
+		} else {
+			for(i = list, cols = len; i; i = alpm_list_next(i)) {
+				char *str = alpm_list_getdata(i);
+				/* s goes from # bytes -> # chars -> # cols */
+				int s = strlen(str) + 1;
+				wcstr = calloc(s, sizeof(wchar_t));
+				s = mbstowcs(wcstr, str, s);
+				s = wcswidth(wcstr, s);
+				free(wcstr);
+				/* two additional spaces are added to the length */
+				s += 2;
+				int maxcols = getcols();
+				if(s + cols > maxcols) {
+					int j;
+					cols = len;
+					printf("\n");
+					for (j = 1; j <= len; j++) {
+						printf(" ");
+					}
+				}
+				printf("%s  ", str);
+				cols += s;
 			}
-			printf("%s  ", str);
-			cols += s;
+			printf("\n");
 		}
-		printf("\n");
 	} else {
 		printf(_("None\n"));
 	}
@@ -529,7 +544,7 @@ void display_targets(const alpm_list_t *pkgs, int install)
 
 	if(install) {
 		asprintf(&str, _("Targets (%d):"), alpm_list_count(targets));
-		list_display(str, targets);
+		list_display(str, targets, 0);
 		free(str);
 		printf("\n");
 
@@ -537,7 +552,7 @@ void display_targets(const alpm_list_t *pkgs, int install)
 		printf(_("Total Installed Size:   %.2f MB\n"), mbisize);
 	} else {
 		asprintf(&str, _("Remove (%d):"), alpm_list_count(targets));
-		list_display(str, targets);
+		list_display(str, targets, 0);
 		free(str);
 		printf("\n");
 
diff --git a/src/pacman/util.h b/src/pacman/util.h
index 2ddc1b5..0a1ac2c 100644
--- a/src/pacman/util.h
+++ b/src/pacman/util.h
@@ -50,7 +50,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 string_display(const char *title, const char *string);
-void list_display(const char *title, const alpm_list_t *list);
+void list_display(const char *title, const alpm_list_t *list, int linebreak);
 void display_targets(const alpm_list_t *pkgs, int install);
 void display_synctargets(const alpm_list_t *syncpkgs);
 int yesno(short preset, char *fmt, ...);
-- 
1.5.6.4





More information about the pacman-dev mailing list