Last active
August 29, 2015 14:04
-
-
Save NickBeeuwsaert/80556afd557823af29dd to your computer and use it in GitHub Desktop.
I was messing around with writing directly to a terminal
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 time | |
| import subprocess | |
| import sys | |
| import random | |
| import fcntl, termios, struct | |
| import re | |
| message = "Mess with the best. Die like the rest" | |
| with open(sys.argv[1], "wb", 0) as tty: | |
| height, width = struct.unpack('hh', fcntl.ioctl(tty.fileno(), termios.TIOCGWINSZ, '1234')) | |
| tty.write("\x1B[2J\x1B[H"); | |
| for c in "Wake up neo\nThe matrix has you\nFollow the white rabbit": | |
| tty.write(c) | |
| if c=='\n': | |
| tty.write('\r'); | |
| time.sleep(0.5 * random.random()) | |
| for word in message.split(' '): | |
| p = subprocess.Popen(["toilet", word], stdout=subprocess.PIPE) | |
| m = p.stdout.readlines() | |
| # clear the screen | |
| tty.write("\x1B[2J"); | |
| max_width = reduce(lambda y, x: max(len(x), y), m, 0) | |
| max_height = len(m) | |
| x = (width-max_width)/2 | |
| y = (height-max_height)/2 | |
| for l in m: | |
| tty.write("\x1B[{};{}H".format(y, x)) | |
| tty.write(l.rstrip("\n")); | |
| y = y + 1 | |
| time.sleep(0.25); | |
| time.sleep(0.25); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment