Reputation: 91
To build a chat, I have to send and receive messages.
Currently, I am sending and receiving messages from the command line.
When I receive a message while I was typing one, the received message is printed and I can't see what was typed before. I use the read()
command to catch the user input.
Here is an example:
Let's suppose I want to send the message Hello World
and that I receive a message after typing Hello Wor
.
This is what happens :
$ Enter your message : Hello Wor
$ Somebody: This is a message
$ Enter your message :
But Hello Wor
is still on the command line: if I press the Delete Key, I will see Hello Wo
.
This is what I want :
$ Enter your message : Hello Wor
$ Somebody: This is a message
$ Enter your message : Hello Wor
I would like to save what was typed before printing the received message and print it.
Upvotes: 1
Views: 185
Reputation: 155035
Without using a terminal control API like curses
or ncurses
, you will need to change your program to work like-so:
getchar
while waiting for the user to enter the next character of their message or [Enter]
.
stdio
at the same time which is inadvisable, while you could use thread synchronization primitives to prevent this bug you're better-off using an asynchronous IO API instead.await
keyword does in C# and JavaScript).ncurses
which handles this for you.^[[2K
), write the received message, then re-write the user's incomplete entered input (if any) on a new line.Upvotes: 1