Last active
April 11, 2016 09:44
-
-
Save jancajthaml/a92113b2b2a43cd8c0cb3cb0909cf94d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import sys | |
import signal | |
import os | |
import fcntl | |
import termios | |
import struct | |
block = os.O_NONBLOCK if 'O_NONBLOCK' in dir(os) else ( | |
os.NOBLOCK if 'NONBLOCK' in dir(os) else None) | |
r = sys.stdin.fileno() | |
w = sys.stdout.fileno() | |
r_flags = fcntl.fcntl(r, fcntl.F_GETFL) | |
if r_flags & block: | |
fcntl.fcntl(r, fcntl.F_SETFL, r_flags & ~block) | |
w_flags = fcntl.fcntl(w, fcntl.F_GETFL) | |
if w_flags & block: | |
fcntl.fcntl(w, fcntl.F_SETFL, w_flags & ~block) | |
def main(): | |
def resize(*args): | |
h, w, hp, wp = struct.unpack('HHHH', fcntl.ioctl(0, termios.TIOCGWINSZ, struct.pack('HHHH', 0, 0, 0, 0))) # noqa | |
while 1: | |
try: | |
# render just a simple bordered box with resolution in middle | |
x = '{0} x {1}'.format(w, h) | |
l = int(float(w) / 2) | |
t = int(float(h) / 2) | |
fill = (w - l - (len(x) - int(len(x) / 2)) - 1) | |
screen = '.' + '.' * (w - 2) + '.\n' + \ | |
(':' + ' ' * (w - 2) + ':\n') * (t - 1) + \ | |
(':' + ' ' * (l - int(len(x) / 2) - 1) + x + | |
' ' * fill + ':\n') + \ | |
(':' + ' ' * (w - 2) + ':\n') * (h - t - 2) + \ | |
(':' + '.' * (w - 2) + ':') | |
sys.stdout.write(screen) | |
break | |
except: | |
continue | |
signal.signal(signal.SIGWINCH, resize) | |
try: | |
sys.stdout.write('\033[?1049h\033[H') | |
resize() | |
r_old = termios.tcgetattr(r) | |
r_new = termios.tcgetattr(r) | |
r_new[3] = r_new[3] & ~termios.ICANON & ~termios.ECHO | |
termios.tcsetattr(r, termios.TCSANOW, r_new) | |
try: | |
while 1: | |
try: | |
c = sys.stdin.read(1) | |
if c == '\n': | |
break | |
except IOError: | |
pass | |
finally: | |
termios.tcsetattr(r, termios.TCSAFLUSH, r_old) | |
except KeyboardInterrupt: | |
pass | |
finally: | |
fcntl.fcntl(r, fcntl.F_SETFL, r_flags) | |
fcntl.fcntl(w, fcntl.F_SETFL, w_flags) | |
sys.stdout.write('\033[?1049l') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment