In expanded mode psql calculates the width of the longest field in all the 
output rows,
and prints the header line according to it. This often results in record header 
wrapping
over several lines (see example below).

This huge record header is printed the same way before each record, even if all 
the fields
in current record are small and fit into terminal width. This often leads to 
situations
when record header occupies the entirety of the screen, for all records, even 
though there are
only a few records with huge fields in a record set.

Maybe we can avoid making the header line longer than terminal width for \pset 
border 0
and \pset border 1? We already have terminal width calculated. Please see 
attached a patch
with the proposed implementation.

Example output before the modification, in a terminal with a 100-column width:

$ psql template1 -c "\x on" -c "\pset border 1;" -c "select n, repeat('x', n) as 
long_column_name from unnest(array[42,210]) as n"
Expanded display is on.
Border style is 1.
─[ RECORD 1 
]────┬──────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────────────────────────
─────────────────────────────
n                │ 42
long_column_name │ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
─[ RECORD 2 
]────┼──────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────────────────────────
─────────────────────────────
n                │ 210
long_column_name │ 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

And here's the same command after the patch:

$ psql template1 -c "\x on" -c "\pset border 1;" -c "select n, repeat('x', n) as 
long_column_name from unnest(array[42,210]) as n"
Expanded display is on.
Border style is 1.
─[ RECORD 1 
]────┬──────────────────────────────────────────────────────────────────────────────────
n                │ 42
long_column_name │ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
─[ RECORD 2 
]────┼──────────────────────────────────────────────────────────────────────────────────
n                │ 210
long_column_name │ 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Best regards,
Platon Pronko
diff --git a/src/fe_utils/print.c b/src/fe_utils/print.c
index d48fcc4a03..9483d0d852 100644
--- a/src/fe_utils/print.c
+++ b/src/fe_utils/print.c
@@ -1157,6 +1157,7 @@ print_aligned_vertical_line(const printTextFormat *format,
 							unsigned long record,
 							unsigned int hwidth,
 							unsigned int dwidth,
+							int output_columns,
 							printTextRule pos,
 							FILE *fout)
 {
@@ -1200,6 +1201,14 @@ print_aligned_vertical_line(const printTextFormat *format,
 	}
 	if (reclen < 0)
 		reclen = 0;
+
+	if (output_columns > 0) {
+		if (opt_border == 0)
+			dwidth = Min(dwidth, output_columns - hwidth);
+		if (opt_border == 1)
+			dwidth = Min(dwidth, output_columns - hwidth - 3);
+	}
+
 	for (i = reclen; i < dwidth; i++)
 		fputs(opt_border > 0 ? lformat->hrule : " ", fout);
 	if (opt_border == 2)
@@ -1501,10 +1510,11 @@ print_aligned_vertical(const printTableContent *cont,
 
 			if (!opt_tuples_only)
 				print_aligned_vertical_line(format, opt_border, record++,
-											lhwidth, dwidth, pos, fout);
+											lhwidth, dwidth, output_columns,
+											pos, fout);
 			else if (i != 0 || !cont->opt->start_table || opt_border == 2)
 				print_aligned_vertical_line(format, opt_border, 0, lhwidth,
-											dwidth, pos, fout);
+											dwidth, output_columns, pos, fout);
 		}
 
 		/* Format the header */
@@ -1691,7 +1701,7 @@ print_aligned_vertical(const printTableContent *cont,
 	{
 		if (opt_border == 2 && !cancel_pressed)
 			print_aligned_vertical_line(format, opt_border, 0, hwidth, dwidth,
-										PRINT_RULE_BOTTOM, fout);
+										output_columns, PRINT_RULE_BOTTOM, fout);
 
 		/* print footers */
 		if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)

Reply via email to