forked from ZSAIm/iqiyi-parser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path___progressbar.py
76 lines (59 loc) · 2.17 KB
/
___progressbar.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
# -*- coding=utf-8 -*-
from colorama import init
init()
class progressBar(object):
def __init__(self, id, sum, width=30, color='red', char=' '):
self.width = width
self.index = 0
self.sum = sum
self.char = char
self.color = color
self.id = id
def __pbarprint(self, color, mass_end=''):
perc = int((self.index*1.0/self.sum)*100)
count = int(perc / 100.0 * self.width)
bar = ''
bar1 = ''
for i in range(count):
bar += self.char
for i in range(self.width - count):
bar1 += ' '
colors = {
'black': '\033[0;37;40m',
'red': '\033[0;37;41m',
'green': '\033[0;37;42m',
'yellow': '\033[0;37;43m',
'blue': '\033[0;37;44m',
'magenta': '\033[0;37;45m',
'cyan': '\033[0;37;46m',
'white': '\033[0;30;47m'
}
sumlen = str(self.__countNum(self.sum))
if mass_end:
format_txt = '%03d: %3d%% |' + colors[color] + '%s\033[0m%s| %0' + sumlen + 'd/%0' + sumlen + 'd [ %s ]'
if len(bar) == 0: format_txt = '%03d: %3d%% |%s\033[0m%s| %0' + sumlen + 'd/%0' + sumlen + 'd [ %s ]'
print format_txt % (self.id, perc, bar, bar1, self.index, self.sum, mass_end)
else:
format_txt = '%03d: %3d%% |' + colors[color] + '%s\033[0m%s| %0' + sumlen + 'd/%0' + sumlen + 'd'
if len(bar) == 0: format_txt = '%03d: %3d%% |%s\033[0m%s| %0' + sumlen + 'd/%0' + sumlen + 'd'
print format_txt % (self.id, perc, bar, bar1, self.index, self.sum)
def update(self, index, mass_end='', color=None):
self.index = index
if color is None:
color = self.color
self.__pbarprint(color, mass_end)
def __placeSpace(self, words, space, left_align):
count = space - len(words)
sp = ''
for i in range(count):
sp += ' '
if left_align:
return words + sp
else:
return sp + words
def __countNum(self, num):
count = 1
while num / 10:
count += 1
num /= 10
return count