utils.py
Code:
import sys
import os
from enum import Enum
from typing import Callable
def clear_terminal() -> None:
#print(chr(27) + "[2J")
os.system('clear')
input_char: Callable[[], str]
try:
import tty, termios
except ImportError:
# Probably Windows.
try:
import msvcrt
except ImportError:
# FIXME what to do on other platforms?
# Just give up here.
raise ImportError('getch not available')
else:
input_char = msvcrt.getch # type: ignore
else:
def _input_char() -> str:
"""getch() -> key character
Read a single keypress from stdin and return the resulting character.
Nothing is echoed to the console. This call will block if a keypress
is not already available, but will not wait for Enter to be pressed.
If the pressed key was a modifier key, nothing will be detected; if
it were a special function key, it may return the first character of
of an escape sequence, leaving additional characters in the buffer.
"""
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
input_char = _input_char
class SimpleEnum(Enum):
def __str__(self) -> str:
return self.value # type: ignore
def __repr__(self) -> str:
return self.__str__()
import os
from enum import Enum
from typing import Callable
def clear_terminal() -> None:
#print(chr(27) + "[2J")
os.system('clear')
input_char: Callable[[], str]
try:
import tty, termios
except ImportError:
# Probably Windows.
try:
import msvcrt
except ImportError:
# FIXME what to do on other platforms?
# Just give up here.
raise ImportError('getch not available')
else:
input_char = msvcrt.getch # type: ignore
else:
def _input_char() -> str:
"""getch() -> key character
Read a single keypress from stdin and return the resulting character.
Nothing is echoed to the console. This call will block if a keypress
is not already available, but will not wait for Enter to be pressed.
If the pressed key was a modifier key, nothing will be detected; if
it were a special function key, it may return the first character of
of an escape sequence, leaving additional characters in the buffer.
"""
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
input_char = _input_char
class SimpleEnum(Enum):
def __str__(self) -> str:
return self.value # type: ignore
def __repr__(self) -> str:
return self.__str__()
Last modified: Wednesday, 13 November 2019, 10:42 PM