Skip to content

Commit c653a1f

Browse files
committed
global flag WIDE_CHARS_MODE to enable/disable double width character alignment
1 parent 23c3edc commit c653a1f

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

tabulate.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ def _is_file(f):
4646
__version__ = "0.7.6-dev"
4747

4848

49+
# minimum extra space in headers
4950
MIN_PADDING = 2
5051

52+
# if True, enable wide-character (CJK) support
53+
WIDE_CHARS_MODE = wcwidth is not None
54+
5155

5256
Line = namedtuple("Line", ["begin", "hline", "sep", "end"])
5357

@@ -478,7 +482,10 @@ def _visible_width(s):
478482
479483
"""
480484
# optional wide-character support
481-
len_fn = len if wcwidth is None else wcwidth.wcswidth
485+
if wcwidth is not None and WIDE_CHARS_MODE:
486+
len_fn = wcwidth.wcswidth
487+
else:
488+
len_fn = len
482489
if isinstance(s, _text_type) or isinstance(s, _binary_type):
483490
return len_fn(_strip_invisible(s))
484491
else:
@@ -516,16 +523,18 @@ def _align_column(strings, alignment, minwidth=0, has_invisible=True):
516523
strings = [s.strip() for s in strings]
517524
padfn = _padright
518525

526+
enable_widechars = wcwidth is not None and WIDE_CHARS_MODE
519527
if has_invisible:
520528
width_fn = _visible_width
529+
elif enable_widechars: # optional wide-character support if available
530+
width_fn = wcwidth.wcswidth
521531
else:
522-
# optional wide-character support if available
523-
width_fn = len if wcwidth is None else wcwidth.wcswidth
532+
width_fn = len
524533

525534
s_lens = list(map(len, strings))
526535
s_widths = list(map(width_fn, strings))
527536
maxwidth = max(max(s_widths), minwidth)
528-
if wcwidth is None and not has_invisible:
537+
if not enable_widechars and not has_invisible:
529538
padded_strings = [padfn(maxwidth, s) for s in strings]
530539
else:
531540
# enable wide-character width corrections
@@ -1036,11 +1045,13 @@ def tabulate(tabular_data, headers=(), tablefmt="simple",
10361045
['\t'.join(map(_text_type, row)) for row in list_of_lists])
10371046

10381047
has_invisible = re.search(_invisible_codes, plain_text)
1048+
enable_widechars = wcwidth is not None and WIDE_CHARS_MODE
10391049
if has_invisible:
10401050
width_fn = _visible_width
1051+
elif enable_widechars: # optional wide-character support if available
1052+
width_fn = wcwidth.wcswidth
10411053
else:
1042-
# optional wide-character support if available
1043-
width_fn = len if wcwidth is None else wcwidth.wcswidth
1054+
width_fn = len
10441055

10451056
# format rows and columns, convert numeric values to strings
10461057
cols = list(zip(*list_of_lists))

0 commit comments

Comments
 (0)