Further tweaking of print_aligned_vertical().

Don't force the data width to extend all the way to the right margin if it
doesn't need to.  This reverts the behavior in non-wrapping cases to be
what it was in 9.4.  Also, make the logic that ensures the data line width
is at least equal to the record-header line width a little less obscure.

In passing, avoid possible calculation of log10(0).  Probably that's
harmless, given the lack of field complaints, but it seems risky:
conversion of NaN to an integer isn't well defined.
This commit is contained in:
Tom Lane 2015-12-01 14:47:13 -05:00
parent db4a5cfc76
commit 95708e1d8e
2 changed files with 55 additions and 41 deletions

View File

@ -1265,7 +1265,7 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
/* /*
* Deal with the pager here instead of in printTable(), because we could * Deal with the pager here instead of in printTable(), because we could
* get here via print_aligned_text() in expanded auto mode, and so we have * get here via print_aligned_text() in expanded auto mode, and so we have
* to recalcuate the pager requirement based on vertical output. * to recalculate the pager requirement based on vertical output.
*/ */
IsPagerNeeded(cont, 0, true, &fout, &is_pager); IsPagerNeeded(cont, 0, true, &fout, &is_pager);
@ -1400,7 +1400,8 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
/* Determine width required for record header lines */ /* Determine width required for record header lines */
if (!opt_tuples_only) if (!opt_tuples_only)
{ {
rwidth = 1 + log10(cont->nrows); if (cont->nrows > 0)
rwidth = 1 + (int) log10(cont->nrows);
if (opt_border == 0) if (opt_border == 0)
rwidth += 9; /* "* RECORD " */ rwidth += 9; /* "* RECORD " */
else if (opt_border == 1) else if (opt_border == 1)
@ -1412,33 +1413,46 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
/* We might need to do the rest of the calculation twice */ /* We might need to do the rest of the calculation twice */
for (;;) for (;;)
{ {
unsigned int width, unsigned int width;
min_width;
/* Total width required to not wrap data */ /* Total width required to not wrap data */
width = hwidth + swidth + dwidth; width = hwidth + swidth + dwidth;
/* ... and not the header lines, either */
if (width < rwidth)
width = rwidth;
if (output_columns > 0)
{
unsigned int min_width;
/* Minimum acceptable width: room for just 3 columns of data */ /* Minimum acceptable width: room for just 3 columns of data */
min_width = hwidth + swidth + 3; min_width = hwidth + swidth + 3;
/* ... but not less than what the record header lines need */ /* ... but not less than what the record header lines need */
if (rwidth > min_width) if (min_width < rwidth)
min_width = rwidth; min_width = rwidth;
if (width < min_width || if (output_columns >= width)
(output_columns > 0 && output_columns < min_width)) {
/* Plenty of room, use native data width */
/* (but at least enough for the record header lines) */
newdwidth = width - hwidth - swidth;
}
else if (output_columns < min_width)
{ {
/* Set data width to match min_width */ /* Set data width to match min_width */
newdwidth = min_width - hwidth - swidth; newdwidth = min_width - hwidth - swidth;
} }
else if (output_columns > 0) else
{ {
/* Set data width to match output_columns */ /* Set data width to match output_columns */
newdwidth = output_columns - hwidth - swidth; newdwidth = output_columns - hwidth - swidth;
} }
}
else else
{ {
/* Use native data width */ /* Don't know the wrap limit, so use native data width */
newdwidth = dwidth; /* (but at least enough for the record header lines) */
newdwidth = width - hwidth - swidth;
} }
/* /*

View File

@ -2296,34 +2296,34 @@ execute q;
\pset format wrapped \pset format wrapped
execute q; execute q;
-[ RECORD 1 ]----+---------------------- -[ RECORD 1 ]----+---------------------
0123456789abcdef | xx 0123456789abcdef | xx
0123456789 | yyyyyyyyyyyyyyyyyy 0123456789 | yyyyyyyyyyyyyyyyyy
-[ RECORD 2 ]----+---------------------- -[ RECORD 2 ]----+---------------------
0123456789abcdef | xxxx 0123456789abcdef | xxxx
0123456789 | yyyyyyyyyyyyyyyy 0123456789 | yyyyyyyyyyyyyyyy
-[ RECORD 3 ]----+---------------------- -[ RECORD 3 ]----+---------------------
0123456789abcdef | xxxxxx 0123456789abcdef | xxxxxx
0123456789 | yyyyyyyyyyyyyy 0123456789 | yyyyyyyyyyyyyy
-[ RECORD 4 ]----+---------------------- -[ RECORD 4 ]----+---------------------
0123456789abcdef | xxxxxxxx 0123456789abcdef | xxxxxxxx
0123456789 | yyyyyyyyyyyy 0123456789 | yyyyyyyyyyyy
-[ RECORD 5 ]----+---------------------- -[ RECORD 5 ]----+---------------------
0123456789abcdef | xxxxxxxxxx 0123456789abcdef | xxxxxxxxxx
0123456789 | yyyyyyyyyy 0123456789 | yyyyyyyyyy
-[ RECORD 6 ]----+---------------------- -[ RECORD 6 ]----+---------------------
0123456789abcdef | xxxxxxxxxxxx 0123456789abcdef | xxxxxxxxxxxx
0123456789 | yyyyyyyy 0123456789 | yyyyyyyy
-[ RECORD 7 ]----+---------------------- -[ RECORD 7 ]----+---------------------
0123456789abcdef | xxxxxxxxxxxxxx 0123456789abcdef | xxxxxxxxxxxxxx
0123456789 | yyyyyy 0123456789 | yyyyyy
-[ RECORD 8 ]----+---------------------- -[ RECORD 8 ]----+---------------------
0123456789abcdef | xxxxxxxxxxxxxxxx 0123456789abcdef | xxxxxxxxxxxxxxxx
0123456789 | yyyy 0123456789 | yyyy
-[ RECORD 9 ]----+---------------------- -[ RECORD 9 ]----+---------------------
0123456789abcdef | xxxxxxxxxxxxxxxxxx 0123456789abcdef | xxxxxxxxxxxxxxxxxx
0123456789 | yy 0123456789 | yy
-[ RECORD 10 ]---+---------------------- -[ RECORD 10 ]---+---------------------
0123456789abcdef | xxxxxxxxxxxxxxxxxxxx 0123456789abcdef | xxxxxxxxxxxxxxxxxxxx
0123456789 | 0123456789 |