def _getch_unix(_getall=False):
    """
    # --- current algorithm ---
    # 1. switch to char-by-char input mode
    # 2. turn off echo
    # 3. wait for at least one char to appear
    # 4. read the rest of the character buffer (_getall=True)
    # 5. return list of characters (_getall on)
    #        or a single char (_getall off)
    """
    import sys, termios
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    chars = []
    try:
        newattr = list(old_settings)
        newattr[3] &= ~termios.ICANON
        newattr[3] &= ~termios.ECHO
        newattr[6][termios.VMIN] = 1
        newattr[6][termios.VTIME] = 0
        termios.tcsetattr(fd, termios.TCSANOW, newattr)
        ch = sys.stdin.read(1)
        chars = [ch]
        if _getall:
            newattr = termios.tcgetattr(fd)
            newattr[6][termios.VMIN] = 0
            newattr[6][termios.VTIME] = 0
            termios.tcsetattr(fd, termios.TCSANOW, newattr)
            while True:
                ch = sys.stdin.read(1)
                if ch != '':
                    chars.append(ch)
                else:
                    break
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    if _getall:
        return chars
    else:
        return chars[0]
