Last active
December 18, 2017 00:16
-
-
Save brentshermana/0ddc3ba36e00007b256524317847f3bc to your computer and use it in GitHub Desktop.
Low Level Printing in Python
This file contains 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
import time | |
import sys | |
import itertools | |
# inspired by tqdm https://github.com/tqdm/tqdm | |
class ConsolePrinter: | |
def __init__(self, file): | |
self.prev_line_len = 0 | |
self.file = file | |
def write(self, s): | |
self.file.write('\r' + s + (' ' * max(self.prev_line_len - len(s), 0)) ) | |
self.prev_line_len = len(s) | |
# https://stackoverflow.com/questions/287871/print-in-terminal-with-colors | |
bcolors = { | |
'HEADER': '\033[95m', | |
'OKBLUE': '\033[94m', | |
'OKGREEN': '\033[92m', | |
'WARNING': '\033[93m', | |
'FAIL': '\033[91m', | |
'ENDC': '\033[0m', | |
'BOLD': '\033[1m', | |
'UNDERLINE': '\033[4m', | |
} | |
printer = ConsolePrinter(sys.stdout) | |
for length, color in zip(itertools.cycle(range(10)), itertools.cycle(bcolors.keys())): | |
printer.write(bcolors[color] + '*' * length + bcolors['ENDC']) | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment