@@ -1161,7 +1161,6 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
1161
1161
struct lineptr * hlineptr ,
1162
1162
* dlineptr ;
1163
1163
bool is_pager = false;
1164
- int output_columns = 0 ; /* Width of interactive console */
1165
1164
1166
1165
if (cancel_pressed )
1167
1166
return ;
@@ -1235,90 +1234,24 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
1235
1234
fprintf (fout , "%s\n" , cont -> title );
1236
1235
}
1237
1236
1238
- /*
1239
- * Choose target output width: \pset columns, or $COLUMNS, or ioctl
1240
- */
1241
- if (cont -> opt -> columns > 0 )
1242
- output_columns = cont -> opt -> columns ;
1243
- else if ((fout == stdout && isatty (fileno (stdout ))) || is_pager )
1244
- {
1245
- if (cont -> opt -> env_columns > 0 )
1246
- output_columns = cont -> opt -> env_columns ;
1247
- #ifdef TIOCGWINSZ
1248
- else
1249
- {
1250
- struct winsize screen_size ;
1251
-
1252
- if (ioctl (fileno (stdout ), TIOCGWINSZ , & screen_size ) != -1 )
1253
- output_columns = screen_size .ws_col ;
1254
- }
1255
- #endif
1256
- }
1257
-
1258
- if (cont -> opt -> format == PRINT_WRAPPED )
1259
- {
1260
- /*
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 ]"
1264
- */
1265
- unsigned int width ,
1266
- swidth ;
1267
-
1268
- if (opt_border == 0 )
1269
- swidth = 1 ; /* "header data" */
1270
- 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
- {
1279
- dwidth = output_columns - hwidth - swidth ;
1280
- width = output_columns ;
1281
- }
1282
-
1283
- /* Wrap to minimum width */
1284
- if (!opt_tuples_only )
1285
- {
1286
- int delta = 1 + log10 (cont -> nrows ) - width ;
1287
-
1288
- if (opt_border == 0 )
1289
- delta += 6 ; /* "* RECORD " */
1290
- else if (opt_border == 1 )
1291
- delta += 10 ; /* "-[ RECORD ]" */
1292
- else
1293
- delta += 15 ; /* "+-[ RECORD ]-+" */
1294
-
1295
- if (delta > 0 )
1296
- dwidth += delta ;
1297
- }
1298
- else if (dwidth < 3 )
1299
- dwidth = 3 ;
1300
- }
1301
-
1302
1237
/* print records */
1303
1238
for (i = 0 , ptr = cont -> cells ; * ptr ; i ++ , ptr ++ )
1304
1239
{
1305
1240
printTextRule pos ;
1306
- int dline ,
1307
- hline ,
1241
+ int line_count ,
1308
1242
dcomplete ,
1309
- hcomplete ,
1310
- offset ,
1311
- chars_to_output ;
1243
+ hcomplete ;
1312
1244
1313
1245
if (cancel_pressed )
1314
1246
break ;
1315
1247
1316
1248
if (i == 0 )
1317
1249
pos = PRINT_RULE_TOP ;
1250
+ else if (!(* (ptr + 1 )))
1251
+ pos = PRINT_RULE_BOTTOM ;
1318
1252
else
1319
1253
pos = PRINT_RULE_MIDDLE ;
1320
1254
1321
- /* Print record header (e.g. "[ RECORD N ]") above each record */
1322
1255
if (i % cont -> ncolumns == 0 )
1323
1256
{
1324
1257
if (!opt_tuples_only )
@@ -1337,126 +1270,48 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
1337
1270
pg_wcsformat ((const unsigned char * ) * ptr , strlen (* ptr ), encoding ,
1338
1271
dlineptr , dheight );
1339
1272
1340
- /*
1341
- * Loop through header and data in parallel dealing with newlines and
1342
- * wrapped lines until they're both exhausted
1343
- */
1344
- dline = hline = 0 ;
1273
+ line_count = 0 ;
1345
1274
dcomplete = hcomplete = 0 ;
1346
- offset = 0 ;
1347
- chars_to_output = dlineptr [dline ].width ;
1348
1275
while (!dcomplete || !hcomplete )
1349
1276
{
1350
- /* Left border */
1351
1277
if (opt_border == 2 )
1352
- fprintf (fout , "%s" , dformat -> leftvrule );
1353
-
1354
- /* Header (never wrapped so just need to deal with newlines) */
1278
+ fprintf (fout , "%s " , dformat -> leftvrule );
1355
1279
if (!hcomplete )
1356
1280
{
1357
- int swidth ,
1358
- twidth = hwidth + 1 ;
1359
-
1360
- fputs (hline ? format -> header_nl_left : " " , fout );
1361
- strlen_max_width (hlineptr [hline ].ptr , & twidth ,
1362
- encoding );
1363
- fprintf (fout , "%-s" , hlineptr [hline ].ptr );
1364
-
1365
- swidth = hwidth - twidth ;
1366
- if (swidth > 0 ) /* spacer */
1367
- fprintf (fout , "%*s" , swidth , " " );
1281
+ fprintf (fout , "%-s%*s" , hlineptr [line_count ].ptr ,
1282
+ hwidth - hlineptr [line_count ].width , "" );
1368
1283
1369
- if (hlineptr [hline + 1 ].ptr )
1370
- {
1371
- /* More lines after this one due to a newline */
1372
- fputs (format -> header_nl_right , fout );
1373
- hline ++ ;
1374
- }
1375
- else
1376
- {
1377
- /* This was the last line of the header */
1378
- fputs (" " , fout );
1284
+ if (!hlineptr [line_count + 1 ].ptr )
1379
1285
hcomplete = 1 ;
1380
- }
1381
1286
}
1382
1287
else
1383
- {
1384
- /* Header exhausted but more data for column */
1385
- fprintf (fout , "%*s" , hwidth + 2 , "" );
1386
- }
1288
+ fprintf (fout , "%*s" , hwidth , "" );
1387
1289
1388
- /* Separator */
1389
1290
if (opt_border > 0 )
1390
- {
1391
- if (offset )
1392
- fputs (format -> midvrule_wrap , fout );
1393
- else if (!dline )
1394
- fputs (dformat -> midvrule , fout );
1395
- else if (dline )
1396
- fputs (format -> midvrule_nl , fout );
1397
- else
1398
- fputs (format -> midvrule_blank , fout );
1399
- }
1291
+ fprintf (fout , " %s " , dformat -> midvrule );
1292
+ else
1293
+ fputc (' ' , fout );
1400
1294
1401
- /* Data */
1402
1295
if (!dcomplete )
1403
1296
{
1404
- int target_width ,
1405
- bytes_to_output ,
1406
- swidth ;
1407
-
1408
- fputs (!dcomplete && !offset ? " " : format -> wrap_left , fout );
1409
-
1410
- target_width = dwidth ;
1411
- bytes_to_output = strlen_max_width (dlineptr [dline ].ptr + offset ,
1412
- & target_width , encoding );
1413
- fputnbytes (fout , (char * ) (dlineptr [dline ].ptr + offset ),
1414
- bytes_to_output );
1415
-
1416
- chars_to_output -= target_width ;
1417
- offset += bytes_to_output ;
1418
-
1419
- /* spacer */
1420
- swidth = dwidth - target_width ;
1421
- if (swidth > 0 )
1422
- fprintf (fout , "%*s" , swidth , "" );
1423
-
1424
- if (chars_to_output )
1425
- {
1426
- /* continuing a wrapped column */
1427
- fputs (format -> wrap_right , fout );
1428
- }
1429
- else if (dlineptr [dline + 1 ].ptr )
1430
- {
1431
- /* reached a newline in the column */
1432
- fputs (format -> nl_right , fout );
1433
- dline ++ ;
1434
- offset = 0 ;
1435
- chars_to_output = dlineptr [dline ].width ;
1436
- }
1297
+ if (opt_border < 2 )
1298
+ fprintf (fout , "%s\n" , dlineptr [line_count ].ptr );
1437
1299
else
1438
- {
1439
- /* reached the end of the cell */
1440
- fputs (" " , fout );
1441
- dcomplete = 1 ;
1442
- }
1443
-
1444
- if (opt_border == 2 )
1445
- fputs (dformat -> rightvrule , fout );
1300
+ fprintf (fout , "%-s%*s %s\n" , dlineptr [line_count ].ptr ,
1301
+ dwidth - dlineptr [line_count ].width , "" ,
1302
+ dformat -> rightvrule );
1446
1303
1447
- fputs ("\n" , fout );
1304
+ if (!dlineptr [line_count + 1 ].ptr )
1305
+ dcomplete = 1 ;
1448
1306
}
1449
1307
else
1450
1308
{
1451
- /*
1452
- * data exhausted (this can occur if header is longer than the
1453
- * data due to newlines in the header)
1454
- */
1455
1309
if (opt_border < 2 )
1456
- fputs ( "\n" , fout );
1310
+ fputc ( '\n' , fout );
1457
1311
else
1458
- fprintf (fout , "%*s %s\n" , dwidth , "" , dformat -> rightvrule );
1312
+ fprintf (fout , "%*s %s\n" , dwidth , "" , dformat -> rightvrule );
1459
1313
}
1314
+ line_count ++ ;
1460
1315
}
1461
1316
}
1462
1317
0 commit comments