[pacman-dev] [PATCH] getcols: return 0 when not attached to a tty
Dave Reisner
d at falconindy.com
Mon Aug 11 10:47:47 EDT 2014
On Mon, Aug 11, 2014 at 09:51:19AM -0400, Andrew Gregory wrote:
> 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>
> ---
I agree there's a bug here, but this doesn't seem like the right fix...
> src/pacman/util.c | 11 ++++++++---
> 1 file changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/src/pacman/util.c b/src/pacman/util.c
> index 6a095fd..de103b6 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,7 +158,7 @@ static int flush_term_input(int fd)
>
> void columns_cache_reset(void)
> {
> - cached_columns = 0;
> + cached_columns = -1;
> }
>
> static int getcols_fd(int fd)
> @@ -189,10 +189,15 @@ unsigned short getcols(void)
> const char *e;
> int c = 0;
>
> - if(cached_columns > 0) {
> + if(cached_columns >= 0) {
> return cached_columns;
> }
>
> + if(!isatty(STDOUT_FILENO)) {
> + cached_columns = 0;
> + return 0;
> + }
> +
Doing this means that you can no longer use COLUMNS to control output
width when not attached to a TTY.
> e = getenv("COLUMNS");
> if(e) {
> c = strtol(e, NULL, 10);
What isn't shown in the context here is that columns_fd is called next
to determine the width of the terminal. I think we should add the isatty
check there and then accept '0' as a valid return from that function.
> --
> 2.0.1
>
More information about the pacman-dev
mailing list