I’m trying to implement an application similar to a chat client where messages can arrive while the user is typing his own message.
I’ll explain how I want my program to behave with an example:
Before an incoming message:
>user partial input
After an incoming message:
>the new message
>user partial input(cursor is here)
Instead what naturally happens after an incoming message is this:
>user partial input the new message
>(cursor is here)
After that the user can still use backspace to delete what he wrote before but it doesn’t show on the screen and there’s a UI mess overall.
Is there any way to achieve the desired behavior without using ncurses?
Thank you.
Edit: Sorry, I forgot to write what my environment is, it’s Cygwin. Thanks for all of the answers.
While ncurses may be the most flexible and simple way to achieve this, you can do it yourself with some work.
You have to arbitrate the IO yourself. You can use “\r” to return to the beginning of a line without ncurses or any platform-specific terminal interaction.
The overall flow would be:
into an input buffer character-wise.
The default behavior is to echo every
character. Once you receive a
newline, you would commit that line
from your buffer and print with a
newline.
There are lots of edge cases to consider here, but ncurses is not your only option. This technique works on both Windows and Linux as far as I have tested.