1

I'm currently working on a chat application with Python, but am currently facing an annoying problem. When the user is inputting a message to the console, while an another user is sending messages, the input message of the first user gets split into multiple lines. I attached a picture of the problem.

As you can see, the input is being intercepted with the output of the console.

Any idea how to prevent this?

For the input, I was holding down Z while a friend was sending messages to the channel. As you can see, It's being interrupted by the messages that are appearing.

Edit:

import colorama
from termcolor import cprint
colorama.init()

green = '\33[92m'
reset    = '\033[0m'

def receive_messages():
    while True:
        data = s.recv(1024).decode()
        #s is a previously defined socket
        ind = data.index(">") + 1
        usr = data[:ind]
        raw = data[ind:]
        cprint(green + usr + green + reset + raw + reset)

threading.Thread(target = receive_messages).start()

username = #opens the file where the username is stored: <username>

while True:
    txt = input()
    s.send((username + " " + txt).encode())

What I want is the input text to be locked in one line and not split into many.

LaurentiuS
  • 304
  • 3
  • 10
  • 1
    [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – DYZ Aug 23 '18 at 20:42
  • 1
    You don't even have any code here – Ryan Schaefer Aug 23 '18 at 20:43
  • @Yunnosch It's pretty long, I'll try to edit the question and add a few lines and I'll briefly explain what's going on. I have a thread that receives messages for the client and an input() that is being sent to the server after the user enters it. I've figured that it probably has something to do with the thread. – LaurentiuS Aug 23 '18 at 20:45
  • Before you edit your question, please read the document that I mentioned earlier. – DYZ Aug 23 '18 at 20:47
  • Yes, when you have asynchronous processes writing to the same device without a locking mechanism, their contributions are handling in the order received. What output did you expect / want? Include that in your update. – Prune Aug 23 '18 at 20:51
  • @DYZ Sorry for the misunderstanding. I've updated the question – LaurentiuS Aug 23 '18 at 20:55
  • 1
    Possible duplicate of [Keep console input line below output](https://stackoverflow.com/questions/25342420/keep-console-input-line-below-output) – Ryan Schaefer Aug 23 '18 at 20:57

1 Answers1

1

In order to deal with this on your own you'll need to keep track of the user input character by character. When a message is recieved, before it's printed, erase the user input, print the incoming message, and then re-print the partially input message. This will make it so that the message is un-interrupted.

A much better idea for simplicity would be to use something like py-term or npyscreen (though I'm not sure these libraries in particular have all the features you need) in order to manage your output to the terminal which should make it so you can more easily manage where messages are printed and where the user message input is displayed in the terminal window.

Don
  • 3,987
  • 15
  • 32