Skip to content

Instantly share code, notes, and snippets.

@brentshermana
Last active December 18, 2017 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentshermana/0ddc3ba36e00007b256524317847f3bc to your computer and use it in GitHub Desktop.
Save brentshermana/0ddc3ba36e00007b256524317847f3bc to your computer and use it in GitHub Desktop.
Low Level Printing in Python
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