Skip to content

Commit fabc9de

Browse files
committed
Upgrade progress to 1.6
1 parent 674d8cf commit fabc9de

File tree

8 files changed

+133
-31
lines changed

8 files changed

+133
-31
lines changed

news/progress.vendor.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Upgrade progress to 1.6

src/pip/_vendor/progress/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2012 Giorgos Verigakis <[email protected]>
1+
# Copyright (c) 2012 Georgios Verigakis <[email protected]>
22
#
33
# Permission to use, copy, modify, and distribute this software for any
44
# purpose with or without fee is hereby granted, provided that the above

src/pip/_vendor/progress/__init__.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2012 Giorgos Verigakis <[email protected]>
1+
# Copyright (c) 2012 Georgios Verigakis <[email protected]>
22
#
33
# Permission to use, copy, modify, and distribute this software for any
44
# purpose with or without fee is hereby granted, provided that the above
@@ -24,7 +24,7 @@
2424
from time import time as monotonic
2525

2626

27-
__version__ = '1.5'
27+
__version__ = '1.6'
2828

2929
HIDE_CURSOR = '\x1b[?25l'
3030
SHOW_CURSOR = '\x1b[?25h'
@@ -46,14 +46,19 @@ def __init__(self, message='', **kwargs):
4646
for key, val in kwargs.items():
4747
setattr(self, key, val)
4848

49-
self._width = 0
49+
self._max_width = 0
50+
self._hidden_cursor = False
5051
self.message = message
5152

5253
if self.file and self.is_tty():
5354
if self.hide_cursor:
5455
print(HIDE_CURSOR, end='', file=self.file)
55-
print(self.message, end='', file=self.file)
56-
self.file.flush()
56+
self._hidden_cursor = True
57+
self.writeln('')
58+
59+
def __del__(self):
60+
if self._hidden_cursor:
61+
print(SHOW_CURSOR, end='', file=self.file)
5762

5863
def __getitem__(self, key):
5964
if key.startswith('_'):
@@ -85,31 +90,30 @@ def update(self):
8590
def start(self):
8691
pass
8792

88-
def clearln(self):
89-
if self.file and self.is_tty():
90-
print('\r\x1b[K', end='', file=self.file)
91-
92-
def write(self, s):
93-
if self.file and self.is_tty():
94-
line = self.message + s.ljust(self._width)
95-
print('\r' + line, end='', file=self.file)
96-
self._width = max(self._width, len(s))
97-
self.file.flush()
98-
9993
def writeln(self, line):
10094
if self.file and self.is_tty():
101-
self.clearln()
102-
print(line, end='', file=self.file)
95+
width = len(line)
96+
if width < self._max_width:
97+
# Add padding to cover previous contents
98+
line += ' ' * (self._max_width - width)
99+
else:
100+
self._max_width = width
101+
print('\r' + line, end='', file=self.file)
103102
self.file.flush()
104103

105104
def finish(self):
106105
if self.file and self.is_tty():
107106
print(file=self.file)
108-
if self.hide_cursor:
107+
if self._hidden_cursor:
109108
print(SHOW_CURSOR, end='', file=self.file)
109+
self._hidden_cursor = False
110110

111111
def is_tty(self):
112-
return self.file.isatty() if self.check_tty else True
112+
try:
113+
return self.file.isatty() if self.check_tty else True
114+
except AttributeError:
115+
msg = "%s has no attribute 'isatty'. Try setting check_tty=False." % self
116+
raise AttributeError(msg)
113117

114118
def next(self, n=1):
115119
now = monotonic()
@@ -120,10 +124,13 @@ def next(self, n=1):
120124
self.update()
121125

122126
def iter(self, it):
127+
self.iter_value = None
123128
with self:
124129
for x in it:
130+
self.iter_value = x
125131
yield x
126132
self.next()
133+
del self.iter_value
127134

128135
def __enter__(self):
129136
self.start()
@@ -152,6 +159,8 @@ def percent(self):
152159

153160
@property
154161
def progress(self):
162+
if self.max == 0:
163+
return 0
155164
return min(1, self.index / self.max)
156165

157166
@property
@@ -171,7 +180,10 @@ def iter(self, it):
171180
except TypeError:
172181
pass
173182

183+
self.iter_value = None
174184
with self:
175185
for x in it:
186+
self.iter_value = x
176187
yield x
177188
self.next()
189+
del self.iter_value

src/pip/_vendor/progress/bar.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (c) 2012 Giorgos Verigakis <[email protected]>
3+
# Copyright (c) 2012 Georgios Verigakis <[email protected]>
44
#
55
# Permission to use, copy, modify, and distribute this software for any
66
# purpose with or without fee is hereby granted, provided that the above
@@ -19,6 +19,7 @@
1919
import sys
2020

2121
from . import Progress
22+
from .colors import color
2223

2324

2425
class Bar(Progress):
@@ -28,13 +29,14 @@ class Bar(Progress):
2829
bar_suffix = '| '
2930
empty_fill = ' '
3031
fill = '#'
32+
color = None
3133

3234
def update(self):
3335
filled_length = int(self.width * self.progress)
3436
empty_length = self.width - filled_length
3537

