Следующий фрагмент кода, использующий curses
, будет обрабатывать стандартные видимые символы ascii, удаляющие символы и новую строку (для отправки).
from curses import wrapper
def prompt(stdscr, prompt_template="--> [ {} ]"):
user_inp = ""
display_str = prompt_template.format(user_inp)
stdscr.addstr(0, 0, display_str)
while True:
inp_ch = stdscr.getch()
# standard ASCII characters
if 32 <= inp_ch <= 126:
user_inp += chr(inp_ch)
elif inp_ch in (8, 127, 263): # BS, DEL, Key_Backspace
user_inp = user_inp[:-1]
elif inp_ch == 10: # is newline, enter pressed
break
else: # all other characters are ignored
continue
display_str = prompt_template.format(user_inp)
stdscr.erase()
stdscr.addstr(0, 0, display_str)
stdscr.refresh()
return user_inp
print(wrapper(prompt))