From e1cde9174a5ba42de8daccd7b74ea10093029220 Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Thu, 15 Aug 2019 21:19:38 +0200 Subject: :truck: Move package into folder --- TermIO/__init__.py | 23 ++++++++++++++++++ TermIO/in/__init__.py | 35 +++++++++++++++++++++++++++ TermIO/out/__init__.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 TermIO/__init__.py create mode 100644 TermIO/in/__init__.py create mode 100644 TermIO/out/__init__.py (limited to 'TermIO') diff --git a/TermIO/__init__.py b/TermIO/__init__.py new file mode 100644 index 0000000..fef84d4 --- /dev/null +++ b/TermIO/__init__.py @@ -0,0 +1,23 @@ +from .out import * + +import os + + +class TermSizeSkeleton: + + def __init__(self): + self.Update() + + def Update(self): + self.Rows, self.Columns = map(int, os.popen('stty size', 'r').read().split()) + + def Rows(self, Update=True): + if Update: self.Update() + return self.Rows + + def Columns(self, Update=True): + if Update: self.Update() + return self.Rows + + +TermSize = TermSizeSkeleton() diff --git a/TermIO/in/__init__.py b/TermIO/in/__init__.py new file mode 100644 index 0000000..b886141 --- /dev/null +++ b/TermIO/in/__init__.py @@ -0,0 +1,35 @@ +class _Getch: + """Gets a single character from standard input. Does not echo to the +screen.""" + def __init__(self): + try: + self.impl = _GetchWindows() + except ImportError: + self.impl = _GetchUnix() + + def __call__(self): return self.impl() + + +class _GetchUnix: + def __init__(self): + import tty, sys + + def __call__(self): + import sys, tty, termios + fd = sys.stdin.fileno() + old_settings = termios.tcgetattr(fd) + try: + tty.setraw(sys.stdin.fileno()) + ch = sys.stdin.read(1) + finally: + termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) + return ch + + +class _GetchWindows: + def __init__(self): + import msvcrt + + def __call__(self): + import msvcrt + return msvcrt.getch() diff --git a/TermIO/out/__init__.py b/TermIO/out/__init__.py new file mode 100644 index 0000000..1a69021 --- /dev/null +++ b/TermIO/out/__init__.py @@ -0,0 +1,64 @@ +import sys + + +class Cursor(): + + def SetPos(col, row, flush=True): + sys.stdout.write(u"\u001b[{0};{1}H".format(int(row), int(col))) + if flush: sys.stdout.flush() + + def Left(n, flush=True): + sys.stdout.write(u"\u001b[{0}D".format(str(n))) + if flush: sys.stdout.flush() + + def Right(n, flush=True): + sys.stdout.write(u"\u001b[{0}C".format(str(n))) + if flush: sys.stdout.flush() + + def Up(n, flush=True): + sys.stdout.write(u"\u001b[{0}A".format(str(n))) + if flush: sys.stdout.flush() + + def Down(n, flush=True): + sys.stdout.write(u"\u001b[{0}B".format(str(n))) + if flush: sys.stdout.flush() + + +class Screen(): + + def Clear(flush=True): + sys.stdout.write(u"\u001b[2J") # Clear screen + if flush: sys.stdout.flush() + + def Flush(): + sys.stdout.flush() + + def Decorate(fg=None, bg=None, dec=None, flush=True): + if (fg is None) and (bg is None) and (dec is None): + sys.stdout.write(u"\u001b[0m") + + if fg is not None: + sys.stdout.write(u"\u001b[38;5;{0}m".format(str(fg))) + if bg is not None: + sys.stdout.write(u"\u001b[48;5;{0}m".format(str(fg))) + if dec is not None: + if type(dec) == list: + for thing in dec: + if (thing == "reversed"): sys.stdout.write(u"\u001b[7m") + elif (thing == "underline"): sys.stdout.write(u"\u001b[4m") + elif (thing == "bold"): sys.stdout.write(u"\u001b[1m") + else: + raise ValueError('"{}" Not rekognized as a decoration'.format(thing)) + + elif type(dec) == str: + if (dec == "reversed"): sys.stdout.write(u"\u001b[7m") + elif (dec == "underline"): sys.stdout.write(u"\u001b[4m") + elif (dec == "bold"): sys.stdout.write(u"\u001b[1m") + else: + raise ValueError('"{}" Not rekognized as a decoration'.format(dec)) + + if flush: sys.stdout.flush() + + def Write(outStr, flush=True): + sys.stdout.write(str(outStr)) + if flush: sys.stdout.flush() -- cgit v1.2.3