Skip to content

Instantly share code, notes, and snippets.

@vurpo
Created November 26, 2015 19:23
Show Gist options
  • Save vurpo/da8b3604302c7debf6ee to your computer and use it in GitHub Desktop.
Save vurpo/da8b3604302c7debf6ee to your computer and use it in GitHub Desktop.
HackerTyper for the terminal
#!/usr/bin/env python3
import tty,os,sys
import termios
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):
char = self.impl()
if char == '\x03':
raise KeyboardInterrupt
elif char == '\x04':
raise EOFError
return char
class _GetchUnix:
def __init__(self):
import tty
import sys
def __call__(self):
import sys
import tty
import 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()
getch = _Getch()
filecontents = open(sys.argv[1]).read()
sys.stdout.write("\033[?1049h\033[H")
#old_settings = termios.tcgetattr(sys.stdin.fileno())
tty.setcbreak(sys.stdin.fileno())
sys.stdout.write('\033[92m')
sys.stdout.flush()
try:
while True:
a = getch()
sys.stdout.write(filecontents[0:6])
sys.stdout.flush()
filecontents = filecontents[6:]
if len(filecontents) == 0:
break
finally:
#termios.tcsetattr(sys.stdio, termios.TCSADRAIN, old_settings|termios.ECHO)
print("\033[?1049l"+"\033[0m")
os.system("stty sane")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment