avoid line wrapping if not in a tty or COLUMNS=0

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

Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
This commit is contained in:
Andrew Gregory 2014-08-11 09:43:12 -04:00 committed by Allan McRae
parent 50296576d0
commit dc339faf6a

View file

@ -46,7 +46,7 @@
#include "conf.h" #include "conf.h"
#include "callback.h" #include "callback.h"
static int cached_columns = 0; static int cached_columns = -1;
struct table_cell_t { struct table_cell_t {
char *label; char *label;
@ -158,13 +158,17 @@ static int flush_term_input(int fd)
void columns_cache_reset(void) void columns_cache_reset(void)
{ {
cached_columns = 0; cached_columns = -1;
} }
static int getcols_fd(int fd) static int getcols_fd(int fd)
{ {
int width = -1; int width = -1;
if(!isatty(fd)) {
return 0;
}
#if defined(TIOCGSIZE) #if defined(TIOCGSIZE)
struct ttysize win; struct ttysize win;
if(ioctl(fd, TIOCGSIZE, &win) == 0) { if(ioctl(fd, TIOCGSIZE, &win) == 0) {
@ -187,22 +191,26 @@ static int getcols_fd(int fd)
unsigned short getcols(void) unsigned short getcols(void)
{ {
const char *e; const char *e;
int c = 0; int c = -1;
if(cached_columns > 0) { if(cached_columns >= 0) {
return cached_columns; return cached_columns;
} }
e = getenv("COLUMNS"); e = getenv("COLUMNS");
if(e) { if(e && *e) {
c = strtol(e, NULL, 10); char *p = NULL;
c = strtol(e, &p, 10);
if(*p != '\0') {
c= -1;
}
} }
if(c <= 0) { if(c < 0) {
c = getcols_fd(STDOUT_FILENO); c = getcols_fd(STDOUT_FILENO);
} }
if(c <= 0) { if(c < 0) {
c = 80; c = 80;
} }