Skip to content

Commit

Permalink
Merge 904af0d into ebda712
Browse files Browse the repository at this point in the history
  • Loading branch information
jeking3 committed Apr 17, 2019
2 parents ebda712 + 904af0d commit 83c90e7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
46 changes: 39 additions & 7 deletions tqdm/_tqdm.py
Expand Up @@ -255,7 +255,7 @@ def print_status(s):
@staticmethod
def format_meter(n, total, elapsed, ncols=None, prefix='', ascii=False,
unit='it', unit_scale=False, rate=None, bar_format=None,
postfix=None, unit_divisor=1000, **extra_kwargs):
bar_width=0, postfix=None, unit_divisor=1000, **extra_kwargs):
"""
Return a string-based progress bar given some parameters
Expand Down Expand Up @@ -303,6 +303,9 @@ def format_meter(n, total, elapsed, ncols=None, prefix='', ascii=False,
remaining, remaining_s, desc, postfix, unit.
Note that a trailing ": " is automatically removed after {desc}
if the latter is empty.
bar_width : int, optional
Specify a fixed bar width. A bar width of 0 means it can change
with each update [default: 0]
postfix : *, optional
Similar to `prefix`, but placed at the end
(e.g. for additional stats).
Expand Down Expand Up @@ -419,11 +422,35 @@ def format_meter(n, total, elapsed, ncols=None, prefix='', ascii=False,
return bar_format.format(**format_dict)

# Formatting progress bar space available for bar's display
if ncols:
N_BARS = max(1, ncols - len(RE_ANSI.sub('', l_bar + r_bar)))
l_bar_len = len(RE_ANSI.sub('', l_bar))
r_bar_len = len(RE_ANSI.sub('', r_bar))
remaining_space = ncols or 999

if remaining_space < l_bar_len:
# l_bar is larger than remaining space
# TODO: How to property trim with ANSI in the string?
return l_bar[0:remaining_space]
remaining_space -= l_bar_len

if bar_width:
N_BARS = bar_width
elif ncols:
N_BARS = max(1, ncols - (l_bar_len + r_bar_len))
else:
N_BARS = 10

if remaining_space < N_BARS:
# there isn't enough space for the full bar
N_BARS = remaining_space
remaining_space = 0
else:
remaining_space -= N_BARS

if remaining_space < r_bar_len:
# there isn't enough space for the r_bar
# TODO: How to property trim with ANSI in the string?
r_bar = r_bar[0:remaining_space]

# format bar depending on availability of unicode/ascii chars
if ascii is True:
ascii = ASCII_FMT
Expand Down Expand Up @@ -709,8 +736,10 @@ def __init__(self, iterable=None, desc=None, total=None, leave=True,
file=None, ncols=None, mininterval=0.1, maxinterval=10.0,
miniters=None, ascii=None, disable=False, unit='it',
unit_scale=False, dynamic_ncols=False, smoothing=0.3,
bar_format=None, initial=0, position=None, postfix=None,
unit_divisor=1000, write_bytes=None, gui=False, **kwargs):
bar_format=None, bar_width=None, initial=0, position=None,
postfix=None, unit_divisor=1000, write_bytes=None, gui=False,
**kwargs):

"""
Parameters
----------
Expand Down Expand Up @@ -789,6 +818,8 @@ def __init__(self, iterable=None, desc=None, total=None, leave=True,
remaining_s, desc, postfix, unit.
Note that a trailing ": " is automatically removed after {desc}
if the latter is empty.
bar_width : int, optional
Specify a fixed bar width. [default: no fixed width]
initial : int, optional
The initial counter value. Useful when restarting a progress
bar [default: 0].
Expand Down Expand Up @@ -919,6 +950,7 @@ def __init__(self, iterable=None, desc=None, total=None, leave=True,
self.avg_time = None
self._time = time
self.bar_format = bar_format
self.bar_width = bar_width
self.postfix = None
if postfix:
try:
Expand Down Expand Up @@ -977,8 +1009,8 @@ def format_dict(self):
prefix=self.desc, ascii=self.ascii, unit=self.unit,
unit_scale=self.unit_scale,
rate=1 / self.avg_time if self.avg_time else None,
bar_format=self.bar_format, postfix=self.postfix,
unit_divisor=self.unit_divisor)
bar_format=self.bar_format, bar_width=self.bar_width,
postfix=self.postfix, unit_divisor=self.unit_divisor)

def __repr__(self):
return self.format_meter(**self.format_dict)
Expand Down
2 changes: 1 addition & 1 deletion tqdm/_version.py
Expand Up @@ -5,7 +5,7 @@
__all__ = ["__version__"]

# major, minor, patch, -extra
version_info = 4, 31, 1
version_info = 4, 32, 0

# Nice string for the version
__version__ = '.'.join(map(str, version_info))
Expand Down
23 changes: 22 additions & 1 deletion tqdm/tests/tests_tqdm.py
Expand Up @@ -263,6 +263,27 @@ def test_format_meter():
"100kiB [00:13, 7.69kiB/s]"
assert format_meter(100, 1000, 12, ncols=0, rate=7.33) == \
" 10% 100/1000 [00:12<02:02, 7.33it/s]"
# ncols is small, l_bar is too large
# l_bar gets chopped
# no bar
# no r_bar
assert \
format_meter(0, 1000, 13, ncols=10, bar_width=10, bar_format="************{bar}$$$$$$$$$$") == \
"**********" # 10/12 stars since ncols is 10
# n_cols allows for l_bar and some of bar
# l_bar displays
# bar gets chopped
# no r_bar
assert \
format_meter(0, 1000, 13, ncols=20, bar_width=10, bar_format="************{bar}$$$$$$$$$$") == \
"************ " # all 12 stars and 8/10 bar parts
# n_cols allows for l_bar, bar, and some of r_bar
# l_bar displays
# bar displays
# r_bar gets chopped
assert \
format_meter(0, 1000, 13, ncols=30, bar_width=10, bar_format="************{bar}$$$$$$$$$$") == \
"************ $$$$$$$$" # all 12 stars and 10 bar parts, but only 8/10 dollar signs
# Check that bar_format correctly adapts {bar} size to the rest
assert format_meter(20, 100, 12, ncols=13, rate=8.1,
bar_format=r'{l_bar}{bar}|{n_fmt}/{total_fmt}') == \
Expand Down Expand Up @@ -876,7 +897,7 @@ def test_ascii():
for ascii in [" .oO0", " #"]:
with closing(StringIO()) as our_file:
for _ in tqdm(_range(len(ascii) - 1), file=our_file, miniters=1,
mininterval=0, ascii=ascii, ncols=1):
mininterval=0, ascii=ascii, ncols=27):
pass
res = our_file.getvalue().strip("\r").split("\r")
for bar, line in zip(ascii, res):
Expand Down

0 comments on commit 83c90e7

Please sign in to comment.