@@ -429,39 +429,36 @@ def _afterpoint(string):
429
429
return - 1 # not a number
430
430
431
431
432
- def _padleft (width , s , has_invisible = True ):
432
+ def _padleft (width , s ):
433
433
"""Flush right.
434
434
435
435
>>> _padleft(6, '\u044f \u0439 \u0446 \u0430 ') == ' \u044f \u0439 \u0446 \u0430 '
436
436
True
437
437
438
438
"""
439
- iwidth = width + len (s ) - len (_strip_invisible (s )) if has_invisible else width
440
- fmt = "{0:>%ds}" % iwidth
439
+ fmt = "{0:>%ds}" % width
441
440
return fmt .format (s )
442
441
443
442
444
- def _padright (width , s , has_invisible = True ):
443
+ def _padright (width , s ):
445
444
"""Flush left.
446
445
447
446
>>> _padright(6, '\u044f \u0439 \u0446 \u0430 ') == '\u044f \u0439 \u0446 \u0430 '
448
447
True
449
448
450
449
"""
451
- iwidth = width + len (s ) - len (_strip_invisible (s )) if has_invisible else width
452
- fmt = "{0:<%ds}" % iwidth
450
+ fmt = "{0:<%ds}" % width
453
451
return fmt .format (s )
454
452
455
453
456
- def _padboth (width , s , has_invisible = True ):
454
+ def _padboth (width , s ):
457
455
"""Center string.
458
456
459
457
>>> _padboth(6, '\u044f \u0439 \u0446 \u0430 ') == ' \u044f \u0439 \u0446 \u0430 '
460
458
True
461
459
462
460
"""
463
- iwidth = width + len (s ) - len (_strip_invisible (s )) if has_invisible else width
464
- fmt = "{0:^%ds}" % iwidth
461
+ fmt = "{0:^%ds}" % width
465
462
return fmt .format (s )
466
463
467
464
@@ -529,15 +526,13 @@ def _align_column(strings, alignment, minwidth=0, has_invisible=True):
529
526
s_widths = list (map (width_fn , strings ))
530
527
maxwidth = max (max (s_widths ), minwidth )
531
528
if wcwidth is None and not has_invisible :
532
- padded_strings = [padfn (maxwidth , s , has_invisible = False )
533
- for s in strings ]
529
+ padded_strings = [padfn (maxwidth , s ) for s in strings ]
534
530
else :
535
531
# enable wide-character width corrections
536
532
visible_widths = [maxwidth - (w - l ) for w , l in zip (s_widths , s_lens )]
537
533
# wcswidth and _visible_width don't count invisible characters;
538
534
# padfn doesn't need to apply another correction
539
- padded_strings = [padfn (w , s , has_invisible = False )
540
- for s , w in zip (strings , visible_widths )]
535
+ padded_strings = [padfn (w , s ) for s , w in zip (strings , visible_widths )]
541
536
return padded_strings
542
537
543
538
@@ -606,7 +601,9 @@ def _format(val, valtype, floatfmt, missingval="", has_invisible=True):
606
601
return "{0}" .format (val )
607
602
608
603
609
- def _align_header (header , alignment , width ):
604
+ def _align_header (header , alignment , width , visible_width ):
605
+ "Pad string header to width chars given known visible_width of the header."
606
+ width += len (header ) - visible_width
610
607
if alignment == "left" :
611
608
return _padright (width , header )
612
609
elif alignment == "center" :
@@ -1062,7 +1059,7 @@ def tabulate(tabular_data, headers=(), tablefmt="simple",
1062
1059
t_cols = cols or [['' ]] * len (headers )
1063
1060
t_aligns = aligns or [stralign ] * len (headers )
1064
1061
minwidths = [max (minw , width_fn (c [0 ])) for minw , c in zip (minwidths , t_cols )]
1065
- headers = [_align_header (h , a , minw )
1062
+ headers = [_align_header (h , a , minw , width_fn ( h ) )
1066
1063
for h , a , minw in zip (headers , t_aligns , minwidths )]
1067
1064
rows = list (zip (* cols ))
1068
1065
else :
0 commit comments