3

I'm making a chat application with Python 2. No GUI, all CLI. I'm trying to think of a way to always make sure the raw_input prompt that's waiting for a command, including everything the user has typed so far, is always the last thing shown. Doesn't need to be at the bottom of the user's view, necessarily. Just needs to be below everything else that's happened so far.

Thing is, if someone else sends a message to the user, it'll look confusing and sloppy when the message pops up in the prompt and then the cursor gets moved down with no prompt, etc.

For example:

--- Started chat ---
Input: Hello. Good to meeReceived: Hi there!
t you. ('Enter')
Input: 

In this example, the user receives a message in the middle of typing "meet". The user can still send the message after typing "you", and the other user will still receive "Hello. Good to meet you." but this still obviously looks bad. This could also happen when the user is trying to type a command to the application in the input, not always a message to someone else, or when nothing has been typed at all. Ideally, the output from some other thread would print, and the input line so far would look like it was just pushed down.

I need this to work on Windows as well as other systems, so I don't think I can use curses, or libraries made with curses like urwid or npyscreen suggested here. I had the idea that the thread listening for messages could just somehow copy what's on the prompt line so far, then remove the text on the line, display a message, and then print the copied input line on a new line so the cursor would be right back where it was. Is that possible? Is there a way to accomplish this in some other windows/unix/linux friendly way?

Jorek
  • 75
  • 7
  • [Here](http://stackoverflow.com/a/37501797/4014959) is an example I wrote a year ago that works in any ANSI/VT100 compatible terminal. I _think_ it will work on Windows if you enable ANSI escape sequences, but I don't use Windows, so I don't know how to do that. – PM 2Ring Jul 24 '17 at 15:38
  • Since you're on Windows you can use `msvcrt` to silently capture user's input while fully controlling how it gets printed on the STDOUT. To do the latter in any sort of an elegant way (so no clearing screen on each change) you'll have to use `curses` which is not built-in on Windows so install [`UniCurses`](https://pypi.python.org/pypi/UniCurses) (and it's `PDCurses` dependency) first and then take it from there. – zwer Jul 24 '17 at 18:47

0 Answers0