I have often found myself wanting that psql automatically switch between normal and \x mode depending on the width of the output. Would others find this useful?
Attached is a crude demo patch. Enable with \pset expanded auto.
diff --git i/src/bin/psql/command.c w/src/bin/psql/command.c index c1edf44..7e24903 100644 --- i/src/bin/psql/command.c +++ w/src/bin/psql/command.c @@ -2129,14 +2129,21 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) /* set expanded/vertical mode */ else if (strcmp(param, "x") == 0 || strcmp(param, "expanded") == 0 || strcmp(param, "vertical") == 0) { - if (value) + if (value && pg_strcasecmp(value, "auto") == 0) + popt->topt.expanded = 2; + else if (value) popt->topt.expanded = ParseVariableBool(value); else popt->topt.expanded = !popt->topt.expanded; if (!quiet) - printf(popt->topt.expanded - ? _("Expanded display is on.\n") - : _("Expanded display is off.\n")); + { + if (popt->topt.expanded == 1) + printf(_("Expanded display is on.\n")); + else if (popt->topt.expanded == 2) + printf(_("Expanded display is used automatically.\n")); + else + printf(_("Expanded display is off.\n")); + } } /* locale-aware numeric output */ diff --git i/src/bin/psql/print.c w/src/bin/psql/print.c index c946076..07f27bc 100644 --- i/src/bin/psql/print.c +++ w/src/bin/psql/print.c @@ -122,9 +122,11 @@ const printTextFormat pg_utf8format = /* Local functions */ static int strlen_max_width(unsigned char *str, int *target_width, int encoding); -static void IsPagerNeeded(const printTableContent *cont, const int extra_lines, +static void IsPagerNeeded(const printTableContent *cont, const int extra_lines, bool expanded, FILE **fout, bool *is_pager); +static void print_aligned_vertical(const printTableContent *cont, FILE *fout); + static void * pg_local_malloc(size_t size) @@ -713,6 +715,13 @@ print_aligned_text(const printTableContent *cont, FILE *fout) } } + if (cont->opt->expanded == 2 && + (output_columns < total_header_width || output_columns < width_total)) + { + print_aligned_vertical(cont, fout); + return; + } + /* If we wrapped beyond the display width, use the pager */ if (!is_pager && fout == stdout && output_columns > 0 && (output_columns < total_header_width || output_columns < width_total)) @@ -756,7 +765,7 @@ print_aligned_text(const printTableContent *cont, FILE *fout) extra_row_output_lines = 0; } } - IsPagerNeeded(cont, extra_output_lines, &fout, &is_pager); + IsPagerNeeded(cont, extra_output_lines, false, &fout, &is_pager); } /* time to output */ @@ -2265,14 +2274,14 @@ printTableCleanup(printTableContent *const content) * Setup pager if required */ static void -IsPagerNeeded(const printTableContent *cont, const int extra_lines, FILE **fout, +IsPagerNeeded(const printTableContent *cont, const int extra_lines, bool expanded, FILE **fout, bool *is_pager) { if (*fout == stdout) { int lines; - if (cont->opt->expanded) + if (expanded) lines = (cont->ncolumns + 1) * cont->nrows; else lines = cont->nrows + 1; @@ -2313,8 +2322,8 @@ printTable(const printTableContent *cont, FILE *fout, FILE *flog) /* print_aligned_text() handles the pager itself */ if ((cont->opt->format != PRINT_ALIGNED && cont->opt->format != PRINT_WRAPPED) || - cont->opt->expanded) - IsPagerNeeded(cont, 0, &fout, &is_pager); + cont->opt->expanded == 1) + IsPagerNeeded(cont, 0, true, &fout, &is_pager); /* print the stuff */ @@ -2324,32 +2333,32 @@ printTable(const printTableContent *cont, FILE *fout, FILE *flog) switch (cont->opt->format) { case PRINT_UNALIGNED: - if (cont->opt->expanded) + if (cont->opt->expanded == 1) print_unaligned_vertical(cont, fout); else print_unaligned_text(cont, fout); break; case PRINT_ALIGNED: case PRINT_WRAPPED: - if (cont->opt->expanded) + if (cont->opt->expanded == 1) print_aligned_vertical(cont, fout); else print_aligned_text(cont, fout); break; case PRINT_HTML: - if (cont->opt->expanded) + if (cont->opt->expanded == 1) print_html_vertical(cont, fout); else print_html_text(cont, fout); break; case PRINT_LATEX: - if (cont->opt->expanded) + if (cont->opt->expanded == 1) print_latex_vertical(cont, fout); else print_latex_text(cont, fout); break; case PRINT_TROFF_MS: - if (cont->opt->expanded) + if (cont->opt->expanded == 1) print_troff_ms_vertical(cont, fout); else print_troff_ms_text(cont, fout); diff --git i/src/bin/psql/print.h w/src/bin/psql/print.h index 1f54075..b79a73b 100644 --- i/src/bin/psql/print.h +++ w/src/bin/psql/print.h @@ -70,8 +70,8 @@ typedef struct printTextFormat typedef struct printTableOpt { enum printFormat format; /* see enum above */ - bool expanded; /* expanded/vertical output (if supported by - * output format) */ + unsigned short int expanded;/* expanded/vertical output (if supported by + * output format); 0=no, 1=yes, 2=auto */ unsigned short int border; /* Print a border around the table. 0=none, * 1=dividing lines, 2=full */ unsigned short int pager; /* use pager for output (if to stdout and
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers