Skip to content

Commit

Permalink
Merge branch 'jnhansen/strip-ansi-codes' into devel
Browse files Browse the repository at this point in the history
- fixes #591 -> closes #592
- related to #450
  • Loading branch information
casperdcl committed Aug 17, 2018
2 parents 68f18b1 + d6b05fb commit 3b26d86
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
11 changes: 6 additions & 5 deletions tqdm/_tqdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# compatibility functions and utilities
from ._utils import _supports_unicode, _environ_cols_wrapper, _range, _unich, \
_term_move_up, _unicode, WeakSet, _basestring, _OrderedDict, \
Comparable
Comparable, RE_ANSI
from ._monitor import TMonitor
# native libraries
import sys
Expand Down Expand Up @@ -360,10 +360,11 @@ def format_meter(n, total, elapsed, ncols=None, prefix='', ascii=False,
# Else no progress bar, we can just format and return
return bar_format.format(**bar_args)

# Formatting progress bar
# space available for bar's display
N_BARS = max(1, ncols - len(l_bar) - len(r_bar)) if ncols \
else 10
# Formatting progress bar space available for bar's display
if ncols:
N_BARS = max(1, ncols - len(RE_ANSI.sub('', l_bar + r_bar)))
else:
N_BARS = 10

# format bar depending on availability of unicode/ascii chars
if ascii:
Expand Down
2 changes: 2 additions & 0 deletions tqdm/_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os
import subprocess
from platform import system as _curos
import re
CUR_OS = _curos()
IS_WIN = CUR_OS in ['Windows', 'cli']
IS_NIX = (not IS_WIN) and any(
CUR_OS.startswith(i) for i in
['CYGWIN', 'MSYS', 'Linux', 'Darwin', 'SunOS', 'FreeBSD', 'NetBSD'])
RE_ANSI = re.compile(r"\x1b\[[;\d]*[A-Za-z]")


# Py2/3 compat. Empty conditional to avoid coverage
Expand Down
13 changes: 13 additions & 0 deletions tqdm/tests/tests_tqdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,19 @@ def test_format_meter():
unich(0x258f) + "|test"


def test_ansi_escape_codes():
"""Test stripping of ANSI escape codes"""
format_meter = tqdm.format_meter
ansi = {'BOLD': '\033[1m',
'RED': '\033[91m',
'END': '\033[0m'}
desc = '{BOLD}{RED}Colored{END} description'.format(**ansi)
ncols = 123
ansi_len = sum([len(code) for code in ansi.values()])
meter = format_meter(0, 100, 0, ncols=ncols, prefix=desc)
assert len(meter) == ncols + ansi_len


def test_si_format():
"""Test SI unit prefixes"""
format_meter = tqdm.format_meter
Expand Down

0 comments on commit 3b26d86

Please sign in to comment.