Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 458ef6b

Browse files
committed
Fix further concerns about psql wrapping in expanded mode having
collateral damage on other formats, by Sergey Muraviov.
1 parent 48d5084 commit 458ef6b

File tree

3 files changed

+1752
-429
lines changed

3 files changed

+1752
-429
lines changed

src/bin/psql/print.c

+148-49
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,9 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
11601160
dformatsize = 0;
11611161
struct lineptr *hlineptr,
11621162
*dlineptr;
1163-
bool is_pager = false;
1163+
bool is_pager = false,
1164+
hmultiline = false,
1165+
dmultiline = false;
11641166
int output_columns = 0; /* Width of interactive console */
11651167

11661168
if (cancel_pressed)
@@ -1196,7 +1198,10 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
11961198
if (width > hwidth)
11971199
hwidth = width;
11981200
if (height > hheight)
1201+
{
11991202
hheight = height;
1203+
hmultiline = true;
1204+
}
12001205
if (fs > hformatsize)
12011206
hformatsize = fs;
12021207
}
@@ -1213,7 +1218,10 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
12131218
if (width > dwidth)
12141219
dwidth = width;
12151220
if (height > dheight)
1221+
{
12161222
dheight = height;
1223+
dmultiline = true;
1224+
}
12171225
if (fs > dformatsize)
12181226
dformatsize = fs;
12191227
}
@@ -1258,45 +1266,82 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
12581266
if (cont->opt->format == PRINT_WRAPPED)
12591267
{
12601268
/*
1261-
* Calculate the available width to wrap the columns to after
1262-
* subtracting the maximum header width and separators. At a minimum
1263-
* enough to print "[ RECORD N ]"
1269+
* Separators width
12641270
*/
12651271
unsigned int width,
1266-
swidth;
1272+
min_width,
1273+
swidth,
1274+
iwidth = 0;
12671275

12681276
if (opt_border == 0)
1269-
swidth = 1; /* "header data" */
1277+
{
1278+
/*
1279+
* For border = 0, one space in the middle.
1280+
*/
1281+
swidth = 1;
1282+
}
12701283
else if (opt_border == 1)
1271-
swidth = 3; /* "header | data" */
1272-
else
1273-
swidth = 7; /* "| header | data |" */
1274-
1275-
/* Wrap to maximum width */
1276-
width = dwidth + swidth + hwidth;
1277-
if ((output_columns > 0) && (width > output_columns))
12781284
{
1279-
dwidth = output_columns - hwidth - swidth;
1280-
width = output_columns;
1285+
/*
1286+
* For border = 1, one for the pipe (|) in the middle
1287+
* between the two spaces.
1288+
*/
1289+
swidth = 3;
12811290
}
1291+
else
1292+
/*
1293+
* For border = 2, two more for the pipes (|) at the begging and
1294+
* at the end of the lines.
1295+
*/
1296+
swidth = 7;
12821297

1283-
/* Wrap to minimum width */
1298+
if ((opt_border < 2) &&
1299+
((hmultiline &&
1300+
(format == &pg_asciiformat_old)) ||
1301+
(dmultiline &&
1302+
(format != &pg_asciiformat_old))))
1303+
iwidth++; /* for newline indicators */
1304+
1305+
min_width = hwidth + iwidth + swidth + 3;
1306+
1307+
/*
1308+
* Record header width
1309+
*/
12841310
if (!opt_tuples_only)
12851311
{
1286-
int delta = 1 + log10(cont->nrows) - width;
1287-
1312+
/*
1313+
* Record number
1314+
*/
1315+
unsigned int rwidth = 1 + log10(cont->nrows);
12881316
if (opt_border == 0)
1289-
delta += 6; /* "* RECORD " */
1317+
rwidth += 9; /* "* RECORD " */
12901318
else if (opt_border == 1)
1291-
delta += 10; /* "-[ RECORD ]" */
1319+
rwidth += 12; /* "-[ RECORD ]" */
12921320
else
1293-
delta += 15; /* "+-[ RECORD ]-+" */
1321+
rwidth += 15; /* "+-[ RECORD ]-+" */
12941322

1295-
if (delta > 0)
1296-
dwidth += delta;
1323+
if (rwidth > min_width)
1324+
min_width = rwidth;
12971325
}
1298-
else if (dwidth < 3)
1299-
dwidth = 3;
1326+
1327+
/* Wrap to minimum width */
1328+
width = hwidth + iwidth + swidth + dwidth;
1329+
if ((width < min_width) || (output_columns < min_width))
1330+
width = min_width - hwidth - iwidth - swidth;
1331+
else if (output_columns > 0)
1332+
/*
1333+
* Wrap to maximum width
1334+
*/
1335+
width = output_columns - hwidth - iwidth - swidth;
1336+
1337+
if ((width < dwidth) || (dheight > 1))
1338+
{
1339+
dmultiline = true;
1340+
if ((opt_border == 0) &&
1341+
(format != &pg_asciiformat_old))
1342+
width--; /* for wrap indicators */
1343+
}
1344+
dwidth = width;
13001345
}
13011346

