Skip to content

Commit ad9d670

Browse files
danielrobbinsastanin
authored andcommitted
Merged in drobbinsfuntoo/python-tabulate/feature/multiline-rows (pull request astanin#36)
fix calculation of padding for lines with ANSI colors.
2 parents 62fc31c + 2d9b1dc commit ad9d670

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

tabulate.py

+18-11
Original file line numberDiff line numberDiff line change
@@ -483,17 +483,18 @@ def _afterpoint(string):
483483
else:
484484
return -1 # not a number
485485

486-
487486
def _padleft(width, s):
488487
"""Flush right.
489488
490489
>>> _padleft(6, '\u044f\u0439\u0446\u0430') == ' \u044f\u0439\u0446\u0430'
491490
True
492491
493492
"""
494-
fmt = "{0:>%ds}" % width
495-
return fmt.format(s)
496-
493+
vis = _strip_invisible(s)
494+
pad = width - len(vis)
495+
if pad > 0:
496+
s = " " * pad + s
497+
return s
497498

498499
def _padright(width, s):
499500
"""Flush left.
@@ -502,9 +503,11 @@ def _padright(width, s):
502503
True
503504
504505
"""
505-
fmt = "{0:<%ds}" % width
506-
return fmt.format(s)
507-
506+
vis = _strip_invisible(s)
507+
pad = width - len(vis)
508+
if pad > 0:
509+
s = s + " " * pad
510+
return s
508511

509512
def _padboth(width, s):
510513
"""Center string.
@@ -513,8 +516,12 @@ def _padboth(width, s):
513516
True
514517
515518
"""
516-
fmt = "{0:^%ds}" % width
517-
return fmt.format(s)
519+
vis = _strip_invisible(s)
520+
pad = width - len(vis)
521+
if pad > 0:
522+
pad_l = pad//2
523+
s = " " * pad_l + s + " " * (pad - pad_l)
524+
return s
518525

519526

520527
def _padnone(ignore_width, s):
@@ -614,7 +621,7 @@ def _align_column(strings, alignment, minwidth=0,
614621
for ms in strings]
615622
else:
616623
# enable wide-character width corrections
617-
s_lens = [max((len(s) for s in re.split("[\r\n]", ms))) for ms in strings]
624+
s_lens = [max((width_fn(s) for s in re.split("[\r\n]", ms))) for ms in strings]
618625
visible_widths = [maxwidth - (w - l) for w, l in zip(s_widths, s_lens)]
619626
# wcswidth and _visible_width don't count invisible characters;
620627
# padfn doesn't need to apply another correction
@@ -625,7 +632,7 @@ def _align_column(strings, alignment, minwidth=0,
625632
padded_strings = [padfn(maxwidth, s) for s in strings]
626633
else:
627634
# enable wide-character width corrections
628-
s_lens = list(map(len, strings))
635+
s_lens = list(map(width_fn, strings))
629636
visible_widths = [maxwidth - (w - l) for w, l in zip(s_widths, s_lens)]
630637
# wcswidth and _visible_width don't count invisible characters;
631638
# padfn doesn't need to apply another correction

0 commit comments

Comments
 (0)