0

In my multi-threaded (2 threads) python program, both threads writes to the console. Main thread accepts commands using input(), another thread fetches some data from server and prints some messages. I have managed to move the input prompt (first thread) to new line by printing '\r' to the console. But I can't find a way to bring back the input that was on half way with that prompt. How can I achieve the correct behavior.

This is a snippet of code from the program:

class GetServerData:
    def mainthread(self, interval):
        updates = threading.Thread(target=self.getUpdates, args={interval})
        updates.start()
        self.threadsrunning = True

        self.continuemainloop = True
        while(self.continuemainloop):
            self.promptinput()
            command = input()
            if(command == "")
                self.promptinput()
            elif(command=="quit")
                self.continueloop = False
                print("\rWaiting for all threads to terminate ...")
                while(self.threadsrunning)
                    pass
                self.continuemainloop = False
        print("Program terminated")


    def getUpdates(self, interval)
        self.continueloop  = True
        while(self.continueloop):
            #Code for getting data from server
            #and print/process the response
            #if server responds
            time.sleep(15)
            print("\rReceived Updates ...") #This prints at the current line
            #After this, prompt should be shown
            #If something was being typed, it should also be shown here
            self.promptinput()
        self.threadsrunning = False


    def promptinput(self):
        print(" >> ", end='', flush=True) #Prompt shows without moving to next line
        #TODO : Print current keyboard input which is not finished yet

What should I put after the prompt prints >>, to show unfinished input.

1 Answers1

0

It depends on your terminal.

There are ANSI escape codes to move Forward and Back on a line, if you target such terminals.

Otherwise, there are plenty of libraries to help you do that.

And similar questions on Stack Overflow : this one for example.

Lenormju
  • 4,078
  • 2
  • 8
  • 22
  • I'm OK with the cursor movement using the ANSI escape sequence '\033[F' and clearing line using '\033[K', but I can't find a way to bring back the input which was being typed to the first thread, after something printed by the second thread. First thread simply waits for the input, so the second thread should bring back the prompt and the last input characters from keyboard to the screen after the message it prints. Any way to print/access the input stream content before End Of Line or Enter key press or should I implement a method to scan and store keystrokes instead of builtin input methods. – eternalRAYs Jul 05 '21 at 09:01
  • I'm already using the ANSI escape sequence '\033[F\033[K' in my code, and used a snippet of code, to just show what I was asking for, from the large program code. – eternalRAYs Jul 05 '21 at 09:06
  • I think erasing the content the user is typing (even it you want to put it back just after) is not an elegant solution. There are libraries to handle things more like a GUI : having a part of the screen where you can put text which will auto-scroll, but keep a fixed part for input. – Lenormju Jul 05 '21 at 09:06
  • I know GUI is better to handle user interactions. But we can see tab completions in terminals printing some suggestions, but holding the input up to where we typed, before pressing Tab key. – eternalRAYs Jul 05 '21 at 09:21