Please check this patch.
2014-05-12 22:56 GMT+04:00 Sergey Muraviov <[email protected]>:
> Hi.
>
> I'll try to fix it tomorrow.
>
>
> 2014-05-12 18:42 GMT+04:00 Tom Lane <[email protected]>:
>
> Greg Stark <[email protected]> writes:
>> > On Mon, May 12, 2014 at 2:12 PM, Greg Stark <[email protected]> wrote:
>> >> Hm, there was an off by one error earlier in some cases, maybe we
>> >> fixed it by breaking other case. Will investigate.
>>
>> > Those spaces are coming from the ascii wrapping indicators. i.e. the
>> periods in:
>>
>> Ah. I wonder whether anyone will complain that the format changed?
>>
>> > Apparently we used to print those with border=1 in normal mode but in
>> > expanded mode we left out the space for those on the outermost edges
>> > since there was no need for them. If we put them in for wrapped mode
>> > then we'll be inconsistent if we don't for nonwrapped mode though. And
>> > if we don't put them in for wrapped mode then there's no way to
>> > indicate wrapping versus newlines.
>>
>> Barring anyone complaining that the format changed, I'd say the issue
>> is not that you added them but that the accounting for line length
>> fails to include them.
>>
>> regards, tom lane
>>
>
>
>
> --
> Best regards,
> Sergey Muraviov
>
--
Best regards,
Sergey MuraviovH
diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c
index 62850d8..69f4efe 100644
--- a/src/bin/psql/print.c
+++ b/src/bin/psql/print.c
@@ -1258,45 +1258,67 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
if (cont->opt->format == PRINT_WRAPPED)
{
/*
- * Calculate the available width to wrap the columns to after
- * subtracting the maximum header width and separators. At a minimum
- * enough to print "[ RECORD N ]"
+ * Separators width
*/
unsigned int width,
+ min_width,
swidth;
if (opt_border == 0)
- swidth = 1; /* "header data" */
+ /*
+ * For border = 0, one space in the middle.
+ */
+ swidth = 1;
else if (opt_border == 1)
- swidth = 3; /* "header | data" */
- else
- swidth = 7; /* "| header | data |" */
-
- /* Wrap to maximum width */
- width = dwidth + swidth + hwidth;
- if ((output_columns > 0) && (width > output_columns))
{
- dwidth = output_columns - hwidth - swidth;
- width = output_columns;
+ /*
+ * For border = 1, one for the pipe (|) in the middle
+ * between the two spaces.
+ */
+ swidth = 3;
}
+ else
+ /*
+ * For border = 2, two more for the pipes (|) at the begging and
+ * at the end of the lines.
+ */
+ swidth = 7;
- /* Wrap to minimum width */
+ min_width = hwidth + swidth + 3;
+
+ /*
+ * Record header width
+ */
if (!opt_tuples_only)
{
- int delta = 1 + log10(cont->nrows) - width;
-
+ /*
+ * Record number
+ */
+ unsigned int rwidth = 1 + log10(cont->nrows);
if (opt_border == 0)
- delta += 6; /* "* RECORD " */
+ rwidth += 9; /* "* RECORD " */
else if (opt_border == 1)
- delta += 10; /* "-[ RECORD ]" */
+ rwidth += 12; /* "-[ RECORD ]" */
else
- delta += 15; /* "+-[ RECORD ]-+" */
+ rwidth += 15; /* "+-[ RECORD ]-+" */
- if (delta > 0)
- dwidth += delta;
+ if (rwidth > min_width)
+ min_width = rwidth;
}
- else if (dwidth < 3)
- dwidth = 3;
+
+ if ((hheight > 1) && (opt_border < 2))
+ hwidth++; /* for wrapping indicator*/
+
+ /* Wrap to minimum width */
+ width = hwidth + swidth + dwidth;
+ if ((width < min_width) || (output_columns < min_width))
+ dwidth = min_width - hwidth - swidth;
+ else if ((output_columns > 0) &&
+ (width > output_columns))
+ /*
+ * Wrap to maximum width
+ */
+ dwidth = output_columns - hwidth - swidth;
}
/* print records */
@@ -1357,12 +1379,17 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
int swidth,
twidth = hwidth + 1;
- fputs(hline ? format->header_nl_left : " ", fout);
+ if ((hheight > 1) || (opt_border == 2))
+ fputs(hline ? format->header_nl_left : " ", fout);
strlen_max_width(hlineptr[hline].ptr, &twidth,
encoding);
fprintf(fout, "%-s", hlineptr[hline].ptr);
swidth = hwidth - twidth;
+ if ((hheight > 1) &&
+ (opt_border < 2) &&
+ (cont->opt->format == PRINT_WRAPPED))
+ swidth--;
if (swidth > 0) /* spacer */
fprintf(fout, "%*s", swidth, " ");
@@ -1382,7 +1409,11 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
else
{
/* Header exhausted but more data for column */
- fprintf(fout, "%*s", hwidth + 2, "");
+ unsigned int ewidth = hwidth + 2;
+ if ((opt_border < 2) &&
+ (cont->opt->format == PRINT_WRAPPED))
+ ewidth--;
+ fprintf(fout, "%*s", ewidth, "");
}
/* Separator */
@@ -1401,13 +1432,24 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
/* Data */
if (!dcomplete)
{
- int target_width,
+ int target_width = dwidth,
bytes_to_output,
swidth;
- fputs(!dcomplete && !offset ? " " : format->wrap_left, fout);
+ if (dheight > 1)
+ {
+ fputs(!dcomplete && !offset ? " " : format->wrap_left, fout);
+ if (cont->opt->format == PRINT_WRAPPED)
+ {
+ if (opt_border < 2)
+ target_width--;
+ if (opt_border < 1)
+ target_width--;
+ }
+ }
+ else if (opt_border > 0)
+ fputs(" ", fout);
- target_width = dwidth;
bytes_to_output = strlen_max_width(dlineptr[dline].ptr + offset,
&target_width, encoding);
fputnbytes(fout, (char *) (dlineptr[dline].ptr + offset),
@@ -1418,6 +1460,13 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
/* spacer */
swidth = dwidth - target_width;
+ if (cont->opt->format == PRINT_WRAPPED)
+ {
+ if (opt_border < 2)
+ swidth--;
+ if (opt_border < 1)
+ swidth--;
+ }
if (swidth > 0)
fprintf(fout, "%*s", swidth, "");
@@ -1437,7 +1486,8 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
else
{
/* reached the end of the cell */
- fputs(" ", fout);
+ if ((dheight > 1) || (opt_border == 2))
+ fputs(" ", fout);
dcomplete = 1;
}
diff --git a/src/test/regress/expected/psql.out b/src/test/regress/expected/psql.out
index c7dbd54..dcc32b0 100644
--- a/src/test/regress/expected/psql.out
+++ b/src/test/regress/expected/psql.out
@@ -334,31 +334,34 @@ execute q;
\pset format wrapped
execute q;
* Record 1
- a+ xx
+ a+ xx
+
b
- a+ yyyyyyyyyyyyyyyyyy
- b
+ a+ yyyyyyyyyyyyyyy.
+ b .yyy
* Record 2
- a+ xxxx +
- + xxxxxx +
- b xxxxxxxx +
- xxxxxxxxxx +
- xxxxxxxxxxxx +
- xxxxxxxxxxxxxx +
- xxxxxxxxxxxxxxxx +
- xxxxxxxxxxxxxxxxxx+
- xxxxxxxxxxxxxxxxxx.
- .xx
- a+ yyyyyyyyyyyyyyyy +
- b yyyyyyyyyyyyyy +
- yyyyyyyyyyyy +
- yyyyyyyyyy +
- yyyyyyyy +
- yyyyyy +
- yyyy +
- yy +
-
+ a+ xxxx +
+ + xxxxxx +
+ b xxxxxxxx +
+ xxxxxxxxxx +
+ xxxxxxxxxxxx +
+ xxxxxxxxxxxxxx +
+ xxxxxxxxxxxxxxx.
+ .x +
+ xxxxxxxxxxxxxxx.
+ .xxx +
+ xxxxxxxxxxxxxxx.
+ .xxxxx
+ a+ yyyyyyyyyyyyyyy.
+ b .y +
+ yyyyyyyyyyyyyy +
+ yyyyyyyyyyyy +
+ yyyyyyyyyy +
+ yyyyyyyy +
+ yyyyyy +
+ yyyy +
+ yy +
+
\pset border 1
\pset format unaligned
@@ -421,32 +424,34 @@ execute q;
\pset format wrapped
execute q;
-[ RECORD 1 ]-------
- a+| xx
+ a+| xx
+|
b |
- a+| yyyyyyyyyyyyyyyy.
- b |.yy
+ a+| yyyyyyyyyyyyyy.
+ b |.yyyy
-[ RECORD 2 ]-------
- a+| xxxx +
- +| xxxxxx +
- b | xxxxxxxx +
- | xxxxxxxxxx +
- | xxxxxxxxxxxx +
- | xxxxxxxxxxxxxx +
- | xxxxxxxxxxxxxxxx+
- | xxxxxxxxxxxxxxxx.
- |.xx +
- | xxxxxxxxxxxxxxxx.
- |.xxxx
- a+| yyyyyyyyyyyyyyyy+
- b | yyyyyyyyyyyyyy +
- | yyyyyyyyyyyy +
- | yyyyyyyyyy +
- | yyyyyyyy +
- | yyyyyy +
- | yyyy +
- | yy +
- |
+ a+| xxxx +
+ +| xxxxxx +
+ b | xxxxxxxx +
+ | xxxxxxxxxx +
+ | xxxxxxxxxxxx +
+ | xxxxxxxxxxxxxx+
+ | xxxxxxxxxxxxxx.
+ |.xx +
+ | xxxxxxxxxxxxxx.
+ |.xxxx +
+ | xxxxxxxxxxxxxx.
+ |.xxxxxx
+ a+| yyyyyyyyyyyyyy.
+ b |.yy +
+ | yyyyyyyyyyyyyy+
+ | yyyyyyyyyyyy +
+ | yyyyyyyyyy +
+ | yyyyyyyy +
+ | yyyyyy +
+ | yyyy +
+ | yy +
+ |
\pset border 2
\pset format unaligned
@@ -803,31 +808,34 @@ execute q;
\pset format wrapped
execute q;
* Record 1
- a xx
+ a xx
+
+b
- a yyyyyyyyyyyyyyyyyy
-+b
+ a yyyyyyyyyyyyyyy
++b yyy
* Record 2
- a xxxx
-+ xxxxxx
-+b xxxxxxxx
- xxxxxxxxxx
- xxxxxxxxxxxx
- xxxxxxxxxxxxxx
- xxxxxxxxxxxxxxxx
- xxxxxxxxxxxxxxxxxx
- xxxxxxxxxxxxxxxxxx
- xx
- a yyyyyyyyyyyyyyyy
-+b yyyyyyyyyyyyyy
- yyyyyyyyyyyy
- yyyyyyyyyy
- yyyyyyyy
- yyyyyy
- yyyy
- yy
-
+ a xxxx
++ xxxxxx
++b xxxxxxxx
+ xxxxxxxxxx
+ xxxxxxxxxxxx
+ xxxxxxxxxxxxxx
+ xxxxxxxxxxxxxxx
+ x
+ xxxxxxxxxxxxxxx
+ xxx
+ xxxxxxxxxxxxxxx
+ xxxxx
+ a yyyyyyyyyyyyyyy
++b y
+ yyyyyyyyyyyyyy
+ yyyyyyyyyyyy
+ yyyyyyyyyy
+ yyyyyyyy
+ yyyyyy
+ yyyy
+ yy
+
\pset border 1
\pset format unaligned
@@ -890,32 +898,34 @@ execute q;
\pset format wrapped
execute q;
-[ RECORD 1 ]-------
- a | xx
+ a | xx
+ ;
+b ;
- a | yyyyyyyyyyyyyyyy
-+b ; yy
+ a | yyyyyyyyyyyyyy
++b ; yyyy
-[ RECORD 2 ]-------
- a | xxxx
-+ : xxxxxx
-+b : xxxxxxxx
- : xxxxxxxxxx
- : xxxxxxxxxxxx
- : xxxxxxxxxxxxxx
- : xxxxxxxxxxxxxxxx
- : xxxxxxxxxxxxxxxx
- ; xx
- : xxxxxxxxxxxxxxxx
- ; xxxx
- a | yyyyyyyyyyyyyyyy
-+b : yyyyyyyyyyyyyy
- : yyyyyyyyyyyy
- : yyyyyyyyyy
- : yyyyyyyy
- : yyyyyy
- : yyyy
- : yy
- :
+ a | xxxx
++ : xxxxxx
++b : xxxxxxxx
+ : xxxxxxxxxx
+ : xxxxxxxxxxxx
+ : xxxxxxxxxxxxxx
+ : xxxxxxxxxxxxxx
+ ; xx
+ : xxxxxxxxxxxxxx
+ ; xxxx
+ : xxxxxxxxxxxxxx
+ ; xxxxxx
+ a | yyyyyyyyyyyyyy
++b ; yy
+ : yyyyyyyyyyyyyy
+ : yyyyyyyyyyyy
+ : yyyyyyyyyy
+ : yyyyyyyy
+ : yyyyyy
+ : yyyy
+ : yy
+ :
\pset border 2
\pset format unaligned
--
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers