-
Notifications
You must be signed in to change notification settings - Fork 41
/
waitingbar.py
82 lines (63 loc) · 2.51 KB
/
waitingbar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
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()
copyright phoemur - 2016
'''
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.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 sys.stdout.encoding.upper() == '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)