Christopher Sardegna
Christopher Sardegna

Reputation: 98

Is it possible to make python input() stick to bottom of console window while other processes print output?

Given a background process that is constantly printing to stddout, is there a way to make the call to input() "stick" to the bottom of the console?

import time
import multiprocessing


def print_in_background():
    x = 0
    while True:
        print(f'Background print {x}')
        x += 1
        time.sleep(1)

def get_input():
    return input('> ')

background_proc = multiprocessing.Process(target=print_in_background)
background_proc.daemon = True
background_proc.start()
while True:
    v = get_input()
    print(v)
background_proc.join()

This works in that you can have the background thread do stuff while the main thread gets input, but the output looks like this, where the input() line gets pushed up by the background process's output:

> Background print 0
Background print 1
Background print 2
I am Background print 3
typingBackground print 4

I am typing
> Background print 5
Background print 6

Theoretically, output like this would be preferable:

Background print 0
Background print 1
Background print 2
Background print 3
Background print 4
I am typing          # From when user hit enter key
Background print 5
Background print 6
> typing in current prompt

If possible, making the input prompt stay on the bottom line of the console would be optimal.

Upvotes: 1

Views: 1176

Answers (1)

Christopher Sardegna
Christopher Sardegna

Reputation: 98

I used the Curses library in Python stdlib to create two windows: one for output and one that had a textbox that the user can type into.

Upvotes: 1

Related Questions