[pacman-dev] [PATCHv2] avoid line wrapping if not in a tty or COLUMNS=0

Andrew Gregory andrew.gregory.8 at gmail.com
Mon Aug 11 12:42:25 EDT 2014


Scripts that parse pacman's output (like pacsearch) generally do not
want wrapped lines.

Signed-off-by: Andrew Gregory <andrew.gregory.8 at gmail.com>
---

* move isatty check to getcols_fd allowing COLUMNS to override
* allow COLUMNS=0

 src/pacman/util.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/pacman/util.c b/src/pacman/util.c
index 6a095fd..5686d92 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -46,7 +46,7 @@
 #include "conf.h"
 #include "callback.h"
 
-static int cached_columns = 0;
+static int cached_columns = -1;
 
 struct table_cell_t {
 	char *label;
@@ -158,13 +158,17 @@ static int flush_term_input(int fd)
 
 void columns_cache_reset(void)
 {
-	cached_columns = 0;
+	cached_columns = -1;
 }
 
 static int getcols_fd(int fd)
 {
 	int width = -1;
 
+	if(!isatty(fd)) {
+		return 0;
+	}
+
 #if defined(TIOCGSIZE)
 	struct ttysize win;
 	if(ioctl(fd, TIOCGSIZE, &win) == 0) {
@@ -189,20 +193,24 @@ unsigned short getcols(void)
 	const char *e;
 	int c = 0;
 
-	if(cached_columns > 0) {
+	if(cached_columns >= 0) {
 		return cached_columns;
 	}
 
 	e = getenv("COLUMNS");
-	if(e) {
-		c = strtol(e, NULL, 10);
+	if(e && *e) {
+		char *p = NULL;
+		c = strtol(e, &p, 10);
+		if(*p != '\0') {
+			c= -1;
+		}
 	}
 
-	if(c <= 0) {
+	if(c < 0) {
 		c = getcols_fd(STDOUT_FILENO);
 	}
 
-	if(c <= 0) {
+	if(c < 0) {
 		c = 80;
 	}
 
-- 
2.0.1


More information about the pacman-dev mailing list