3638
message = self.message % self
37-
bar = self.fill * filled_length
39+
bar = color(self.fill * filled_length, fg=self.color)
3840
empty = self.empty_fill * empty_length
3941
suffix = self.suffix % self
4042
line = ''.join([message, self.bar_prefix, bar, empty, self.bar_suffix,
@@ -74,7 +76,7 @@ def update(self):
7476
nempty = self.width - nfull # Number of empty chars
7577

7678
message = self.message % self
77-
bar = self.phases[-1] * nfull
79+
bar = color(self.phases[-1] * nfull, fg=self.color)
7880
current = self.phases[phase] if phase > 0 else ''
7981
empty = self.empty_fill * max(0, nempty - len(current))
8082
suffix = self.suffix % self

src/pip/_vendor/progress/colors.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright (c) 2020 Georgios Verigakis <[email protected]>
4+
#
5+
# Permission to use, copy, modify, and distribute this software for any
6+
# purpose with or without fee is hereby granted, provided that the above
7+
# copyright notice and this permission notice appear in all copies.
8+
#
9+
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15+
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16+
17+
from functools import partial
18+
19+
20+
COLORS = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan',
21+
'white')
22+
STYLES = ('bold', 'faint', 'italic', 'underline', 'blink', 'blink2',
23+
'negative', 'concealed', 'crossed')
24+
25+
26+
def color(s, fg=None, bg=None, style=None):
27+
sgr = []
28+
29+
if fg:
30+
if fg in COLORS:
31+
sgr.append(str(30 + COLORS.index(fg)))
32+
elif isinstance(fg, int) and 0 <= fg <= 255:
33+
sgr.append('38;5;%d' % int(fg))
34+
else:
35+
raise Exception('Invalid color "%s"' % fg)
36+
37+
if bg:
38+
if bg in COLORS:
39+
sgr.append(str(40 + COLORS.index(bg)))
40+
elif isinstance(bg, int) and 0 <= bg <= 255:
41+
sgr.append('48;5;%d' % bg)
42+
else:
43+
raise Exception('Invalid color "%s"' % bg)
44+
45+
if style:
46+
for st in style.split('+'):
47+
if st in STYLES:
48+
sgr.append(str(1 + STYLES.index(st)))
49+
else:
50+
raise Exception('Invalid style "%s"' % st)
51+
52+
if sgr:
53+
prefix = '\x1b[' + ';'.join(sgr) + 'm'
54+
suffix = '\x1b[0m'
55+
return prefix + s + suffix
56+
else:
57+
return s
58+
59+
60+
# Foreground shortcuts
61+
black = partial(color, fg='black')
62+
red = partial(color, fg='red')
63+
green = partial(color, fg='green')
64+
yellow = partial(color, fg='yellow')
65+
blue = partial(color, fg='blue')
66+
magenta = partial(color, fg='magenta')
67+
cyan = partial(color, fg='cyan')
68+
white = partial(color, fg='white')
69+
70+
# Style shortcuts
71+
bold = partial(color, style='bold')
72+
faint = partial(color, style='faint')
73+
italic = partial(color, style='italic')
74+
underline = partial(color, style='underline')
75+
blink = partial(color, style='blink')
76+
blink2 = partial(color, style='blink2')
77+
negative = partial(color, style='negative')
78+
concealed = partial(color, style='concealed')
79+
crossed = partial(color, style='crossed')

src/pip/_vendor/progress/counter.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (c) 2012 Giorgos Verigakis <[email protected]>
3+
# Copyright (c) 2012 Georgios Verigakis <[email protected]>
44
#
55
# Permission to use, copy, modify, and distribute this software for any
66
# purpose with or without fee is hereby granted, provided that the above
@@ -20,12 +20,16 @@
2020

2121
class Counter(Infinite):
2222
def update(self):
23-
self.write(str(self.index))
23+
message = self.message % self
24+
line = ''.join([message, str(self.index)])
25+
self.writeln(line)
2426

2527

2628
class Countdown(Progress):
2729
def update(self):
28-
self.write(str(self.remaining))
30+
message = self.message % self
31+
line = ''.join([message, str(self.remaining)])
32+
self.writeln(line)
2933

3034

3135
class Stack(Progress):
@@ -34,7 +38,9 @@ class Stack(Progress):
3438
def update(self):
3539
nphases = len(self.phases)
3640
i = min(nphases - 1, int(self.progress * nphases))
37-
self.write(self.phases[i])
41+
message = self.message % self
42+
line = ''.join([message, self.phases[i]])
43+
self.writeln(line)
3844

3945

4046
class Pie(Stack):

src/pip/_vendor/progress/spinner.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (c) 2012 Giorgos Verigakis <[email protected]>
3+
# Copyright (c) 2012 Georgios Verigakis <[email protected]>
44
#
55
# Permission to use, copy, modify, and distribute this software for any
66
# purpose with or without fee is hereby granted, provided that the above
@@ -24,7 +24,9 @@ class Spinner(Infinite):
2424

2525
def update(self):
2626
i = self.index % len(self.phases)
27-
self.write(self.phases[i])
27+
message = self.message % self
28+
line = ''.join([message, self.phases[i]])
29+
self.writeln(line)
2830

2931

3032
class PieSpinner(Spinner):

src/pip/_vendor/vendor.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgpack==1.0.2
77
packaging==21.0
88
pep517==0.11.0
99
platformdirs==2.4.0
10-
progress==1.5
10+
progress==1.6
1111
pyparsing==2.4.7
1212
requests==2.26.0
1313
certifi==2021.05.30

0 commit comments

Comments
 (0)