line_input(): fix inserted text handling, add hold_protect arg
This commit is contained in:
parent
7d2531b7b3
commit
3062de696c
2 changed files with 19 additions and 17 deletions
30
mmgen/ui.py
30
mmgen/ui.py
|
|
@ -37,7 +37,7 @@ def get_data_from_user(desc='data'): # user input MUST be UTF-8
|
|||
dmsg(f'User input: [{data}]')
|
||||
return data
|
||||
|
||||
def line_input(prompt,echo=True,insert_txt=''):
|
||||
def line_input(prompt,echo=True,insert_txt='',hold_protect=True):
|
||||
"""
|
||||
multi-line prompts OK
|
||||
one-line prompts must begin at beginning of line
|
||||
|
|
@ -45,34 +45,33 @@ def line_input(prompt,echo=True,insert_txt=''):
|
|||
"""
|
||||
assert prompt,'calling line_input() with an empty prompt forbidden'
|
||||
|
||||
def init_readline():
|
||||
def get_readline():
|
||||
try:
|
||||
import readline
|
||||
return readline
|
||||
except ImportError:
|
||||
return False
|
||||
else:
|
||||
if insert_txt:
|
||||
readline.set_startup_hook(lambda: readline.insert_text(insert_txt))
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
if not sys.stdout.isatty():
|
||||
msg_r(prompt)
|
||||
prompt = ''
|
||||
|
||||
from .term import kb_hold_protect
|
||||
kb_hold_protect()
|
||||
if hold_protect:
|
||||
from .term import kb_hold_protect
|
||||
kb_hold_protect()
|
||||
|
||||
readline = None
|
||||
|
||||
if g.test_suite_popen_spawn:
|
||||
msg(prompt)
|
||||
sys.stderr.flush()
|
||||
reply = os.read(0,4096).decode().rstrip('\n') # strip NL to mimic behavior of input()
|
||||
elif echo or not sys.stdin.isatty():
|
||||
clear_buffer = init_readline() if sys.stdin.isatty() else False
|
||||
readline = sys.stdin.isatty() and get_readline()
|
||||
if readline and insert_txt:
|
||||
readline.set_startup_hook(lambda: readline.insert_text(insert_txt))
|
||||
reply = input(prompt)
|
||||
if clear_buffer:
|
||||
import readline
|
||||
if readline and insert_txt:
|
||||
readline.set_startup_hook(lambda: readline.insert_text(''))
|
||||
else:
|
||||
from getpass import getpass
|
||||
|
|
@ -84,9 +83,10 @@ def line_input(prompt,echo=True,insert_txt=''):
|
|||
else:
|
||||
reply = getpass(prompt)
|
||||
|
||||
kb_hold_protect()
|
||||
if hold_protect:
|
||||
kb_hold_protect()
|
||||
|
||||
return reply.strip()
|
||||
return (insert_txt if insert_txt and not readline else '') + reply.strip()
|
||||
|
||||
def keypress_confirm(prompt,default_yes=False,verbose=False,no_nl=False,complete_prompt=False):
|
||||
|
||||
|
|
|
|||
|
|
@ -1276,12 +1276,14 @@ class TestSuiteEthdev(TestSuiteBase,TestSuiteShared):
|
|||
return t
|
||||
|
||||
def edit_label1(self):
|
||||
return self.edit_label(out_num=del_addrs[0],label_text=tw_label_zh)
|
||||
return self.edit_label(out_num=del_addrs[0],label_text=tw_label_zh[:3])
|
||||
def edit_label2(self):
|
||||
return self.edit_label(out_num=del_addrs[0],label_text=tw_label_zh[:-1],changed=True)
|
||||
return self.edit_label(out_num=del_addrs[0],label_text=tw_label_zh[3:],changed=True)
|
||||
def edit_label3(self):
|
||||
return self.edit_label(out_num=del_addrs[1],label_text=tw_label_lat_cyr_gr)
|
||||
def edit_label4(self):
|
||||
if not opt.pexpect_spawn:
|
||||
return 'skip'
|
||||
return self.edit_label(out_num=del_addrs[0],label_text=self.erase_input)
|
||||
|
||||
def token_edit_label1(self):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue