[pacman-dev] [PATCH] pacman/util.c: support terminals with unknown width

Dave Reisner d at falconindy.com
Tue Jun 14 09:26:12 EDT 2011


Fall back on the default passed to getcols when we encounter a device
attached to stdout that reports itself as being a tty but which has no
width. This is only (currently) known to affect serial consoles, which
have a legitimate size of 80x24.

Signed-off-by: Dave Reisner <d at falconindy.com>
---
So this seems to solve the issues with serial consoles reporting crap widths
but still keep real terminals happy. All calls to getcols now supply a default
of 80 (which I guess makes sense), but we hardcode the width of 0 in the case
of stdout not being a tty (pager/grep/sed/etc).

Note that this only applies on top of Dan's previous patch which fixes
list_display (after he broke it).

d

 src/pacman/callback.c |    4 ++--
 src/pacman/util.c     |   16 +++++++++++-----
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/src/pacman/callback.c b/src/pacman/callback.c
index 4ac3b56..d956677 100644
--- a/src/pacman/callback.c
+++ b/src/pacman/callback.c
@@ -355,7 +355,7 @@ void cb_trans_progress(pmtransprog_t event, const char *pkgname, int percent,
 	int len, wclen, wcwid, padwid;
 	wchar_t *wcstr;
 
-	const int cols = getcols(0);
+	const int cols = getcols(80);
 
 	if(config->noprogressbar || cols == 0) {
 		return;
@@ -504,7 +504,7 @@ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total)
 	const char *rate_label, *xfered_label;
 	int file_percent = 0, total_percent = 0;
 
-	const int cols = getcols(0);
+	const int cols = getcols(80);
 
 	if(config->noprogressbar || cols == 0 || file_total == -1) {
 		if(file_xfered == 0) {
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 9709011..a112d1c 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -118,18 +118,24 @@ static int flush_term_input(void) {
 /* gets the current screen column width */
 int getcols(int def)
 {
+	int termwidth = -1;
+
+	if(!isatty(fileno(stdout))) {
+		return 0;
+	}
+
 #ifdef TIOCGSIZE
 	struct ttysize win;
 	if(ioctl(1, TIOCGSIZE, &win) == 0) {
-		return win.ts_cols;
+		termwidth = win.ts_cols;
 	}
 #elif defined(TIOCGWINSZ)
 	struct winsize win;
 	if(ioctl(1, TIOCGWINSZ, &win) == 0) {
-		return win.ws_col;
+		termwidth = win.ws_col;
 	}
 #endif
-	return def;
+	return termwidth <= 0 ? def : termwidth;
 }
 
 /* does the same thing as 'rm -rf' */
@@ -225,7 +231,7 @@ void indentprint(const char *str, int indent)
 	wchar_t *wcstr;
 	const wchar_t *p;
 	int len, cidx;
-	const int cols = getcols(0);
+	const int cols = getcols(80);
 
 	if(!str) {
 		return;
@@ -577,7 +583,7 @@ void list_display(const char *title, const alpm_list_t *list)
 	if(!list) {
 		printf("%s\n", _("None"));
 	} else {
-		const int maxcols = getcols(0);
+		const int maxcols = getcols(80);
 		int cols = len;
 		const char *str = alpm_list_getdata(list);
 		printf("%s", str);
-- 
1.7.5.4



More information about the pacman-dev mailing list