Browse Source

term.py: add 'sleep' option

The MMGen Project 5 years ago
parent
commit
5617dcad9a
1 changed files with 21 additions and 8 deletions
  1. 21 8
      mmgen/term.py

+ 21 - 8
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)