zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: draw prompt on the correct line after window change
@ 2019-07-15 17:20 Roman Perepelitsa
  2019-07-15 19:14 ` Mikael Magnusson
                   ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Roman Perepelitsa @ 2019-07-15 17:20 UTC (permalink / raw)
  To: Zsh hackers list

[-- Attachment #1: Type: text/plain, Size: 1636 bytes --]

Many terminals reflow text when the window is resized. When the height
of the prompt changes as a result of this reflowing, ZSH draws the
updated prompt on the wrong line. This can lead to some parts of the
prompt not being erased, or to the disappearance of lines prior to the
prompt.

There are many ways to reproduce this issue. Here are a couple. Both
require terminals that reflow text when the window is resized.

1. Run `zsh -df`, hit <enter> a few times, then type
  `xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx` (don't hit <enter>).

  OR

2. Run `PROMPT="${(pl.$COLUMNS..-.)}%f"$'\n> ' zsh -df`
  and hit <enter> a few times.

Now try resizing the terminal window back and forth causing lines to
wrap and unwrap. Terminal content before the last prompt will be
erased one line at a time.

This patch cannot handle the case when the terminal window is being
resized while the first prompt line is outside the terminal window.
The content of the viewport will be correct but scrolling the terminal
window up will reveal some mess up there. ZSH before this patch also
fails in this case although it creates a different mess.

The change is conservative. The new code triggers only on window
resize and not, for example, on redisplay. This reduces the chance
that it'll break something that isn't currently broken.

I've tested this code only on GNOME Terminal. Before I go testing on a
dozen different terminals I'd like to get some feedback. Anything I'm
missing? Anything tricky to look out for?

The patch is attached to the email. You can also see it in
https://github.com/zsh-users/zsh/compare/master...romkatv:fix-winchanged.

Roman.

[-- Attachment #2: window-resize-fix.patch --]
[-- Type: text/x-patch, Size: 2390 bytes --]

diff --git a/Src/Zle/zle_refresh.c b/Src/Zle/zle_refresh.c
index 85e55e0d4..249d75d71 100644
--- a/Src/Zle/zle_refresh.c
+++ b/Src/Zle/zle_refresh.c
@@ -660,6 +660,7 @@ static int more_start,		/* more text before start of screen?	    */
     lpromptw, rpromptw,		/* prompt widths on screen                  */
     lpromptwof,			/* left prompt width with real end position */
     lprompth,			/* lines taken up by the prompt		    */
+    cursorsaved,                /* whether prompt start position was saved  */
     rprompth,			/* right prompt height                      */
     vcs, vln,			/* video cursor position column & line	    */
     vmaxln,			/* video maximum number of lines	    */
@@ -994,6 +995,7 @@ zrefresh(void)
     int remetafy;		/* flag that zle line is metafied	     */
     zattr txtchange;		/* attributes set after prompts              */
     int rprompt_off = 1;	/* Offset of rprompt from right of screen    */
+    int savecursorneeded = 0;	/* prompt start position needs to be saved   */
     struct rparams rpms;
 #ifdef MULTIBYTE_SUPPORT
     int width;			/* width of wide character		     */
@@ -1133,7 +1135,13 @@ zrefresh(void)
 	zsetterm();
 #ifdef TIOCGWINSZ
 	if (winchanged) {
-	    moveto(0, 0);
+	    if (cursorsaved) {
+		tcout(TCRESTRCURSOR);
+		zputc(&zr_cr);
+		vln = vcs = 0;
+	    } else {
+		moveto(0, 0);
+	    }
 	    t0 = olnct;		/* this is to clear extra lines even when */
 	    winchanged = 0;	/* the terminal cannot TCCLEAREOD	  */
 	    listshown = 0;
@@ -1164,7 +1172,16 @@ zrefresh(void)
         if (termflags & TERM_SHORT)
             vcs = 0;
 	else if (!clearflag && lpromptbuf[0]) {
-            zputs(lpromptbuf, shout);
+	    cursorsaved = 0;
+	    if (tccan(TCSAVECURSOR) && tccan(TCRESTRCURSOR)) {
+		if (!(termflags & TERM_SHORT)) {
+		    savecursorneeded = 1;
+                } else if (lprompth <= rwinh) {
+		    tcout(TCSAVECURSOR);
+		    cursorsaved = 1;
+                }
+	    }
+	    zputs(lpromptbuf, shout);
 	    if (lpromptwof == winw)
 		zputs("\n", shout);	/* works with both hasam and !hasam */
 	} else {
@@ -1737,6 +1754,12 @@ individually */
     clearf = 0;
     oput_rpmpt = put_rpmpt;
 
+    if (savecursorneeded && lprompth + nlnct <= rwinh) {
+	moveto(1 - lprompth, 0);
+	tcout(TCSAVECURSOR);
+	cursorsaved = 1;
+    }
+
 /* move to the new cursor position */
     moveto(rpms.nvln, rpms.nvcs);
 

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2020-02-01 17:23 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-15 17:20 PATCH: draw prompt on the correct line after window change Roman Perepelitsa
2019-07-15 19:14 ` Mikael Magnusson
2019-07-15 19:32   ` Roman Perepelitsa
2019-07-15 19:41     ` Roman Perepelitsa
2019-07-15 21:24 ` Bart Schaefer
2019-07-15 22:09   ` Roman Perepelitsa
2019-07-15 22:11     ` Roman Perepelitsa
2019-07-15 23:28     ` Bart Schaefer
2019-07-16  8:16       ` Roman Perepelitsa
2019-07-16  0:01     ` Bart Schaefer
2020-01-06 17:50 ` Sebastian Gniazdowski
2020-01-06 18:07   ` Roman Perepelitsa
2020-01-06 18:31     ` Sebastian Gniazdowski
2020-01-06 18:46       ` Roman Perepelitsa
2020-01-06 19:02         ` Sebastian Gniazdowski
2020-01-06 19:28           ` Bart Schaefer
2020-01-06 19:38             ` Roman Perepelitsa
2020-02-01 17:22               ` Roman Perepelitsa
2020-01-06 19:40             ` Sebastian Gniazdowski

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).