@@ -46,8 +46,12 @@ def _is_file(f):
46
46
__version__ = "0.7.6-dev"
47
47
48
48
49
+ # minimum extra space in headers
49
50
MIN_PADDING = 2
50
51
52
+ # if True, enable wide-character (CJK) support
53
+ WIDE_CHARS_MODE = wcwidth is not None
54
+
51
55
52
56
Line = namedtuple ("Line" , ["begin" , "hline" , "sep" , "end" ])
53
57
@@ -478,7 +482,10 @@ def _visible_width(s):
478
482
479
483
"""
480
484
# 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
482
489
if isinstance (s , _text_type ) or isinstance (s , _binary_type ):
483
490
return len_fn (_strip_invisible (s ))
484
491
else :
@@ -516,16 +523,18 @@ def _align_column(strings, alignment, minwidth=0, has_invisible=True):
516
523
strings = [s .strip () for s in strings ]
517
524
padfn = _padright
518
525
526
+ enable_widechars = wcwidth is not None and WIDE_CHARS_MODE
519
527
if has_invisible :
520
528
width_fn = _visible_width
529
+ elif enable_widechars : # optional wide-character support if available
530
+ width_fn = wcwidth .wcswidth
521
531
else :
522
- # optional wide-character support if available
523
- width_fn = len if wcwidth is None else wcwidth .wcswidth
532
+ width_fn = len
524
533
525
534
s_lens = list (map (len , strings ))
526
535
s_widths = list (map (width_fn , strings ))
527
536
maxwidth = max (max (s_widths ), minwidth )
528
- if wcwidth is None and not has_invisible :
537
+ if not enable_widechars and not has_invisible :
529
538
padded_strings = [padfn (maxwidth , s ) for s in strings ]
530
539
else :
531
540
# enable wide-character width corrections
@@ -1036,11 +1045,13 @@ def tabulate(tabular_data, headers=(), tablefmt="simple",
1036
1045
['\t ' .join (map (_text_type , row )) for row in list_of_lists ])
1037
1046
1038
1047
has_invisible = re .search (_invisible_codes , plain_text )
1048
+ enable_widechars = wcwidth is not None and WIDE_CHARS_MODE
1039
1049
if has_invisible :
1040
1050
width_fn = _visible_width
1051
+ elif enable_widechars : # optional wide-character support if available
1052
+ width_fn = wcwidth .wcswidth
1041
1053
else :
1042
- # optional wide-character support if available
1043
- width_fn = len if wcwidth is None else wcwidth .wcswidth
1054
+ width_fn = len
1044
1055
1045
1056
# format rows and columns, convert numeric values to strings
1046
1057
cols = list (zip (* list_of_lists ))
0 commit comments