So I am writing a project where I run a program that constantly receives/sends messages to other computers running the same program.
The receiver/sender of data is running on a thread and prints to stdout. I get stuff like this:
[INFO] User 'blah' wants to send message to you.
[INFO] some other info
[MSG REC] Message 'hello' received from blah.
Now the issue is that sometimes I wish to input commands into the terminal, the problem is when I try to enter a command and a new info message or MSG REC
is printed to stdout. I have commands such as quit
and status
etc.
>> indicates the input line.
Something like this may happen:
[INFO] User 'blah' wants to send message to you.
[INFO] some other info
[MSG REC] Message 'hello' received from blah.
>> stat[MSG REC] Message 'sup' received from Bob.
us
Then I would press enter and the command status
gets executed but looks so poor in the terminal. A message appears every 2-4 seconds so this is an issue. Is there a good way to solve this? I tried using ANSI cursor commands to try and insert a new line before the last line so the last line would always remain as the input line and I could type in "stat", wait for a while and finish it with "us" without any issues.
I also saw people recommend curses
but attempting to integrate that with my program completely messed up the formatting of my output among other things (and I think its overkill perhaps).
So is there an easy way to make the thread insert new MSG REC
lines 1 line above the last line so the last line would always remain as the input line with >> and whatever else I have typed in.
Using Python2.7 on Linux.
EDIT: Change that made James Mills answer work: I had to use this whenever my thread was printing a new line.
myY, myX = stdscr.getyx();
str = "blah blah"; #my message I want to print
stdscr.addstr(len(lines), 0, str)
lines.append(str)
stdscr.move(myY, myX) #move cursor back to proper position