Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
daanx opened this issue Aug 2, 2021 · 8 comments
Closed
Labels
Area-Output Related to output processing (inserting text into buffer, retrieving buffer text, etc.) Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting No-Recent-Activity This issue/PR is going stale and may be auto-closed without further activity. Priority-2 A description (P2) Product-Terminal The new Windows Terminal.

Comments

@daanx
Copy link

daanx commented 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 �[33mlong�[0m line.
set /p "ANSWER=Press <enter> to stop ..�[1F"
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.

@ghost ghost added Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Tag-Fix Doesn't match tag requirements labels Aug 2, 2021
@zadjii-msft zadjii-msft added the Product-Terminal The new Windows Terminal. label Aug 2, 2021
@zadjii-msft
Copy link
Member

zadjii-msft commented 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 zadjii-msft added Area-Output Related to output processing (inserting text into buffer, retrieving buffer text, etc.) Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-2 A description (P2) labels Aug 2, 2021
@ghost ghost removed the Needs-Tag-Fix Doesn't match tag requirements label Aug 2, 2021
@zadjii-msft zadjii-msft added this to the Terminal v2.0 milestone Aug 2, 2021
@zadjii-msft zadjii-msft removed the Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting label Aug 5, 2021
@zadjii-msft
Copy link
Member

zadjii-msft commented 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
Copy link
Member

zadjii-msft commented 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

@daxian-dbw
Copy link
Member

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

@zadjii-msft zadjii-msft modified the milestones: Terminal v2.0, 22H2 Jan 4, 2022
@zadjii-msft zadjii-msft modified the milestones: 22H2, Backlog Jul 5, 2023
@lhecker
Copy link
Member

lhecker commented Aug 15, 2023

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

@lhecker lhecker added the Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting label Aug 15, 2023
@zadjii-msft zadjii-msft modified the milestones: Backlog, Terminal v1.19 Aug 15, 2023
@zadjii-msft zadjii-msft added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Aug 15, 2023
@lhecker
Copy link
Member

lhecker commented 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.

@microsoft-github-policy-service
Copy link
Contributor

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.

1 similar comment
@microsoft-github-policy-service
Copy link
Contributor

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 microsoft-github-policy-service bot added No-Recent-Activity This issue/PR is going stale and may be auto-closed without further activity. labels Aug 19, 2023
@microsoft-github-policy-service microsoft-github-policy-service bot removed this from the Terminal v1.19 milestone Aug 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Output Related to output processing (inserting text into buffer, retrieving buffer text, etc.) Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting No-Recent-Activity This issue/PR is going stale and may be auto-closed without further activity. Priority-2 A description (P2) Product-Terminal The new Windows Terminal.
Projects
None yet
Development

No branches or pull requests

4 participants