Re: Proposed patch - psql wraps at window width

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Bryce Nesbitt <bryce2(at)obviously(dot)com>
Cc: pgsql-patches(at)postgresql(dot)org, heikki(at)enterprisedb(dot)com
Subject: Re: Proposed patch - psql wraps at window width
Date: 2008-04-17 02:49:00
Message-ID: 200804170249.m3H2n0f05297@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-patches

Bryce Nesbitt wrote:
> I've attached a patch, against current 8.4 cvs, which optionally sets a
> maximum width for psql output:
>
> # \pset format aligned-wrapped
> # \pset border 2
> # select * from distributors order by did;
> +------+--------------------+---------------------+---------------+
> | did | name | descr | long_col_name |
> +------+--------------------+---------------------+---------------+
> | 1 | Food fish and wine | default | |
> | 2 | Cat Food Heaven 2 | abcdefghijklmnopqrs ! |
> | | | tuvwxyz | |
> | 3 | Cat Food Heaven 3 | default | |
> | 10 | Lah | default | |
> | 12 | name | line one | |
> | 2892 ! short name | short | |
> | 8732 | | | |
> +------+--------------------+---------------------+---------------+
> (8 rows)
>
> The interactive terminal column width comes from
> char * temp = getenv("COLUMNS");
> Which has the strong advantage of great simplicity and portability. But
> it may not be 1000% universal. If $COLUMNS is not defined, the code
> bails to assuming an infinitely wide terminal.
>
> I will also backport this to Postgres 8.1, for my own use. Though the
> code is almost totally different in structure.

I spent time reviewing your patch --- quite impressive. I have attached
and updated version with mostly stylistic changes.

In testing I found the regression tests were failing because of a divide
by zero error (fixed), and a missing case where the column delimiter has
to be ":". In fact I now see all your line continuation cases using ":"
rather than "!". It actually looks better --- "!" was too close to "|"
to be easily recognized. (Did you update your patch to use ":". I
didn't see "!" in your patch.)

I have added an XXX comment questioning whether the loop to find the
column to wrap is inefficient because it potentially loops over the
length of the longest column and for each character loops over the
number of columns. Not sure if that is a problem.

I checked the use of COLUMNS and it seems bash updates the environment
variable when a window is resized. I added ioctl(TIOCGWINSZ) if COLUMNS
isn't set. We already had a call in print.c for detecting the
number of rows on the screen to determine if the pager should
be used. Seems COLUMNS should take precedence over ioctl(), right?
I don't think Win32 supports that ioctl(), does it?

I added some comments and clarified some variable names. I also renamed
the option to a shorter "wrapped". I added documentation too.

For testers compare:

\df

with:

\pset format wrap
\df

Impressive!

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

Attachment Content-Type Size
unknown_filename text/plain 26.9 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bryce Nesbitt 2008-04-17 04:12:47 Re: Proposed patch - psql wraps at window width
Previous Message Robert Treat 2008-04-17 02:18:53 Re: MERGE SQL Statement

Browse pgsql-patches by date

  From Date Subject
Next Message Bryce Nesbitt 2008-04-17 04:12:47 Re: Proposed patch - psql wraps at window width
Previous Message Alvaro Herrera 2008-04-16 23:53:56 Re: printTable API (was: Show INHERIT in \du)