mαττjαĸøb
mαττjαĸøb

Reputation: 335

Python read console input using select on the same line

I need to use a non blocking way to read from the console. I successfully managed to do this using select

ready = select.select(read_list, [], [], timeout)[0]
read_list = [sys.stdin]
timeout = 0.1 # seconds

My problem now is that I need to provide a text line (eg UI) before the input and would like the input cursor to be on the same line. Before when I was not using select I could achieve this by doing:

buff = raw_input('                     ENTER CODE: ------\b\b\b\b\b\b')

In this way the cursor would have been just after the comma (eg. on the first '-')

Now that I need to use stdin, the cursor always goes at the beginning of a new line. Even if I do:

print('                     ENTER CODE: ------\b\b\b\b\b\b\r')
while read_list:
        ready = select.select(read_list, [], [], timeout)[0]
        if not ready:
            idle_work()
        else:
            for file in ready:
                line = file.readline()
                if not line: # EOF, remove file from input list
                    read_list.remove(file)
                elif line.rstrip(): # optional: skipping empty lines
                    #treat_input(line)
                    buff =line.upper()
                    ETC...

Any ideas ?

Upvotes: 0

Views: 644

Answers (1)

mαττjαĸøb
mαττjαĸøb

Reputation: 335

I resolved this appending a ' at the end of the print statement and then flushing the std:

print('                     ENTER CODE: ------\b\b\b\b\b\b'),
sys.stdout.flush()

Upvotes: 2

Related Questions