On Mon, May 9, 2022 at 7:07 PM Pavel Stehule <pavel.steh...@gmail.com> wrote:
> Ășt 13. 7. 2021 v 19:50 odesĂ­latel Tom Lane <t...@sss.pgh.pa.us> napsal:
>> ^Cregression=#
>>
>> then as you can see I get nothing but the "^C" echo before the next
>> psql prompt.  The problem with this is that now libreadline is
>> misinformed about the cursor position, messing up any editing I
>> might try to do on the next line of input.  So I think it would
>> be a good idea to have some explicit final output when the \watch
>> command terminates, along the line of
>>
>> ...
>> Tue Jul 13 13:44:46 2021 (every 2s)
>>
>>               now
>> -------------------------------
>>  2021-07-13 13:44:46.396572-04
>> (1 row)
>>
>> ^C\watch cancelled
>> regression=#
>>
>> This strikes me as a usability improvement even without the
>> readline-confusion angle.
>
> here is an patch

I played with this.  On a libedit build (tested on my Mac), an easy
way to see corruption is to run eg SELECT;, then \watch 1, then ^C,
then up-arrow to see the previous command clobber the wrong columns.
On a libreadline build (tested on my Debian box), that simple test
doesn't fail in the same way.  Though there may be some other way to
make it misbehave that would take me longer to find, it's enough for
me that libedit is befuddled by what we're doing.

Do we really need the extra text?  What about just \n, so you get:

postgres=# \watch 1
...blah blah...
^C
postgres=#

This affects all release branches too.  Should we bother to fix this
there?  For them, I think the fix is just:

diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index d1ee795cb6..3a88d5d6c4 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -4992,6 +4992,9 @@ do_watch(PQExpBuffer query_buf, double sleep)
        sigint_interrupt_enabled = false;
    }

+   fprintf(pset.queryFout, "\n");
+   fflush(pset.queryFout);
+
    pg_free(title);
    return (res >= 0);
 }
From 409a5ce2d81c6fd976ff7cc0ed4de50197d4ce55 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.mu...@gmail.com>
Date: Tue, 7 Jun 2022 14:18:51 +1200
Subject: [PATCH] Fix \watch's interaction with libedit/libreadline.

When you hit ^C, the terminal driver in Unix-like systems echos "^C" as
well as sending an interrupt signal (depending on stty settings).  The
line editing library (libedit/libreadline) is then confused about the
current cursor location, and might corrupt the display.  Fix, by moving
to a new line before the next prompt is displayed.

Author: Pavel Stehule <pavel.steh...@gmail.com>
Reported-by: Tom Lane <t...@sss.pgh.pa.us>
Discussion: https://postgr.es/m/3278793.1626198638%40sss.pgh.pa.us
---
 src/bin/psql/command.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index b51d28780b..5a974a5ad9 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -5169,6 +5169,11 @@ do_watch(PQExpBuffer query_buf, double sleep)
 		pclose(pagerpipe);
 		restore_sigpipe_trap();
 	}
+	else
+	{
+		fprintf(stdout, "\n");
+		fflush(stdout);
+	}
 
 #ifdef HAVE_POSIX_DECL_SIGWAIT
 	/* Disable the interval timer. */
-- 
2.35.1

Reply via email to