C
C#2w ago
polemic

Console.SetCursorPosition does not work properly on windows terminal.

On windows terminal, SetCursorPosition sets the cursor relevant to where the console is scrolled to. On console host this behaviour does not happen. I want to disable this behaviour or find a workaround for windows terminal that lets me use SetCursorPosition to change the console position freely. I have also tried using an ANSI escape sequence, this does not seem to work either.
11 Replies
Sehra
Sehra2w ago
\x1b[2J
polemic
polemicOP2w ago
thats the ansi escape sequence for clearing the terminal can you tell me how i use that? does that fix the relative thing
Sehra
Sehra2w ago
it's not really clear from your description what works, what doesn't, and what you expect
polemic
polemicOP2w ago
in conhost, there is a hard limit of 9000 lines and each line you print goes 0 1 2 3 4 5 whatever and so on windows terminal is different, line 0 is always the top line that is visible but that means that if i call setcursorposition(0, 15) for example on conhost it will go to the 15th line that was printed which might scroll up hundreds of lines but on windows terminal it will go to the 15th line relative from the first visible line on the screen i want to revert the behaviour back to the original one, or find a way to go to an exact position instead of a relative one
taner.
taner.2w ago
Normally SetCursorPosition(x, y) should work
polemic
polemicOP2w ago
works on conhost, doesnt work on windows terminal / has the unwanted behaviour of moving relative instead of absolute
Sehra
Sehra2w ago
could try sending CHA+VPA sequence
Sehra
Sehra2w ago
GitHub
Console-Docs/docs/console-virtual-terminal-sequences.md at main · M...
Windows Console Docs Repo. Contribute to MicrosoftDocs/Console-Docs development by creating an account on GitHub.
polemic
polemicOP2w ago
Doesn't work, but I think I'm doing it wrong because it also fails on conhost. This is the original code
Console.SetCursorPosition(0, LineNumber);
Console.SetCursorPosition(0, LineNumber);
And this is what I tried changing it to
Console.Write("\x1b[" + LineNumber.ToString() + "d");
Console.Write("\x1b[" + LineNumber.ToString() + "d");
Sehra
Sehra2w ago
yeah, no idea if you can make terminal scroll to behave like conhost
polemic
polemicOP2w ago
Console.SetBufferSize fixes it but it breaks again if the window is resized but it also has the side affect of malforming data when the setcursorposition is called so not a good solution

Did you find this page helpful?