Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async Console Output

I have a problem with my application win32 console.

The console is used to give commands to my application. However, at the same time it is used to output log messages which mostly comes from asynchronous threads. This becomes a problem when the user tries to write some input and simultaneously an async log message is printed, thus thrashing the display of the users input.

I would like to have some advice in regards to how to handle such a situtation?

Is it possible for example to dedicate the last line in the console to input, similarly to how it looks in the in-game consoles for some games?

like image 320
ronag Avatar asked Dec 22 '22 18:12

ronag


2 Answers

You can use SetConsoleMode to disable input echo and line editing mode. You can then echo back input whenever your program is ready to do so. Note that this means you will need to implement things like backspace manually. And don't forget to reset the mode back when you're done with the console!

like image 77
bdonlan Avatar answered Jan 07 '23 12:01

bdonlan


This is possible using the Console API, but it involves quite a bit of work and all the threads that use the console will have to cooperate by calling your output method rather than directly calling the Console API functions or the runtime library output functions.

The basic idea is to have your common output function write to the console screen buffer, and scroll the buffer in code rather than letting the text flow onto the last line and scroll automatically. As I recall, you'll have to parse the output for newlines and other control characters, and handle them correctly.

You might be able to get away with using "cooked" console input on the last line, although in doing so you risk problems if the user enters more text than will fit on a single line. Also, the user hitting Enter at the end of the line might cause it to scroll up. Probably best in this situation to use raw console input.

You'll want to become very familiar with Windows consoles.

like image 35
Jim Mischel Avatar answered Jan 07 '23 12:01

Jim Mischel