Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: Call update_window_size when the console is resized #32

Open
ilai-deutel opened this issue Mar 30, 2020 · 5 comments
Open

Windows: Call update_window_size when the console is resized #32

ilai-deutel opened this issue Mar 30, 2020 · 5 comments
Labels
enhancement New feature or request windows

Comments

@ilai-deutel
Copy link
Owner

On UNIX platforms, we call update_window_size when a SIGWINCH signal is received.
Similarly, we should update the window size on Windows when the console is resized.

@ilai-deutel ilai-deutel added enhancement New feature or request windows labels Mar 30, 2020
@nico-abram
Copy link

nico-abram commented Oct 25, 2020

I'm no expert on windows console APIs, but after a bit of googling this is what I found:

According to https://docs.microsoft.com/en-us/windows/console/console-input-buffer#buffer-resizing-events

Buffer-resizing events are placed in the input buffer if the console's input mode is set to ENABLE_WINDOW_INPUT (that is, the default mode is disabled).

If the input event is a buffer-resizing event, the Event member of INPUT_RECORD is a WINDOW_BUFFER_SIZE_RECORD structure containing the new size of the console screen buffer, expressed in character-cell columns and rows.

If the user reduces the size of the console screen buffer, any data in the discarded portion of the buffer is lost.

Changes to the console screen buffer size as a result of application calls to the SetConsoleScreenBufferSize function are not generated as buffer-resizing events.

When we look at that mode in https://docs.microsoft.com/en-us/windows/console/setconsolemode it says:

Information about these events can be read from the input buffer by applications using the ReadConsoleInput function, but not by those using ReadFile or ReadConsole.

And a cursory glance at std seems to indicate it is using ReadConsoleW (https://github.com/rust-lang/rust/blob/ebc03f7c80a7ab8cdf95c0ddc31b57e065906a13/library/std/src/sys/windows/stdio.rs#L220)

By briefly looking at kibi, it seems to be reading stdin using std (https://github.com/ilai-deutel/kibi/blob/master/src/editor.rs#L216) so we might have to replicate the std behaviour here using ReadConsoleInput instead for windows which involves utf16->utf8, and it is also "fixing up surrogates"

I have not really actually tested any of this much yet, if/when I do I will comment here

EDIT:

There is an example of reading console input buffer events here

Once the ReadConsoleInput vs ReadConsole issue is solved it seems we would just need to check for https://docs.rs/winapi/0.3.9/winapi/um/wincontypes/constant.WINDOW_BUFFER_SIZE_EVENT.html

Maybe we could keep the stdin reading code the same and instead just use PeekConsoleInput (https://docs.microsoft.com/en-us/windows/console/peekconsoleinput) before reading stdin just to check for resize events, but I think we'd need to use a buffer size big enough to somehow ensure std does not consume more events than we have picked

@nico-abram
Copy link

nico-abram commented Oct 25, 2020

I've got a proof of concept of the peeking approach here: nico-abram@434b355 (loc change here is 997 => 1007)

Would have to dig around std to make sure a buffer of 1024 events is enough to ensure we never peek less events than std reads, but the bigger issue is it seems setting the mode to ENABLE_WINDOW_INPUT broke the way kibi was detecting ctrl

@nico-abram
Copy link

nico-abram commented Oct 25, 2020

Actually, I accidentally set the mode to ECHO above which was why it seemed to work, my apologies

Naively peeking does't work properly since the next() call to the stdin bytes iterator seems to block, so I'm peeking an empty buffer and then missing all the events until a key is in the input buffer. Perhaps it could block before peeking until a key is found to try to ensure we don't miss any events

@sergeevabc
Copy link

Windows 7, kibi 0.2.2

$ kibi
Error: Io(Os { code: 87, kind: InvalidInput, message: "The parameter is incorrect." })

$ kibi --help
Error: Io(Os { code: 87, kind: InvalidInput, message: "The parameter is incorrect." })

$ kibi hello.txt
Error: Io(Os { code: 87, kind: InvalidInput, message: "The parameter is incorrect." })

@ilai-deutel
Copy link
Owner Author

@sergeevabc Unfortunately kibi is not compatible with Windows 7, only Windows 10 version 1703 (Creators Update, April 2017) and above are supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request windows
Projects
None yet
Development

No branches or pull requests

3 participants