From e12ed8cad1c672488a64a44183c8a7b3154a57ca Mon Sep 17 00:00:00 2001 From: Erik Wienhold Date: Tue, 9 Sep 2025 02:36:49 +0200 Subject: [PATCH v3 3/3] psql: Fix counting of header and footer lines in pager setup * The table header only produces extra lines when printed in normal mode. So don't count those lines in tuples_only mode or expanded mode. * Count the lines of the table title, if present. * Count all footer lines instead of treating each footer as a single line. --- src/fe_utils/print.c | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/fe_utils/print.c b/src/fe_utils/print.c index 82f4fc9af20..94043fae663 100644 --- a/src/fe_utils/print.c +++ b/src/fe_utils/print.c @@ -3394,15 +3394,8 @@ IsPagerNeeded(const printTableContent *cont, unsigned int *width_wrap, NULL, &nl_lines, NULL); header_height[i] = nl_lines; - - if (nl_lines > max_lines) - max_lines = nl_lines; } - /* Add height of tallest header column */ - lines += max_lines; - max_lines = 0; - /* Scan all cells to count their lines */ for (i = 0, cell = cont->cells; *cell; cell++) { @@ -3449,12 +3442,34 @@ IsPagerNeeded(const printTableContent *cont, unsigned int *width_wrap, { printTableFooter *f; - /* - * FIXME -- this is slightly bogus: it counts the number of - * footers, not the number of lines in them. - */ + if (cont->title) + { + pg_wcssize((const unsigned char *) cont->title, strlen(cont->title), + cont->opt->encoding, NULL, &nl_lines, NULL); + lines += nl_lines; + } + + if (!expanded) + { + /* Find the tallest header column */ + for (i = 0; i < cont->ncolumns; i++) + { + if (header_height[i] > max_lines) + max_lines = header_height[i]; + } + + /* Add height of tallest header column */ + lines += max_lines; + max_lines = 0; + } + + /* Count all footer lines */ for (f = cont->footers; f; f = f->next) - lines++; + { + pg_wcssize((const unsigned char *) f->data, strlen(f->data), + cont->opt->encoding, NULL, &nl_lines, NULL); + lines += nl_lines; + } } *fout = PageOutput(lines, cont->opt); -- 2.51.0