microsoft / microsoft/terminal

Automatic scrolling of window when setting cursor position

Open
#14,774 12 comments 2 reactions 0 assignees View on GitHub
Area-Output Area-Server Help Wanted Issue-Bug Priority-1 Product-Conhost
Dominant language
C++
Stars
105k
Forks
9.6k
Avg merge
3d 17h
Merged PRs (30d)
29

Description

### Windows Terminal version

1.16.230126001

### Windows build number

10.0.19044.2486

### Other Software

_No response_

### Steps to reproduce

I mentioned this first in #14759.

You can reproduce the problem with this test program:
```c
#include

int main()
{
HANDLE con = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(con, &csbi);

// use scrollback of 100 lines
COORD con_size = {
csbi.srWindow.Right - csbi.srWindow.Left + 1,
csbi.srWindow.Bottom - csbi.srWindow.Top + 1 + 100
};
SetConsoleScreenBufferSize(con, con_size);

// write 50 lines so the position is not at the top of the scrollback
for (int i = 0; i < 50; i++)
WriteConsoleA(con, "line\n", 5, NULL, NULL);

// stop at escape key
HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD ir;
DWORD didread;
while (ReadConsoleInput(in, &ir, 1, &didread))
{
if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown)
{
if (ir.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
break;

WriteConsoleA(con, &ir.Event.KeyEvent.uChar.AsciiChar, 1, NULL, NULL);
GetConsoleScreenBufferInfo(con, &csbi);
// re-set the cursor position where it already is, this is done
// so the cursor is visible at the new location after the character
// was printed
SetConsoleCursorPosition(con, csbi.dwCursorPosition);
// now the console window is scrolled so that the cursor is at the bottom
// of the window.
}
}

return 0;
}
```
There is a difference of behavior when tried with either the conhost.exe of the current Win10, or the OpenConsole.exe of Microsoft.WindowsTerminal_Win10_1.16.10261.0_8wekyb3d8bbwe.msixbundle.

Between entering some keys, scroll the window up or down with the mouse.

### Expected Behavior

Once you enter a key, and the cursor position is re-set, it will scroll the window to the cursor, but only if the cursor was not already in the visible part of the window.
Which is exactly how it works in conhost.exe.

### Actual Behavior

But with OpenConsole.exe 1.16.230126001, the window is always scrolled so that the cursor is at the bottom of the screen, even if it was already visible before.

Contributor guide

Open the contributing guide

Research direction

Start with the provided C reproduction and compare the cursor-scrolling behavior in conhost.exe and OpenConsole.exe while moving the window with the mouse. Trace the handling of SetConsoleCursorPosition and visible-window scrolling in OpenConsole, then verify that resetting a cursor already in view does not move it to the bottom while an off-screen cursor is brought into view.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
cli, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.