I'm creating a simple chat on my terminal between multiple clients, using Python. The problem is, whenever I'm typing a message, it can get overwritten by an incoming message in the terminal. This is only visual however, as finishing my message and pressing enter, still sends my whole message. How to avoid having my message overwritten?
I've tried using the following output tweaking code lines, with the aim of printing any incoming messages on some created new line :
sys.stdout.write("\033[F") #back to previous line
print("\n") #create a new line between second last and last line
print(msg) #print on new line any incoming messages
or this way :
print("\r") #go back to line start
print("\n") #create a new line after last line (this also pushes any unfinished message to this new last line)
sys.stdout.write("\033[F") #back to previous line (empty now)
print(msg) #print on new line any incoming messages
but this didn't actually work as expected. I also tried to write received messages only when the input buffer (=last line?) is empty. I don't have this code anymore as it didn't work, but it ressembled this :
while sys.stdin.buffer.read():
#do nothing
print(msg)