@@ -1160,7 +1160,9 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
1160
1160
dformatsize = 0 ;
1161
1161
struct lineptr * hlineptr ,
1162
1162
* dlineptr ;
1163
- bool is_pager = false;
1163
+ bool is_pager = false,
1164
+ hmultiline = false,
1165
+ dmultiline = false;
1164
1166
int output_columns = 0 ; /* Width of interactive console */
1165
1167
1166
1168
if (cancel_pressed )
@@ -1196,7 +1198,10 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
1196
1198
if (width > hwidth )
1197
1199
hwidth = width ;
1198
1200
if (height > hheight )
1201
+ {
1199
1202
hheight = height ;
1203
+ hmultiline = true;
1204
+ }
1200
1205
if (fs > hformatsize )
1201
1206
hformatsize = fs ;
1202
1207
}
@@ -1213,7 +1218,10 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
1213
1218
if (width > dwidth )
1214
1219
dwidth = width ;
1215
1220
if (height > dheight )
1221
+ {
1216
1222
dheight = height ;
1223
+ dmultiline = true;
1224
+ }
1217
1225
if (fs > dformatsize )
1218
1226
dformatsize = fs ;
1219
1227
}
@@ -1258,45 +1266,82 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
1258
1266
if (cont -> opt -> format == PRINT_WRAPPED )
1259
1267
{
1260
1268
/*
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
1264
1270
*/
1265
1271
unsigned int width ,
1266
- swidth ;
1272
+ min_width ,
1273
+ swidth ,
1274
+ iwidth = 0 ;
1267
1275
1268
1276
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
+ }
1270
1283
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 ))
1278
1284
{
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 ;
1281
1290
}
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 ;
1282
1297
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
+ */
1284
1310
if (!opt_tuples_only )
1285
1311
{
1286
- int delta = 1 + log10 (cont -> nrows ) - width ;
1287
-
1312
+ /*
1313
+ * Record number
1314
+ */
1315
+ unsigned int rwidth = 1 + log10 (cont -> nrows );
1288
1316
if (opt_border == 0 )
1289
- delta += 6 ; /* "* RECORD " */
1317
+ rwidth += 9 ; /* "* RECORD " */
1290
1318
else if (opt_border == 1 )
1291
- delta += 10 ; /* "-[ RECORD ]" */
1319
+ rwidth += 12 ; /* "-[ RECORD ]" */
1292
1320
else
1293
- delta += 15 ; /* "+-[ RECORD ]-+" */
1321
+ rwidth += 15 ; /* "+-[ RECORD ]-+" */
1294
1322
1295
- if (delta > 0 )
1296
- dwidth += delta ;
1323
+ if (rwidth > min_width )
1324
+ min_width = rwidth ;
1297
1325
}
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 ;
1300
1345
}
1301
1346
1302
1347
/* print records */
@@ -1321,11 +1366,17 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
1321
1366
/* Print record header (e.g. "[ RECORD N ]") above each record */
1322
1367
if (i % cont -> ncolumns == 0 )
1323
1368
{
1369
+ unsigned int lhwidth = hwidth ;
1370
+ if ((opt_border < 2 ) &&
1371
+ (hmultiline ) &&
1372
+ (format == & pg_asciiformat_old ))
1373
+ lhwidth ++ ; /* for newline indicators */
1374
+
1324
1375
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 );
1327
1378
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 ,
1329
1380
pos , fout );
1330
1381
}
1331
1382
@@ -1354,35 +1405,62 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
1354
1405
/* Header (never wrapped so just need to deal with newlines) */
1355
1406
if (!hcomplete )
1356
1407
{
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 ,
1362
1420
encoding );
1363
1421
fprintf (fout , "%-s" , hlineptr [hline ].ptr );
1364
1422
1365
- swidth = hwidth - twidth ;
1366
- if (swidth > 0 ) /* spacer */
1423
+ /*
1424
+ * Spacer
1425
+ */
1426
+ swidth -= target_width ;
1427
+ if (swidth > 0 )
1367
1428
fprintf (fout , "%*s" , swidth , " " );
1368
1429
1430
+ /*
1431
+ * New line indicator or separator's space
1432
+ */
1369
1433
if (hlineptr [hline + 1 ].ptr )
1370
1434
{
1371
1435
/* 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 );
1373
1439
hline ++ ;
1374
1440
}
1375
1441
else
1376
1442
{
1377
1443
/* 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 );
1379
1447
hcomplete = 1 ;
1380
1448
}
1381
1449
}
1382
1450
else
1383
1451
{
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 , " " );
1386
1464
}
1387
1465
1388
1466
/* Separator */
@@ -1401,13 +1479,18 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
1401
1479
/* Data */
1402
1480
if (!dcomplete )
1403
1481
{
1404
- int target_width ,
1482
+ int target_width = dwidth ,
1405
1483
bytes_to_output ,
1406
- swidth ;
1484
+ swidth = dwidth ;
1407
1485
1486
+ /*
1487
+ * Left spacer on wrap indicator
1488
+ */
1408
1489
fputs (!dcomplete && !offset ? " " : format -> wrap_left , fout );
1409
1490
1410
- target_width = dwidth ;
1491
+ /*
1492
+ * Data text
1493
+ */
1411
1494
bytes_to_output = strlen_max_width (dlineptr [dline ].ptr + offset ,
1412
1495
& target_width , encoding );
1413
1496
fputnbytes (fout , (char * ) (dlineptr [dline ].ptr + offset ),
@@ -1416,31 +1499,47 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
1416
1499
chars_to_output -= target_width ;
1417
1500
offset += bytes_to_output ;
1418
1501
1419
- /* spacer */
1420
- swidth = dwidth - target_width ;
1421
- if (swidth > 0 )
1422
- fprintf (fout , "%*s" , swidth , "" );
1502
+ /* Spacer */
1503
+ swidth -= target_width ;
1423
1504
1424
1505
if (chars_to_output )
1425
1506
{
1426
1507
/* 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
+ }
1428
1515
}
1429
1516
else if (dlineptr [dline + 1 ].ptr )
1430
1517
{
1431
1518
/* 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
+ }
1433
1526
dline ++ ;
1434
1527
offset = 0 ;
1435
1528
chars_to_output = dlineptr [dline ].width ;
1436
1529
}
1437
1530
else
1438
1531
{
1439
1532
/* 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
+ }
1441
1539
dcomplete = 1 ;
1442
1540
}
1443
1541
1542
+ /* Right border */
1444
1543
if (opt_border == 2 )
1445
1544
fputs (dformat -> rightvrule , fout );
1446
1545
0 commit comments