Count output lines automatically in psql/help.c

Started by Tom Lanealmost 4 years ago7 messageshackers
Jump to latest
#1Tom Lane
tgl@sss.pgh.pa.us

I finally reached the point of being fed up with our inability
to maintain the number of lines output by psql's usage() and
sibling functions. Almost every year, we find ourselves updating
those magic constants sometime late in the dev cycle, and I just
had to do it again today.

So, attached is a patch to remove that maintenance chore by
constructing the output in a PQExpBuffer and then counting the
lines automatically. While I was at it, I introduced a couple of
macros to make the code shorter rather than longer.

We could alternatively decide that we've blown past whatever
vertical screen space anybody has and just use 1000 or something
like that as the PageOutput count. However, that's a somewhat
dicey proposition for usage() itself, which is at 63 lines today;
that's well within reach of larger monitors.

Thoughts?

regards, tom lane

Attachments:

count-help-lines-automatically-1.patchtext/x-diff; charset=us-ascii; name=count-help-lines-automatically-1.patchDownload+447-397
#2Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#1)
Re: Count output lines automatically in psql/help.c

On Fri, Jun 3, 2022 at 4:51 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

Thoughts?

+1 from me. Wish we'd done this years ago.

--
Robert Haas
EDB: http://www.enterprisedb.com

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Robert Haas (#2)
Re: Count output lines automatically in psql/help.c

Robert Haas <robertmhaas@gmail.com> writes:

On Fri, Jun 3, 2022 at 4:51 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

Thoughts?

+1 from me. Wish we'd done this years ago.

Pushed, thanks for looking at it.

regards, tom lane

#4Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Tom Lane (#1)
Re: Count output lines automatically in psql/help.c

On 2022-Jun-03, Tom Lane wrote:

So, attached is a patch to remove that maintenance chore by
constructing the output in a PQExpBuffer and then counting the
lines automatically. While I was at it, I introduced a couple of
macros to make the code shorter rather than longer.

What about adding stringInfoCountLines or something like that?

--
Álvaro Herrera

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Alvaro Herrera (#4)
Re: Count output lines automatically in psql/help.c

Alvaro Herrera <alvherre@alvh.no-ip.org> writes:

What about adding stringInfoCountLines or something like that?

If we have other use-cases, maybe that'd be worthwhile.

(In the committed patch, I dumbed it down to a plain per-char
loop without the strchr() complication. So it's very little code.
I'm not real sure that strchr would make it faster.)

regards, tom lane

#6Peter Eisentraut
peter_e@gmx.net
In reply to: Tom Lane (#1)
Re: Count output lines automatically in psql/help.c

On 03.06.22 22:51, Tom Lane wrote:

+	HELP0("  -c, --command=COMMAND    run only single command (SQL or internal) and exit\n");
+	HELP("  -d, --dbname=DBNAME      database name to connect to (default: \"%s\")\n",
+		 env);

I wonder whether this mix of HELP0 and HELP is necessary. The original
code didn't care about calling fprintf even if there are no
substitutions. I think this could lead to misalignment errors. I
vaguely recall we once had mixes of fprintf and fputs and got rid of
them for this reason.

#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Eisentraut (#6)
Re: Count output lines automatically in psql/help.c

Peter Eisentraut <peter.eisentraut@enterprisedb.com> writes:

I wonder whether this mix of HELP0 and HELP is necessary. The original
code didn't care about calling fprintf even if there are no
substitutions. I think this could lead to misalignment errors. I
vaguely recall we once had mixes of fprintf and fputs and got rid of
them for this reason.

In the committed patch, I changed HELP to HELPN exactly so that
the strings would still line up.

regards, tom lane