From 5617dcad9afac8fa8ae1f6e8c08954e15bff79e4 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Wed, 30 Oct 2019 09:26:20 +0000 Subject: [PATCH] term.py: add 'sleep' option --- mmgen/term.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/mmgen/term.py b/mmgen/term.py index c51a7c2e..8c50bde3 100755 --- a/mmgen/term.py +++ b/mmgen/term.py @@ -56,12 +56,14 @@ def _kb_hold_protect_unix(): # Use os.read(), not file.read(), to get a variable number of bytes without blocking. # Request 5 bytes to cover escape sequences generated by F1, F2, .. Fn keys (5 bytes) # as well as UTF8 chars (4 bytes max). -def _get_keypress_unix(prompt='',immed_chars='',prehold_protect=True,num_chars=5): - msg_r(prompt) +def _get_keypress_unix(prompt='',immed_chars='',prehold_protect=True,num_chars=5,sleep=None): timeout = float(0.3) fd = sys.stdin.fileno() old = termios.tcgetattr(fd) tty.setcbreak(fd) + if sleep: + time.sleep(sleep) + msg_r(prompt) immed_chars = immed_chars.encode() if g.test_suite: prehold_protect = False while True: @@ -78,16 +80,20 @@ def _get_keypress_unix(prompt='',immed_chars='',prehold_protect=True,num_chars=5 termios.tcsetattr(fd, termios.TCSADRAIN, old) return s -def _get_keypress_unix_raw(prompt='',immed_chars='',prehold_protect=None,num_chars=5): - msg_r(prompt) +def _get_keypress_unix_raw(prompt='',immed_chars='',prehold_protect=None,num_chars=5,sleep=None): fd = sys.stdin.fileno() old = termios.tcgetattr(fd) tty.setcbreak(fd) + if sleep: + time.sleep(sleep) + msg_r(prompt) ch = os.read(fd,num_chars) termios.tcsetattr(fd, termios.TCSADRAIN, old) return ch -def _get_keypress_unix_stub(prompt='',immed_chars='',prehold_protect=None,num_chars=None): +def _get_keypress_unix_stub(prompt='',immed_chars='',prehold_protect=None,num_chars=None,sleep=None): + if sleep: + time.sleep(0.1) msg_r(prompt) return sys.stdin.read(1).encode() @@ -106,7 +112,10 @@ def _kb_hold_protect_mswin(): if float(time.time() - hit_time) > timeout: return -def _get_keypress_mswin(prompt='',immed_chars='',prehold_protect=True,num_chars=None): +def _get_keypress_mswin(prompt='',immed_chars='',prehold_protect=True,num_chars=None,sleep=None): + + if sleep: + time.sleep(sleep) msg_r(prompt) timeout = float(0.5) @@ -129,13 +138,17 @@ def _get_keypress_mswin(prompt='',immed_chars='',prehold_protect=True,num_chars= if float(time.time() - hit_time) > timeout: return ch -def _get_keypress_mswin_raw(prompt='',immed_chars='',prehold_protect=None,num_chars=None): +def _get_keypress_mswin_raw(prompt='',immed_chars='',prehold_protect=None,num_chars=None,sleep=None): + if sleep: + time.sleep(sleep) msg_r(prompt) ch = msvcrt.getch() if ch == b'\x03': raise KeyboardInterrupt return ch -def _get_keypress_mswin_stub(prompt='',immed_chars='',prehold_protect=None,num_chars=None): +def _get_keypress_mswin_stub(prompt='',immed_chars='',prehold_protect=None,num_chars=None,sleep=None): + if sleep: + time.sleep(0.1) msg_r(prompt) return os.read(0,1)