Last active
September 15, 2015 01:54
-
-
Save phoemur/fdb59aefe1ff9014e3c1 to your computer and use it in GitHub Desktop.
This class prints a fancy waiting bar with greek chars and spins. It uses a thread to keep printing the bar while the main program runs
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
#!/usr/bin/env python | |
# -*- coding: UTF-8 -*- | |
import sys | |
import threading | |
import time | |
from itertools import cycle | |
class WaitingBar(object): | |
''' | |
This class prints a fancy waiting bar with Greek chars and spins. | |
It uses a thread to keep printing the bar while the main program runs | |
Usage: | |
THE_BAR = WaitingBar('Your Message Here') | |
# Do something slow here | |
(...) | |
THE_BAR.stop() | |
''' | |
def __init__(self, message='[*] Wait until loading is complete...'): | |
self.MESSAGE = ' ' + str(message) | |
self.CYCLES = ['-', '-', '\\', '\\', '|', '|', '/', '/', '-', '-', '\\', '\\', '|', '|', '/', '/'] | |
self.intab = u'abcdefghijklmnopqrstuvwxyzáàãâéèẽêíìîĩóòôõúùũûçABCDEFGHIJKLMNOPQRSTUVWXYZÁÀÃÂÉÈẼÊÍÌÎĨÓÒÔÕÚÙŨÛÇ' | |
self.outab = u'αβ¢ΔεϝγηιφκλμνΩπσϼΣτυϞωχψζααααεεεειιιιΩΩΩΩυυυυ¢αβ¢ΔεϝγηιφκλμνΩπσϼΣτυϞωχψζααααεεεειιιιΩΩΩΩυυυυ¢' | |
self.TABLE = {x: y for x, y in zip(self.intab, self.outab)} | |
self.encoding = sys.stdout.encoding.upper() | |
self.event = threading.Event() | |
self.waiting_bar = threading.Thread(target=self.start, args=(self.event,)) | |
self.waiting_bar.start() | |
def start(self, e): | |
for index in cycle(range(len(self.MESSAGE))): | |
if e.is_set(): | |
break | |
if not self.MESSAGE[index].isalpha(): | |
continue | |
for c in self.CYCLES: | |
buff = list(self.MESSAGE) | |
buff.append(c) | |
try: | |
if self.encoding == 'UTF-8': | |
buff[index] = self.TABLE[buff[index]] | |
else: | |
buff[index] = buff[index].swapcase() | |
except KeyError: | |
pass | |
sys.stdout.write(''.join(buff)) | |
time.sleep(0.05) | |
sys.stdout.write('\r') | |
sys.stdout.flush() | |
def stop(self): | |
self.event.set() | |
self.waiting_bar.join() | |
sys.stdout.write(self.MESSAGE + ' \n') | |
if __name__ == '__main__': | |
''' | |
A simple example to demonstrate the class in action | |
''' | |
# Start the bar | |
THE_BAR = WaitingBar('[*] Calculating useless stuff...') | |
# Do something slow | |
import math | |
from pprint import pprint | |
a_list = {a: b for a, b in zip(range(1, 41), map(math.factorial, range(1, 41)))} | |
time.sleep(20) | |
# Stop the bar and print result | |
THE_BAR.stop() | |
pprint(a_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment