@@ -122,9 +122,11 @@ const printTextFormat pg_utf8format =
122122
123123/* Local functions */
124124static int strlen_max_width (unsigned char * str , int * target_width , int encoding );
125- static void IsPagerNeeded (const printTableContent * cont , const int extra_lines ,
125+ static void IsPagerNeeded (const printTableContent * cont , const int extra_lines , bool expanded ,
126126 FILE * * fout , bool * is_pager );
127127
128+ static void print_aligned_vertical (const printTableContent * cont , FILE * fout );
129+
128130
129131static void *
130132pg_local_malloc (size_t size )
@@ -713,6 +715,17 @@ print_aligned_text(const printTableContent *cont, FILE *fout)
713715 }
714716 }
715717
718+ /*
719+ * If in expanded auto mode, we have now calculated the expected width, so
720+ * we can now escape to vertical mode if necessary.
721+ */
722+ if (cont -> opt -> expanded == 2 && output_columns > 0 &&
723+ (output_columns < total_header_width || output_columns < width_total ))
724+ {
725+ print_aligned_vertical (cont , fout );
726+ return ;
727+ }
728+
716729 /* If we wrapped beyond the display width, use the pager */
717730 if (!is_pager && fout == stdout && output_columns > 0 &&
718731 (output_columns < total_header_width || output_columns < width_total ))
@@ -756,7 +769,7 @@ print_aligned_text(const printTableContent *cont, FILE *fout)
756769 extra_row_output_lines = 0 ;
757770 }
758771 }
759- IsPagerNeeded (cont , extra_output_lines , & fout , & is_pager );
772+ IsPagerNeeded (cont , extra_output_lines , false, & fout , & is_pager );
760773 }
761774
762775 /* time to output */
@@ -1125,6 +1138,7 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
11251138 dformatsize = 0 ;
11261139 struct lineptr * hlineptr ,
11271140 * dlineptr ;
1141+ bool is_pager = false;
11281142
11291143 if (cancel_pressed )
11301144 return ;
@@ -1139,6 +1153,13 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
11391153 return ;
11401154 }
11411155
1156+ /*
1157+ * Deal with the pager here instead of in printTable(), because we could
1158+ * get here via print_aligned_text() in expanded auto mode, and so we have
1159+ * to recalcuate the pager requirement based on vertical output.
1160+ */
1161+ IsPagerNeeded (cont , 0 , true, & fout , & is_pager );
1162+
11421163 /* Find the maximum dimensions for the headers */
11431164 for (i = 0 ; i < cont -> ncolumns ; i ++ )
11441165 {
@@ -1295,6 +1316,9 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
12951316 free (dlineptr -> ptr );
12961317 free (hlineptr );
12971318 free (dlineptr );
1319+
1320+ if (is_pager )
1321+ ClosePager (fout );
12981322}
12991323
13001324
@@ -2265,14 +2289,14 @@ printTableCleanup(printTableContent *const content)
22652289 * Setup pager if required
22662290 */
22672291static void
2268- IsPagerNeeded (const printTableContent * cont , const int extra_lines , FILE * * fout ,
2292+ IsPagerNeeded (const printTableContent * cont , const int extra_lines , bool expanded , FILE * * fout ,
22692293 bool * is_pager )
22702294{
22712295 if (* fout == stdout )
22722296 {
22732297 int lines ;
22742298
2275- if (cont -> opt -> expanded )
2299+ if (expanded )
22762300 lines = (cont -> ncolumns + 1 ) * cont -> nrows ;
22772301 else
22782302 lines = cont -> nrows + 1 ;
@@ -2310,11 +2334,10 @@ printTable(const printTableContent *cont, FILE *fout, FILE *flog)
23102334 if (cont -> opt -> format == PRINT_NOTHING )
23112335 return ;
23122336
2313- /* print_aligned_text() handles the pager itself */
2314- if ((cont -> opt -> format != PRINT_ALIGNED &&
2315- cont -> opt -> format != PRINT_WRAPPED ) ||
2316- cont -> opt -> expanded )
2317- IsPagerNeeded (cont , 0 , & fout , & is_pager );
2337+ /* print_aligned_*() handles the pager themselves */
2338+ if (cont -> opt -> format != PRINT_ALIGNED &&
2339+ cont -> opt -> format != PRINT_WRAPPED )
2340+ IsPagerNeeded (cont , 0 , (cont -> opt -> expanded == 1 ), & fout , & is_pager );
23182341
23192342 /* print the stuff */
23202343
@@ -2324,32 +2347,32 @@ printTable(const printTableContent *cont, FILE *fout, FILE *flog)
23242347 switch (cont -> opt -> format )
23252348 {
23262349 case PRINT_UNALIGNED :
2327- if (cont -> opt -> expanded )
2350+ if (cont -> opt -> expanded == 1 )
23282351 print_unaligned_vertical (cont , fout );
23292352 else
23302353 print_unaligned_text (cont , fout );
23312354 break ;
23322355 case PRINT_ALIGNED :
23332356 case PRINT_WRAPPED :
2334- if (cont -> opt -> expanded )
2357+ if (cont -> opt -> expanded == 1 )
23352358 print_aligned_vertical (cont , fout );
23362359 else
23372360 print_aligned_text (cont , fout );
23382361 break ;
23392362 case PRINT_HTML :
2340- if (cont -> opt -> expanded )
2363+ if (cont -> opt -> expanded == 1 )
23412364 print_html_vertical (cont , fout );
23422365 else
23432366 print_html_text (cont , fout );
23442367 break ;
23452368 case PRINT_LATEX :
2346- if (cont -> opt -> expanded )
2369+ if (cont -> opt -> expanded == 1 )
23472370 print_latex_vertical (cont , fout );
23482371 else
23492372 print_latex_text (cont , fout );
23502373 break ;
23512374 case PRINT_TROFF_MS :
2352- if (cont -> opt -> expanded )
2375+ if (cont -> opt -> expanded == 1 )
23532376 print_troff_ms_vertical (cont , fout );
23542377 else
23552378 print_troff_ms_text (cont , fout );
0 commit comments