0

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)
J. Schmidt
  • 419
  • 1
  • 6
  • 17
  • 1
    You need to set a fixed position for your Chat entry location. using `curses` is the easiest to build such input apps. here's a similar question with python only https://stackoverflow.com/questions/14300245/python-console-application-output-above-input-line – Abhishek Dujari Apr 12 '19 at 11:38
  • @Abhishet Dujari If you'd like I can accept your answer to this post, but I cannot do it with your comment. – J. Schmidt Sep 17 '19 at 14:47

1 Answers1

0

You need to set a fixed position for your Chat entry location. using curses is the easiest to build such input apps. here's a similar question with python only Python console application - output above input line

Abhishek Dujari
  • 2,343
  • 33
  • 43