Skip to content

Commit

Permalink
Merge pull request #421 from oising/move-readconsoleinputstream-demo
Browse files Browse the repository at this point in the history
moved readconsoleinputstream to samples folder
  • Loading branch information
miniksa authored Apr 29, 2019
2 parents 2e0fd58 + 987805e commit 2d1055d
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ You'll also find assorted Console tools, samples, including the following:
* [ColorTool](https:/Microsoft/Console/tree/master/tools/ColorTool) - A tool for changing the color scheme of the Windows console.
* [EchoCon](https:/Microsoft/console/tree/master/samples/ConPTY/EchoCon) - A C++ sample application that illustrates how to use the new Win32 Pseudo Console (ConPTY).
* [MiniTerm](https:/Microsoft/console/tree/master/samples/ConPTY/MiniTerm) - A C# sample terminal that illustrates how to use ConPTY.
* [ReadConsoleInputStream](https:/Microsoft/console/tree/master/samples/ReadConsoleInputStream) - A C# sample console application that shows how to use the console APIs to stream stdin while asynchronously watching for console events (buffer size, viewport size, mouse input etc.)

### Tool Build Status

Expand Down
File renamed without changes.
16 changes: 16 additions & 0 deletions samples/ReadConsoleInputStream/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# ReadConsoleInputStream Demo

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 resize events. This is partcularly useful when working with VT100 streams and ConPTY.

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 and David Hall's excellent P/Invoke wrappers to make this demo cleaner to read; both are pulled from NuGet.

**Note:**

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.

NuGet packages used (GitHub links):

* [Pipelines.Sockets.Unofficial](https:/mgravell/Pipelines.Sockets.Unofficial)
* [Vanara P/Invoke](https:/dahall/Vanara)

0 comments on commit 2d1055d

Please sign in to comment.