Cursor position is lost when resizing the window if not on the bottom line #14761

Closed
opened 2026-01-31 04:18:45 +00:00 by claunia · 8 comments
Owner

Originally created by @daanx on GitHub (Aug 2, 2021).

Windows Terminal version (or Windows build number)

10.0.19042.1110 (windows terminal 1.9.1942.0)

Other Software

No response

Steps to reproduce

If the cursor position is not on the last line (of the input), making the screen more narrow will push the cursor down to the last line when lines wrap and the original position is lost; on a resize to make it wider again it stays on the last line.

This affects utilities like "readline" when editing multiple lines of input. Hope it can be fixed as it makes multiline editing not work at all.

To reproduce, run the following batch file:

@echo off
echo Resize the window to make it more narrow and wider again.
echo The cursor will lose its position and move down.
echo.
echo The cursor is first positioned at the start of this long line.
set /p "ANSWER=Press <enter> to stop .."
echo.
echo Thank you.

(note: I think the paste lost it but there should be an ESC character in front of the [ characters to make the escape sequence function; the ESC[1F moves the cursor one up so it is no longer on the last line.)

Expected Behavior

The cursor should always stay at the same point in the input regardless of a window resize. The "old" cmd terminal is correct and the cursor stays at the same point..

Actual Behavior

cursor-bug-cmd

Here you see the cursor in Windows Terminal (I made it red) move down a line on the resize and lose its position.

Originally created by @daanx on GitHub (Aug 2, 2021). ### Windows Terminal version (or Windows build number) 10.0.19042.1110 (windows terminal 1.9.1942.0) ### Other Software _No response_ ### Steps to reproduce If the cursor position is not on the last line (of the input), making the screen more narrow will push the cursor down to the last line when lines wrap and the original position is lost; on a resize to make it wider again it stays on the last line. This affects utilities like "readline" when editing multiple lines of input. Hope it can be fixed as it makes multiline editing not work at all. To reproduce, run the following batch file: ```bat @echo off echo Resize the window to make it more narrow and wider again. echo The cursor will lose its position and move down. echo. echo The cursor is first positioned at the start of this long line. set /p "ANSWER=Press <enter> to stop .." echo. echo Thank you. ``` (note: I think the paste lost it but there should be an `ESC` character in front of the `[` characters to make the escape sequence function; the `ESC[1F` moves the cursor one up so it is no longer on the last line.) ### Expected Behavior The cursor should always stay at the same point in the input regardless of a window resize. The "old" cmd terminal is correct and the cursor stays at the same point.. ### Actual Behavior ![cursor-bug-cmd](https://user-images.githubusercontent.com/2171100/127802782-ae3a8a8a-5d30-4dbd-92ac-e41ef3726d4a.png) Here you see the cursor in Windows Terminal (I made it red) move down a line on the resize and lose its position.
Author
Owner

@zadjii-msft commented on GitHub (Aug 2, 2021):

Thanks for the helpful repro case! I'm gonna link this up to #5800, because this might be a repro for a heisenbug I've been hunting. I think #6546 is the one I'm thinking of

@zadjii-msft commented on GitHub (Aug 2, 2021): Thanks for the helpful repro case! I'm gonna link this up to #5800, because this might be a repro for a heisenbug I've been hunting. I think #6546 is the one I'm thinking of
Author
Owner

@zadjii-msft commented on GitHub (Aug 19, 2021):

Okay my notes:
this repros when the line is close enough to the bottom line of the terminal window, that the Press <enter> line will get placed on exactly the bottom line. When the long line wraps (which will create a blank line between the long line and the <enter> line), then the cursor gets left at the start of the <enter> line, instead of the long one.

Curiously, in conhost, if the buffer size == viewport size, then the cursor gets left at the end of the long line. It moves down one row, to the created blank one, then back up to the end of the long line when you de-flow the long line.
gh-10848-000

This does not occur if the viewport is above the bottom of the buffer.

Oh but interesting. If there's two outputs of gh-10848.bat in the buffer, and view==buffer, then it will repro exactly the same as this bug report. Interesting.
gh-10848-001

@zadjii-msft commented on GitHub (Aug 19, 2021): Okay my notes: this repros when the line is close enough to the bottom line of the terminal window, that the `Press <enter>` line will get placed on exactly the bottom line. When the `long` line wraps (which will create a blank line between the `long` line and the `<enter>` line), then the cursor gets left at the start of the `<enter>` line, instead of the `long` one. Curiously, in `conhost`, if the buffer size == viewport size, then the cursor gets left at the _end_ of the `long` line. It moves down one row, to the created blank one, then back up to the end of the `long` line when you de-flow the long line. ![gh-10848-000](https://user-images.githubusercontent.com/18356694/130109718-6a88fce5-c803-4d51-b635-731ce8ca89e6.gif) This does _not_ occur if the viewport is above the bottom of the buffer. Oh but interesting. If there's two outputs of `gh-10848.bat` in the buffer, and view==buffer, then it will repro exactly the same as this bug report. Interesting. ![gh-10848-001](https://user-images.githubusercontent.com/18356694/130109813-eab4e603-0e2d-4271-9bb0-335b861558e1.gif)
Author
Owner

@zadjii-msft commented on GitHub (Aug 19, 2021):

This is because we'll end up circling the buffer during the resize, after we found the cursor position.

Say the window has 20 rows. That puts the cursor on row 18. When we resize down, and reflow the buffer, we'll look for the old cursor. We'll find it at (0,18) in the old buffer, when we're writing to (0,19) in the new buffer. This is because long line 1 already reflowed, creating an extra row in the buffer.

So we'll stash (0,19) as the expected cursor position in the new buffer. We'll reflow this whole line into the buffer, which will cause us to circle! The place the cursor should really be is line 17, not 19.

I'm putting this in the same bucket as #10130/#6546. I think this is gonna just go away with #8000, though we're probably "out of the frying pan, into the fire" with that one

@zadjii-msft commented on GitHub (Aug 19, 2021): This is because we'll end up circling the buffer during the resize, _after_ we found the cursor position. Say the window has 20 rows. That puts the cursor on row 18. When we resize down, and reflow the buffer, we'll look for the old cursor. We'll find it at (0,18) in the old buffer, when we're writing to (0,19) in the new buffer. This is because `long` line 1 already reflowed, creating an extra row in the buffer. So we'll stash (0,19) as the expected cursor position in the new buffer. We'll reflow this whole line into the buffer, which will cause us to circle! The place the cursor should _really_ be is line 17, not 19. I'm putting this in the same bucket as #10130/#6546. I think this is gonna just go away with #8000, though we're probably _"out of the frying pan, into the fire"_ with that one
Author
Owner

@daxian-dbw commented on GitHub (Nov 30, 2021):

I observed the same. Basically, the cursor is not guaranteed to point to the same character that it was originally pointing at when the window resizing happens. This happens often enough, and makes it difficult for PowerShell to calculate the position of the initial prompt after a resizing.

Animation

@daxian-dbw commented on GitHub (Nov 30, 2021): I observed the same. Basically, the cursor is not guaranteed to point to the same character that it was originally pointing at when the window resizing happens. This happens often enough, and makes it difficult for PowerShell to calculate the position of the initial prompt after a resizing. ![Animation](https://user-images.githubusercontent.com/127450/143963370-2d777e14-13f1-4dfb-8183-2d5f29cafef2.gif)
Author
Owner

@lhecker commented on GitHub (Aug 15, 2023):

Same here... I can't seem to reproduce this issue in 1.17/1.18. Did we fix this?

@lhecker commented on GitHub (Aug 15, 2023): Same here... I can't seem to reproduce this issue in 1.17/1.18. Did we fix this?
Author
Owner

@lhecker commented on GitHub (Aug 15, 2023):

(Sorry for the other comment I posted here - I meant to post it in #10868.)

I believe this issue as described is fixed, but #14448 gave me an idea for reproducing a similar, probably related issue.
Using this .bat file:

@echo off
set /p "ANSWER=Could you share maybe a screenshot/? What shell are you using, and/or what program is doing the outputting? I bet this is #3088 just based on the description, but I can't be sure."

and by resizing the window randomly it is possible to place the cursor in the wrong position. From what I can tell, the issue is due to a desync between ConPTY and Windows Terminal, because Windows Terminal doesn't resize its buffer at the same time as ConPTY. This most obvious when actively printing text of course, but it also happens when regularly resizing. I believe my PR #15701 exacerbates this issue, because the reflow is so much faster giving more time for a desync to happen.

Edit: Additionally, there's the issue that ConPTY has no scrollback and so line wrapping behaves differently leading to #14448. But what I meant is an actual cursor misplacement even if the buffer is mostly empty. Here for instance, I just have 2 long lines and never hit the scrollback in ConPTY, and still, the output is corrupted ("ConsoleMonitor" shows the internal buffer of ConPTY):

image

But as I said, I believe that the two issues you filed, as they're described, are fixed, even if reflow in general is still buggy.

@lhecker commented on GitHub (Aug 15, 2023): (Sorry for the other comment I posted here - I meant to post it in #10868.) I believe this issue as described is fixed, but #14448 gave me an idea for reproducing a similar, probably related issue. Using this .bat file: ```bat @echo off set /p "ANSWER=Could you share maybe a screenshot/? What shell are you using, and/or what program is doing the outputting? I bet this is #3088 just based on the description, but I can't be sure." ``` and by resizing the window randomly it is possible to place the cursor in the wrong position. From what I can tell, the issue is due to a desync between ConPTY and Windows Terminal, because Windows Terminal doesn't resize its buffer at the same time as ConPTY. This most obvious when actively printing text of course, but it also happens when regularly resizing. I believe my PR #15701 exacerbates this issue, because the reflow is so much faster giving more time for a desync to happen. Edit: Additionally, there's the issue that ConPTY has no scrollback and so line wrapping behaves differently leading to #14448. But what I meant is an actual cursor misplacement even if the buffer is mostly empty. Here for instance, I just have 2 long lines and never hit the scrollback in ConPTY, and still, the output is corrupted ("ConsoleMonitor" shows the internal buffer of ConPTY): ![image](https://github.com/microsoft/terminal/assets/2256941/f47d4b7c-b0e0-415b-9f1e-0cd97d1169a3) But as I said, I believe that the two issues you filed, as they're described, are fixed, even if reflow in general is still buggy.
Author
Owner

@microsoft-github-policy-service[bot] commented on GitHub (Aug 19, 2023):

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@microsoft-github-policy-service[bot] commented on GitHub (Aug 19, 2023): This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for **4 days**. It will be closed if no further activity occurs **within 3 days of this comment**.
Author
Owner

@microsoft-github-policy-service[bot] commented on GitHub (Aug 19, 2023):

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@microsoft-github-policy-service[bot] commented on GitHub (Aug 19, 2023): This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for **4 days**. It will be closed if no further activity occurs **within 3 days of this comment**.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#14761