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

ReadConsoleInputStream demo #414

Merged
merged 5 commits into from
Apr 25, 2019

Conversation

oising
Copy link
Collaborator

@oising oising commented Apr 21, 2019

This is a demo that shows how we can have a stream-oriented view of characters from the console
while also listening to console events like mouse, menu, focus, buffer/viewport(1) resize events.

This has always been tricky to do because ReadConsoleW/A doesn't allow retrieving events.
Only ReadConsoleInputW/A returns events, but isn't stream-oriented. Using both doesn't work because
ReadConsoleW/A flushes the input queue, meaning calls to ReadConsoleInputW/A will wait forever.
I do this by deriving a new Stream class which wraps ReadConsoleInputW and accepts a provider/consumer implementation of BlockingCollection<Kernel32.INPUT_RECORD>. This allows asynchronous monitoring of console events while simultaneously streaming the character input. I also use Mark Gravell's great System.IO.Pipelines utility classes (2) and David Hall's excellent P/Invoke wrappers (3) to make this demo cleaner to read; both are pulled from NuGet.

(1) in versions of windows 10 prior to 1809, the buffer resize event only fires for enlarging
the viewport, as this would cause the buffer to be enlarged too. Now it fires even when
shrinking the viewport, which won't change the buffer size.

(2) https://github.com/mgravell/Pipelines.Sockets.Unofficial
https://www.nuget.org/packages/Pipelines.Sockets.Unofficial

(3) https://github.com/dahall/Vanara
https://www.nuget.org/packages/Vanara.Pinvoke.Kernel32

Oisin Grehan - 2019/4/21
https://twitter.com/oising
https://github.com/oising

@oising
Copy link
Collaborator Author

oising commented Apr 21, 2019

The point of this is to make stream oriented work with VT sequences easier to do. This is the basis of my ongoing multiplexer project.

@miniksa
Copy link
Member

miniksa commented Apr 24, 2019

I'm good with this, but I'd like at least one other person to look (we do a 2-other-eyes policy for our code, generally). @DHowett-MSFT, @adiviness, @zadjii-msft, can one of you three give it a look?

Copy link
Member

@miniksa miniksa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@miniksa miniksa added the Issue-Samples A request for an example on how we do something. Probably a good docs candidate. label Apr 24, 2019
@miniksa miniksa merged commit 2e0fd58 into microsoft:master Apr 25, 2019
@oising oising deleted the readconsoleinputstream-demo branch April 25, 2019 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Samples A request for an example on how we do something. Probably a good docs candidate.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants