Is it possible to limit the terminal not to go over a single page? #9637

Closed
opened 2026-01-31 01:59:49 +00:00 by claunia · 6 comments
Owner

Originally created by @AnFunctionArray on GitHub (Jul 15, 2020).

I have a code that uses SetConsoleCursorPosition to set a position on the screen and write to it. However sometimes it goes over the window buffer and the terminal scrolls down. Is it possible to restrict this and limit the cursor at the current window.

If it's not I would be grateful if you at least point me in the right direction. I've been tinkering with the source code - limiting cursor positions and scrolls all day (both in my application and lately mostly at the terminal source).

Originally created by @AnFunctionArray on GitHub (Jul 15, 2020). I have a code that uses `SetConsoleCursorPosition` to set a position on the screen and write to it. However sometimes it goes over the window buffer and the terminal scrolls down. Is it possible to restrict this and limit the cursor at the current window. If it's not I would be grateful if you at least point me in the right direction. I've been tinkering with the source code - limiting cursor positions and scrolls all day (both in my application and lately mostly at the terminal source).
Author
Owner

@zadjii-msft commented on GitHub (Jul 16, 2020):

Well, I'd probably start writing code to clamp all your calls to SetCursorPosition within the bounds of the srWindow returned in the CONSOLE_SCREEN_BUFFER_INFO from GetConsoleScreenBufferInfo.

Another thing you could do, which is common for applications which are "painting the entire viewport" (for things like vim, emacs, top, tmux, etc), is use the "alternate screen buffer". This is a special buffer which is exactly the size of the window. You can make edits to the contents of this buffer, and when you leave the buffer, the changes will not be persisted in the "main" buffer, so you don't just have the contents of vim leftover in your history.

You can see a sample of using the alt buffer in a console app here. You'll need to enable ENABLE_VIRTUAL_TERMINAL_PROCESSING, and emit "\x1b[?1049h" to enter the alt buffer, and "\x1b[?1049l" to leave.

You should note that the alt buffer doesn't work exactly right in the Windows Terminal quite yet (see #381, #3492 and others), but it'll work just fine in the vintage console window (conhost.exe)

@zadjii-msft commented on GitHub (Jul 16, 2020): Well, I'd probably start writing code to clamp all your calls to `SetCursorPosition` within the bounds of the `srWindow` returned in the `CONSOLE_SCREEN_BUFFER_INFO` from `GetConsoleScreenBufferInfo`. Another thing you could do, which is common for applications which are "painting the entire viewport" (for things like `vim`, `emacs`, `top`, `tmux`, etc), is use the "alternate screen buffer". This is a special buffer which is exactly the size of the window. You can make edits to the contents of this buffer, and when you leave the buffer, the changes will not be persisted in the "main" buffer, so you don't just have the contents of `vim` leftover in your history. You can see a sample of using the alt buffer in a console app [here](https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#example-of-select-anniversary-update-features). You'll need to enable `ENABLE_VIRTUAL_TERMINAL_PROCESSING`, and emit `"\x1b[?1049h"` to enter the alt buffer, and `"\x1b[?1049l"` to leave. You should note that the alt buffer doesn't work exactly right in the Windows Terminal quite yet (see #381, #3492 and others), but it'll work just fine in the vintage console window (`conhost.exe`)
Author
Owner

@AnFunctionArray commented on GitHub (Jul 16, 2020):

So it looks like this alternative buffer is exactly what I need - but I also need it in Windows Terminal because I depend on the Unicode support.

I guess for now the only thing I can do is wait.

@AnFunctionArray commented on GitHub (Jul 16, 2020): So it looks like this alternative buffer is exactly what I need - but I also need it in Windows Terminal because I depend on the Unicode support. I guess for now the only thing I can do is wait.
Author
Owner

@zadjii-msft commented on GitHub (Jul 16, 2020):

You can definitely still use the alt buffer currently in the Windows Terminal, there'll just be slight bugs in our implementation. Heck, you can use vim, emacs, htop, etc currently in the Terminal, and they're all pretty fundamentally relying on it.

Currently in the Windows Terminal, your cursor positioning will still be clamped to the size of the viewport, it's just, we'll leave the output in the main buffer for now.

I'd say go ahead and start prototyping with that now and keep an eye on the issues I've linked previously ☺️

@zadjii-msft commented on GitHub (Jul 16, 2020): You can definitely still use the alt buffer currently in the Windows Terminal, there'll just be _slight_ bugs in our implementation. Heck, you can use `vim`, `emacs`, `htop`, etc currently in the Terminal, and they're all pretty fundamentally relying on it. Currently in the Windows Terminal, your cursor positioning will still be clamped to the size of the viewport, it's just, we'll leave the output in the main buffer for now. I'd say go ahead and start prototyping with that _now_ and keep an eye on the issues I've linked previously ☺️
Author
Owner

@AnFunctionArray commented on GitHub (Jul 17, 2020):

@zadjii-msft The alternative buffer isn't locked to the viewpoint - is this an issue? I was able to restrict the viewpoint by commenting some code but I think it would be better is we can write this on stone. Alternatively we could implement a viewpointLock setting to enable. If you think any of those things can be useful/accepted - I can create some code plus I think I can write some tests too.

@AnFunctionArray commented on GitHub (Jul 17, 2020): @zadjii-msft The alternative buffer isn't locked to the viewpoint - is this an issue? I was able to restrict the viewpoint by commenting some code but I think it would be better is we can write this on stone. Alternatively we could implement a `viewpointLock` setting to enable. If you think any of those things can be useful/accepted - I can create some code plus I think I can write some tests too.
Author
Owner

@zadjii-msft commented on GitHub (Aug 19, 2020):

Sorry for the incredibly delayed response. You're right that the alt buffer in the Terminal currently isn't locked to the viewport. That's something that'll need #381 to actually be finished to fix. I'd rather not implement another workaround - let's just focus on fixing the real root cause 😄

@zadjii-msft commented on GitHub (Aug 19, 2020): Sorry for the incredibly delayed response. You're right that the alt buffer _in the Terminal_ currently isn't locked to the viewport. That's something that'll need #381 to actually be finished to fix. I'd rather not implement another workaround - let's just focus on fixing the real root cause 😄
Author
Owner

@ghost commented on GitHub (Aug 23, 2020):

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.

@ghost commented on GitHub (Aug 23, 2020): 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**.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#9637