Cursor in wrong position with text wrapping to multiple lines in GHCi
Summary
Text wrapping to a new line causes issues with the cursor position in GHCi; possibly related to #20022 (closed) (sorry if it's the same thing. I opened a new issue because that issue appears to have been fixed).
Steps to reproduce
- Run
ghci
- Run
:set prompt "\ESC[m"
- Type some relatively long text that still fits on one line, such as
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
- Resize the terminal so the text wraps to a new line
- Ctrl + A
- The cursor won't be at the start of the line (e.g. before
j
instead of beforea
) - Type anything. The entire input
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
appears after the cursor, overwriting what was previously there. For example, when the cursor was beforej
after Ctrl + A, typingA
results inabcdefghiAabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
. This persists even if you backspace the character you just entered - Hitting return reveals that the actual input GHCi sees is
Aabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
(because ofData constructor not in scope
for example), which matches the keystrokes done but not with what is displayed
- Up arrow → Ctrl + A results in the cursor being in the same incorrect position as before
- Down arrow: cursor remains in the incorrect position with text before the cursor still displayed (e.g.
Aabcdefghi
)
- Resizing the terminal back to the original size when
ghci
was invoked restores the terminal to normal behaviour
This only seems to occur with an escape sequence in the prompt. My prompt is \ESC[1;35mλ ❯ \ESC[m
, and when I removed the last \ESC[m
from it, the issue still occurred, leading me to think that this happens with all escape sequences.
I tested this on Hyper, Terminal.app, and the integrated terminal in Visual Studio Code.
This isn't the only issue that happens, but it was the easiest and simplest I could reproduce. Other issues include the text input overwriting the top message Loaded GHCi configuration from
, or overwriting the existing input on the previous line when typing and the text goes to a new line.
Edit
Issues also occur without resizing the terminal at all.
- Type some long text that wraps onto a new line and press return.
- Up arrow → Down arrow
- Some text from the input (e.g.
abcdefgh
) will still stay on the screen before the cursor, even though it's not actually part of the input.
Expected behavior
The cursor should be at the correct position at the beginning of the line, not someplace in the middle of it.
Environment
- GHC version used: 9.0.1
Optional:
- Operating System: macOS 11.5
- System Architecture: x86_64