summaryrefslogtreecommitdiff
path: root/inputs.py
diff options
context:
space:
mode:
Diffstat (limited to 'inputs.py')
-rw-r--r--inputs.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/inputs.py b/inputs.py
new file mode 100644
index 0000000..47160c3
--- /dev/null
+++ b/inputs.py
@@ -0,0 +1,40 @@
+import sys, tty, termios
+
+def getch():
+ import sys, tty, termios
+ old_settings = termios.tcgetattr(0)
+ new_settings = old_settings[:]
+ new_settings[3] &= ~termios.ICANON
+ try:
+ termios.tcsetattr(0, termios.TCSANOW, new_settings)
+ ch = sys.stdin.read(1)
+# except BlockingIOError:
+# pass
+ finally:
+ termios.tcsetattr(0, termios.TCSANOW, old_settings)
+ return ch
+
+def get_valid_char(char_list):
+ """returns input if it matches a list of valid chars, else retruns False"""
+ valid = False
+
+ in_char = getch()
+ for char in char_list:
+ if in_char == char:
+ valid = True
+ break
+
+ if not valid:
+ return False
+ return in_char
+
+def get_pos_int():
+ """returns input as int; returns False if not a positive integer"""
+ try:
+ in_int = int(input())
+ except:
+ return False
+ else:
+ if in_int < 1:
+ return False
+ return in_int