diff options
author | Andrew Dunstan <andrew@dunslane.net> | 2022-07-25 14:24:50 -0400 |
---|---|---|
committer | Andrew Dunstan <andrew@dunslane.net> | 2022-07-25 14:25:02 -0400 |
commit | a45388d6e0984abb02074f0300cd9c5cbda13848 (patch) | |
tree | 95689aab9fda59493f9f119f9e45b5f22f0d4fab /src/fe_utils/print.c | |
parent | b35617de37870756bdb0e00ffc0a42441e56eefa (diff) | |
download | postgresql-a45388d6e0984abb02074f0300cd9c5cbda13848.tar.gz postgresql-a45388d6e0984abb02074f0300cd9c5cbda13848.zip |
Add xheader_width pset option to psql
The setting controls tha maximum length of the header line in expanded
format output. Possible settings are full, column, page, or an integer.
the default is full, the current behaviour, and in this case the header
line is the length of the widest line of output. column causes the
header to be truncated to the width of the first column, page causes it
to be truncated to the width of the terminal page, and an integer causes
it to be truncated to that value. If the full value is less than the
page or integer value no truncation occurs. If given without an argument
this option prints its current setting.
Platon Pronko, somewhat modified by me.
Discussion: https://postgr.es/m/f03d38a3-db96-a56e-d1bc-dbbc80bbde4d@gmail.com
Diffstat (limited to 'src/fe_utils/print.c')
-rw-r--r-- | src/fe_utils/print.c | 77 |
1 files changed, 60 insertions, 17 deletions
diff --git a/src/fe_utils/print.c b/src/fe_utils/print.c index fe676a971b9..a48c9196978 100644 --- a/src/fe_utils/print.c +++ b/src/fe_utils/print.c @@ -1222,15 +1222,16 @@ cleanup: static void -print_aligned_vertical_line(const printTextFormat *format, - const unsigned short opt_border, +print_aligned_vertical_line(const printTableOpt *topt, unsigned long record, unsigned int hwidth, unsigned int dwidth, + int output_columns, printTextRule pos, FILE *fout) { - const printTextLineFormat *lformat = &format->lrule[pos]; + const printTextLineFormat *lformat = &get_line_style(topt)->lrule[pos]; + const unsigned short opt_border = topt->border; unsigned int i; int reclen = 0; @@ -1259,8 +1260,18 @@ print_aligned_vertical_line(const printTextFormat *format, if (reclen-- <= 0) fputs(lformat->hrule, fout); if (reclen-- <= 0) - fputs(lformat->midvrule, fout); - if (reclen-- <= 0) + { + if (topt->expanded_header_width_type == PRINT_XHEADER_COLUMN) + { + fputs(lformat->rightvrule, fout); + } + else + { + fputs(lformat->midvrule, fout); + } + } + if (reclen-- <= 0 + && topt->expanded_header_width_type != PRINT_XHEADER_COLUMN) fputs(lformat->hrule, fout); } else @@ -1268,12 +1279,43 @@ print_aligned_vertical_line(const printTextFormat *format, if (reclen-- <= 0) fputc(' ', fout); } - if (reclen < 0) - reclen = 0; - for (i = reclen; i < dwidth; i++) - fputs(opt_border > 0 ? lformat->hrule : " ", fout); - if (opt_border == 2) - fprintf(fout, "%s%s", lformat->hrule, lformat->rightvrule); + + if (topt->expanded_header_width_type != PRINT_XHEADER_COLUMN) + { + if (topt->expanded_header_width_type == PRINT_XHEADER_PAGE + || topt->expanded_header_width_type == PRINT_XHEADER_EXACT_WIDTH) + { + if (topt->expanded_header_width_type == PRINT_XHEADER_EXACT_WIDTH) + { + output_columns = topt->expanded_header_exact_width; + } + if (output_columns > 0) + { + if (opt_border == 0) + dwidth = Min(dwidth, Max(0, (int) (output_columns - hwidth))); + if (opt_border == 1) + dwidth = Min(dwidth, Max(0, (int) (output_columns - hwidth - 3))); + /* + * Handling the xheader width for border=2 doesn't make + * much sense because this format has an additional + * right border, but keep this for consistency. + */ + if (opt_border == 2) + dwidth = Min(dwidth, Max(0, (int) (output_columns - hwidth - 7))); + } + } + + if (reclen < 0) + reclen = 0; + if (dwidth < reclen) + dwidth = reclen; + + for (i = reclen; i < dwidth; i++) + fputs(opt_border > 0 ? lformat->hrule : " ", fout); + if (opt_border == 2) + fprintf(fout, "%s%s", lformat->hrule, lformat->rightvrule); + } + fputc('\n', fout); } @@ -1570,11 +1612,12 @@ print_aligned_vertical(const printTableContent *cont, lhwidth++; /* for newline indicators */ if (!opt_tuples_only) - print_aligned_vertical_line(format, opt_border, record++, - lhwidth, dwidth, pos, fout); + print_aligned_vertical_line(cont->opt, record++, + 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); + print_aligned_vertical_line(cont->opt, 0, lhwidth, + dwidth, output_columns, pos, fout); } /* Format the header */ @@ -1760,8 +1803,8 @@ print_aligned_vertical(const printTableContent *cont, if (cont->opt->stop_table) { if (opt_border == 2 && !cancel_pressed) - print_aligned_vertical_line(format, opt_border, 0, hwidth, dwidth, - PRINT_RULE_BOTTOM, fout); + print_aligned_vertical_line(cont->opt, 0, hwidth, dwidth, + output_columns, PRINT_RULE_BOTTOM, fout); /* print footers */ if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed) |