13021347
/* print records */
@@ -1321,11 +1366,17 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
13211366
/* Print record header (e.g. "[ RECORD N ]") above each record */
13221367
if (i % cont->ncolumns == 0)
13231368
{
1369+
unsigned int lhwidth = hwidth;
1370+
if ((opt_border < 2) &&
1371+
(hmultiline) &&
1372+
(format == &pg_asciiformat_old))
1373+
lhwidth++; /* for newline indicators */
1374+
13241375
if (!opt_tuples_only)
1325-
print_aligned_vertical_line(cont, record++, hwidth, dwidth,
1326-
pos, fout);
1376+
print_aligned_vertical_line(cont, record++, lhwidth,
1377+
dwidth, pos, fout);
13271378
else if (i != 0 || !cont->opt->start_table || opt_border == 2)
1328-
print_aligned_vertical_line(cont, 0, hwidth, dwidth,
1379+
print_aligned_vertical_line(cont, 0, lhwidth, dwidth,
13291380
pos, fout);
13301381
}
13311382

@@ -1354,35 +1405,62 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
13541405
/* Header (never wrapped so just need to deal with newlines) */
13551406
if (!hcomplete)
13561407
{
1357-
int swidth,
1358-
twidth = hwidth + 1;
1359-
1360-
fputs(hline ? format->header_nl_left : " ", fout);
1361-
strlen_max_width(hlineptr[hline].ptr, &twidth,
1408+
int swidth = hwidth,
1409+
target_width = hwidth;
1410+
/*
1411+
* Left spacer or new line indicator
1412+
*/
1413+
if ((opt_border == 2) ||
1414+
(hmultiline && (format == &pg_asciiformat_old)))
1415+
fputs(hline ? format->header_nl_left : " ", fout);
1416+
/*
1417+
* Header text
1418+
*/
1419+
strlen_max_width(hlineptr[hline].ptr, &target_width,
13621420
encoding);
13631421
fprintf(fout, "%-s", hlineptr[hline].ptr);
13641422

1365-
swidth = hwidth - twidth;
1366-
if (swidth > 0) /* spacer */
1423+
/*
1424+
* Spacer
1425+
*/
1426+
swidth -= target_width;
1427+
if (swidth > 0)
13671428
fprintf(fout, "%*s", swidth, " ");
13681429

1430+
/*
1431+
* New line indicator or separator's space
1432+
*/
13691433
if (hlineptr[hline + 1].ptr)
13701434
{
13711435
/* More lines after this one due to a newline */
1372-
fputs(format->header_nl_right, fout);
1436+
if ((opt_border > 0) ||
1437+
(hmultiline && (format != &pg_asciiformat_old)))
1438+
fputs(format->header_nl_right, fout);
13731439
hline++;
13741440
}
13751441
else
13761442
{
13771443
/* This was the last line of the header */
1378-
fputs(" ", fout);
1444+
if ((opt_border > 0) ||
1445+
(hmultiline && (format != &pg_asciiformat_old)))
1446+
fputs(" ", fout);
13791447
hcomplete = 1;
13801448
}
13811449
}
13821450
else
13831451
{
1384-
/* Header exhausted but more data for column */
1385-
fprintf(fout, "%*s", hwidth + 2, "");
1452+
unsigned int swidth = hwidth + opt_border;
1453+
if ((opt_border < 2) &&
1454+
(hmultiline) &&
1455+
(format == &pg_asciiformat_old))
1456+
swidth++;
1457+
1458+
if ((opt_border == 0) &&
1459+
(format != &pg_asciiformat_old) &&
1460+
(hmultiline))
1461+
swidth++;
1462+
1463+
fprintf(fout, "%*s", swidth, " ");
13861464
}
13871465

13881466
/* Separator */
@@ -1401,13 +1479,18 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
14011479
/* Data */
14021480
if (!dcomplete)
14031481
{
1404-
int target_width,
1482+
int target_width = dwidth,
14051483
bytes_to_output,
1406-
swidth;
1484+
swidth = dwidth;
14071485

1486+
/*
1487+
* Left spacer on wrap indicator
1488+
*/
14081489
fputs(!dcomplete && !offset ? " " : format->wrap_left, fout);
14091490

1410-
target_width = dwidth;
1491+
/*
1492+
* Data text
1493+
*/
14111494
bytes_to_output = strlen_max_width(dlineptr[dline].ptr + offset,
14121495
&target_width, encoding);
14131496
fputnbytes(fout, (char *) (dlineptr[dline].ptr + offset),
@@ -1416,31 +1499,47 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
14161499
chars_to_output -= target_width;
14171500
offset += bytes_to_output;
14181501

1419-
/* spacer */
1420-
swidth = dwidth - target_width;
1421-
if (swidth > 0)
1422-
fprintf(fout, "%*s", swidth, "");
1502+
/* Spacer */
1503+
swidth -= target_width;
14231504

14241505
if (chars_to_output)
14251506
{
14261507
/* continuing a wrapped column */
1427-
fputs(format->wrap_right, fout);
1508+
if ((opt_border > 1) ||
1509+
(dmultiline && (format != &pg_asciiformat_old)))
1510+
{
1511+
if (swidth > 0)
1512+
fprintf(fout, "%*s", swidth, " ");
1513+
fputs(format->wrap_right, fout);
1514+
}
14281515
}
14291516
else if (dlineptr[dline + 1].ptr)
14301517
{
14311518
/* reached a newline in the column */
1432-
fputs(format->nl_right, fout);
1519+
if ((opt_border > 1) ||
1520+
(dmultiline && (format != &pg_asciiformat_old)))
1521+
{
1522+
if (swidth > 0)
1523+
fprintf(fout, "%*s", swidth, " ");
1524+
fputs(format->nl_right, fout);
1525+
}
14331526
dline++;
14341527
offset = 0;
14351528
chars_to_output = dlineptr[dline].width;
14361529
}
14371530
else
14381531
{
14391532
/* reached the end of the cell */
1440-
fputs(" ", fout);
1533+
if (opt_border > 1)
1534+
{
1535+
if (swidth > 0)
1536+
fprintf(fout, "%*s", swidth, " ");
1537+
fputs(" ", fout);
1538+
}
14411539
dcomplete = 1;
14421540
}
14431541

1542+
/* Right border */
14441543
if (opt_border == 2)
14451544
fputs(dformat->rightvrule, fout);
14461545

0 commit comments

Comments
 (0)