summaryrefslogtreecommitdiff
path: root/inputs.py
blob: 47160c32c119de0bda3cf3b2cfb0975c7c5fc934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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