I'm writing some sort of terminal chat application that has a very simple interface:
[Me] Here I write my messages.
[Me] When the Enter key is pressed, the message is sent to other users.
[user1] Other users can also send me messages.
[user2] Their usernames are shown at the start of the line, between square brackets.
This application's main thread just waits until the user writes a message and presses Enter:
msg = input('[Me] ')
# Do stuff with msg...
And received messages from other users are shown in the screen with a simple print function call (that runs in other thread):
print(f'\n[{msg_author}] {msg_content}')
Thing is I'm not able to deal with the printing of messages received from other users in a pretty way. For example, after a new message is received, the state of the screen may be the next one:
[Me]
[user1] New message appeared!
Since the print function ran because of the new message, now the app is still waiting for me to write a message, but the cursor is in a new line (note the last blank line), so if I start to write a message, it will be shown like this
[Me]
[user1] New message appeared!
I'm writing here...
when the prettier way will be that the screen updates to
[user1] New message appeared!
[Me] This will be the way!
This is even a bigger problem when I have wrote some part of my new message (without pressing Enter) and a new message is received:
[Me] I have still not finished this message...
[user1] Another message appeared!
The message I was writing should be shown like this:
[user1] Another message appeared!
[Me] I have still not finished this message... And I would like to continue it down here!!!
I'm aware of the existence of CSI sequences, but I believe they don't help to solve this problem (actually, I don't even know if this is solvable using the input function to ask for messages).