On Wed, 27 Nov 2019 at 17:09, Tom Lane <t...@sss.pgh.pa.us> wrote:

> Good idea, but I think you need to account for "visible" (ie, if the
> newline is inside RL_PROMPT_START_IGNORE, it shouldn't change the width).
> It might be best to add logic inside the existing "if (visible)" instead
> of making a new top-level case.
>

Right, I assumed that it was safe given that only terminal control
characters were invisible.
Since the title of the terminal window can be changed as well via control
characters, it's probably better not to make that assumption.

I updated the patch accordingly.


> Another special case that somebody's likely to whine about is \t, though
> to handle that we'd have to make assumptions about the tab stop distance.
> Maybe assuming that it's 8 is good enough.
>

The problem with tabs is that any user can set their tabstops to whatever
they want, and a tab doesn't have a fixed width, it just goes up to the
next tab stop.
One way to do it would be to add tabs wherever necessary in prompt2 to make
sure they have the same size as in prompt1 (a list of numbers of spaces,
which we would concatenate with a tab?), but I'm not sure it's worth the
effort.
commit 491cf173aad247299622796775feea580a8f9b13 (HEAD -> refs/heads/patch_psql_prompt)
Author: Maxence Ahlouche <maxence.ahlou...@gmail.com>
Date:   Wed Nov 27 16:21:35 2019 +0100

    Fix %w length in PROMPT2 when PROMPT1 contains a newline, in psql.
    
    The width of the invisible PROMPT2 must take into account, in order for user input to be aligned with the first line, that PROMPT1 can contain newlines.

diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c
index 41c6f21ecf..24efb8f686 100644
--- a/src/bin/psql/prompt.c
+++ b/src/bin/psql/prompt.c
@@ -379,7 +379,10 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
 				if (visible)
 				{
 					chwidth = PQdsplen(p, pset.encoding);
-					if (chwidth > 0)
+
+					if (*p == '\n')
+						last_prompt1_width = 0;
+					else if (chwidth > 0)
 						last_prompt1_width += chwidth;
 				}
 

Reply via email to