[pacman-dev] [PATCH 1/2][RFC] pacman/util: add methods to create a divider for tables

Dave Reisner d at falconindy.com
Sat Aug 20 13:19:18 EDT 2011


This creates a dashed line underneath the header for each column in the
table, e.g.:

Name                        Old Version  New Version         Size
-----                       ------------ ------------       -----
at-spi2-core                             2.0.2-1         0.18 MiB
dconf                                    0.8.0-1         0.08 MiB
at-spi2-atk                              2.0.2-1         0.05 MiB
gsettings-desktop-schemas                3.0.1-2         0.04 MiB

Inspired-by: Pat Brisbin <pbrisbin at gmail.com>
Signed-off-by: Dave Reisner <dreisner at archlinux.org>
---
I think it's a nice touch, and it's not a hugely invasive patch.

 src/pacman/util.c |   45 +++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/src/pacman/util.c b/src/pacman/util.c
index 91625a1..0c83020 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -555,6 +555,40 @@ static alpm_list_t *table_create_format(const alpm_list_t *header,
 	return formats;
 }
 
+static char *string_create_filled(const unsigned int fill, size_t len)
+{
+	char *filled;
+
+	filled = malloc(len + 1);
+	if(!filled) {
+		return NULL;
+	}
+
+	memset(filled, fill, len);
+	filled[len] = '\0';
+
+	return filled;
+}
+
+static alpm_list_t *table_create_divider(const alpm_list_t *header,
+		const unsigned int fill)
+{
+	const alpm_list_t *i;
+	alpm_list_t *divider = NULL;
+
+	for(i = header; i; i = alpm_list_next(i)) {
+		const char *header = alpm_list_getdata(i);
+		char *div = string_create_filled(fill, string_length(header) + 1);
+		if(!div) {
+			FREELIST(divider);
+			return NULL;
+		}
+		divider = alpm_list_add(divider, div);
+	}
+
+	return divider;
+}
+
 /** Displays the list in table format
  *
  * @param title the tables title
@@ -569,7 +603,7 @@ int table_display(const char *title, const alpm_list_t *header,
 		const alpm_list_t *rows)
 {
 	const alpm_list_t *i;
-	alpm_list_t *formats;
+	alpm_list_t *formats, *divider;
 
 	if(rows == NULL || header == NULL) {
 		return 0;
@@ -580,17 +614,24 @@ int table_display(const char *title, const alpm_list_t *header,
 		return -1;
 	}
 
+	divider = table_create_divider(header, '-');
+	if(divider == NULL) {
+		FREELIST(formats);
+		return -1;
+	}
+
 	if(title != NULL) {
 		printf("%s\n\n", title);
 	}
 
 	table_print_line(header, formats);
-	printf("\n");
+	table_print_line(divider, formats);
 
 	for(i = rows; i; i = alpm_list_next(i)) {
 		table_print_line(alpm_list_getdata(i), formats);
 	}
 
+	FREELIST(divider);
 	FREELIST(formats);
 	return 0;
 }
-- 
1.7.6



More information about the pacman-dev mailing list