Repository: microsoft/terminal (language: cpp)

Issue:
Cursor movement is not always detected on double-width lines
### Windows Terminal version

1.21.1272.0

### Windows build number

10.0.19045.4291

### Other Software

_No response_

### Steps to reproduce

1. Open a WSL bash shell in Window Terminal
2. Execute the following statement:
   ```
   printf "\e[2 q\e#6\e[11G"; for i in {1..10}; do printf "\e[D"; sleep 1; done
   ```


### Expected Behavior

This starts by setting the cursor style to a steady block so it's easy to see, then positions the cursor in column 11 and moves it left by 1 column every second until it reaches column 1.

### Actual Behavior

When it gets to column 3 is pauses for 2 seconds before jumping to column 1. You never see the cursor in column 2.

This is because of the way conpty determines whether the cursor has moved in the `InvalidateCursor` method here:

https://github.com/microsoft/terminal/blob/49e4eea60f737b46b8aeda505f4693df8a9d44a6/src/renderer/vt/invalidate.cpp#L78

The `psrRegion` is in screen coordinates, while the `_lastText` position in buffer coordinates. So on a double-width line, when moving from column 3 to column 2, the `_lastText` column is 3, and the `psrRegion->origin` is also 3, because buffer column 2 spans screen columns 3 and 4. Thus it seems like no movement has occurred.

This was another bug introduced by PR #17194.


Discussion / hints:
Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

### Open similar issues:

- [Conpty cursor mispositioned when using double width lines (#15014)](https://github.com/microsoft/terminal/issues/15014),  similarity score: 0.80

### Closed similar issues:
- [Issues with cursor invalidation in GDI (#17150)](https://github.com/microsoft/terminal/issues/17150),  similarity score: 0.81
- [Cursor droppings on wide chars in Windows Terminal (#14657)](https://github.com/microsoft/terminal/issues/14657),  similarity score: 0.79
- [More cursor droppings (#8213)](https://github.com/microsoft/terminal/issues/8213),  similarity score: 0.76

> Note: You can give me feedback by thumbs upping or thumbs downing this comment.

Fix patch:
diff --git a/src/renderer/vt/invalidate.cpp b/src/renderer/vt/invalidate.cpp
index 942ba58df26..d67ca839437 100644
--- a/src/renderer/vt/invalidate.cpp
+++ b/src/renderer/vt/invalidate.cpp
@@ -75,7 +75,8 @@ CATCH_RETURN();
     }
     _skipCursor = false;
 
-    _cursorMoved = psrRegion->origin() != _lastText;
+    _cursorMoved = psrRegion->origin() != _lastCursorOrigin;
+    _lastCursorOrigin = psrRegion->origin();
     return S_OK;
 }
 
diff --git a/src/renderer/vt/vtrenderer.hpp b/src/renderer/vt/vtrenderer.hpp
index 26fc67cb138..2641614b32d 100644
--- a/src/renderer/vt/vtrenderer.hpp
+++ b/src/renderer/vt/vtrenderer.hpp
@@ -111,6 +111,7 @@ namespace Microsoft::Console::Render
         til::pmr::bitmap _invalidMap;
 
         til::point _lastText;
+        til::point _lastCursorOrigin;
         til::point _scrollDelta;
 
         bool _clearedAllThisFrame;


Test patch: