#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
# %% Getch
It's not easily possible in Python to read a character
without having to type the return key as well.
On the other hand this is very easy on the Bash shell.
The Bash command "read -n 1 waits for a key (any key) to be typed.
If you import os, it's easy to write a script providing getch()
by using os.system() and the Bash shell.
getch() waits just for one character to be typed without a return:

You can import this file if you ever need a getch!

You could alternatively use ncurses for this.
"""
import os


def getch() -> str:
    try:
        import sys
        import tty
        import termios
    except ImportError:
        raise ImportError("You're on Windows, aren't you?")
    stdin_fd = sys.stdin.fileno()  # We know this is a tty
    old_cfg = termios.tcgetattr(stdin_fd)  # save off the current config
    try:
        tty.setraw(stdin_fd)
        char = sys.stdin.read(1)
        if ord(char) == 3:
            termios.tcsetattr(stdin_fd, termios.TCSADRAIN, old_cfg)
            print("Ctrl-C quitting")
            sys.exit(0)
        elif ord(char) == 27:
            char += sys.stdin.read(2)
        return char
    finally:
        # Reset the terminal once we receive the Ctrl-C
        termios.tcsetattr(stdin_fd, termios.TCSADRAIN, old_cfg)


if __name__ == "__main__":
    print("This will take your input without needing to hit enter!")
    while True:
        userin = getch()
        if userin == "s":
            print("You hit 's' so we stop now")
            break
        else:
            print("Outputting your character 5 times")
            print(userin * 5)