cmd - Print text before input() prompt in python -


in python, possible request user input input() in console while simultaneously printing out text in line before prompt? should this:

text 1 text 2 text 3 please enter something: abc 

whenever new text printed, should printed after previous text , before input() prompt. also, should not interrupt user entering text.

therefore, after printing "text 4" console should this:

text 1 text 2 text 3 text 4 please enter something: abc 

is possible in python without using external libraries?

i have tried using \r, \b , similar codes threading. know need have 1 thread printing out text while have 1 requesting user input.

here's approach using ansi/vt100 terminal control escape sequences.

we tell terminal scroll upper region of terminal, output data printed, lower region, input prompt printed, stays fixed in place. when exit program (using ctrl c) need restore default scrolling settings.

this program first clears screen , sits in loop prompting user number n , prints numbers in range(n), 1 per line, small time delay make easier see what's happening. output each range follows on previous range. if user enters non-integer prompt re-printed. readline module imported editing , history available @ input prompt; module may not available on platforms.

first, python 2 version.

''' print text in scrolling region of terminal above fixed line input      written pm 2ring 2016.05.29      python 2 version '''  __future__ import print_function time import sleep import readline  # ansi/vt100 terminal control escape sequences csi = '\x1b[' clear = csi + '2j' clear_line = csi + '2k' save_cursor = csi + 's' unsave_cursor = csi + 'u'  def emit(*args):     print(*args, sep='', end='')  def set_scroll(n):     return csi + '0;%dr' % n  # height of scrolling region height = 40  goto_input = csi + '%d;0h' % (height + 1)  emit(clear, set_scroll(height))  try:     while true:         #get input         emit(save_cursor, goto_input, clear_line)         try:             n = int(raw_input('number: '))         except valueerror:             continue         finally:             emit(unsave_cursor)          #display output         in range(n):             print(i)             sleep(0.1)  except keyboardinterrupt:     #disable scrolling, leave cursor below input row     emit(set_scroll(0), goto_input, '\n') 

and here's version runs on python 2 , python 3. when run on python 3 script calls shutil.get_terminal_size() set height of scrolling region. is possible terminal size in python 2, code rather messy, opted fixed height.

i should mention both versions of script don't cope if terminal size changed while running; handling left exercise reader. :)

''' print text in scrolling region of terminal above fixed line input      written pm 2ring 2016.05.29      python 2 / 3 version '''  __future__ import print_function import sys import readline time import sleep  if sys.version_info > (3,):     # (current) number of lines in terminal     import shutil     height = shutil.get_terminal_size().lines - 1      stdout_write_bytes = sys.stdout.buffer.write else:     height = 40     input = raw_input     stdout_write_bytes = sys.stdout.write   # ansi/vt100 terminal control escape sequences csi = b'\x1b[' clear = csi + b'2j' clear_line = csi + b'2k' save_cursor = csi + b's' unsave_cursor = csi + b'u'  goto_input = csi + b'%d;0h' % (height + 1)  def emit(*args):     stdout_write_bytes(b''.join(args))  def set_scroll(n):     return csi + b'0;%dr' % n  emit(clear, set_scroll(height))  try:     while true:         #get input         emit(save_cursor, goto_input, clear_line)         try:             n = int(input('number: '))         except valueerror:             continue         finally:             emit(unsave_cursor)          #display output         in range(n):             print(i)             sleep(0.1)  except keyboardinterrupt:     #disable scrolling, leave cursor below input row     emit(set_scroll(0), goto_input, b'\n') 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -