Skip to content

Commit

Permalink
Merge branch 'widechar' into devel
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Oct 11, 2019
2 parents 1b1ec3e + b3d7739 commit c5d5af8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions tqdm/std.py
Expand Up @@ -12,7 +12,7 @@
from __future__ import division
# compatibility functions and utilities
from .utils import _supports_unicode, _environ_cols_wrapper, _range, _unich, \
_term_move_up, _unicode, WeakSet, _basestring, _OrderedDict, \
_term_move_up, _unicode, WeakSet, _basestring, _OrderedDict, _text_width, \
Comparable, RE_ANSI, _is_ascii, SimpleTextIOWrapper, FormatReplace
from ._monitor import TMonitor
# native libraries
Expand Down Expand Up @@ -474,7 +474,7 @@ def format_meter(n, total, elapsed, ncols=None, prefix='', ascii=False,
# Formatting progress bar space available for bar's display
full_bar = Bar(
frac,
max(1, ncols - len(RE_ANSI.sub('', nobar))) if ncols else 10,
max(1, ncols - _text_width(RE_ANSI.sub('', nobar))) if ncols else 10,
charset=Bar.ASCII if ascii is True else ascii or Bar.UTF)
if not _is_ascii(full_bar.charset) and _is_ascii(bar_format):
bar_format = _unicode(bar_format)
Expand All @@ -490,7 +490,7 @@ def format_meter(n, total, elapsed, ncols=None, prefix='', ascii=False,
return nobar
full_bar = Bar(
0,
max(1, ncols - len(RE_ANSI.sub('', nobar))) if ncols else 10,
max(1, ncols - _text_width(RE_ANSI.sub('', nobar))) if ncols else 10,
charset=Bar.BLANK)
return bar_format.format(bar=full_bar, **format_dict)
else:
Expand Down
7 changes: 7 additions & 0 deletions tqdm/tests/tests_tqdm.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Advice: use repr(our_file.read()) to print the full output of tqdm
# (else '\r' will replace the previous lines and you'll see only the latest.

Expand Down Expand Up @@ -277,6 +278,12 @@ def test_format_meter():
assert format_meter(20, 100, 12, ncols=14, rate=8.1,
bar_format=r'{l_bar}{bar}|{n_fmt}/{total_fmt}') == \
" 20%|" + unich(0x258d) + " |20/100"
# Check wide characters
if sys.version_info >= (3,):
assert format_meter(0, 1000, 13, ncols=68, prefix='fullwidth: ') == \
"fullwidth: 0%| | 0/1000 [00:13<?, ?it/s]"
assert format_meter(0, 1000, 13, ncols=68, prefix='ニッポン [ニッポン]: ') == \
"ニッポン [ニッポン]: 0%| | 0/1000 [00:13<?, ?it/s]"
# Check that bar_format can print only {bar} or just one side
assert format_meter(20, 100, 12, ncols=2, rate=8.1,
bar_format=r'{bar}') == \
Expand Down
10 changes: 10 additions & 0 deletions tqdm/utils.py
Expand Up @@ -283,3 +283,13 @@ def _environ_cols_linux(fp): # pragma: no cover

def _term_move_up(): # pragma: no cover
return '' if (os.name == 'nt') and (colorama is None) else '\x1b[A'


try:
# TODO consider using wcswidth third-party package for 0-width characters
from unicodedata import east_asian_width
except ImportError:
_text_width = len
else:
def _text_width(s):
return sum(2 if east_asian_width(ch) in 'FW' else 1 for ch in _unicode(s))

0 comments on commit c5d5af8

Please sign in to comment.