Skip to content

Commit 23c3edc

Browse files
committed
refactor: simplify _padleft, _padright and _padboth
1 parent b96fb5f commit 23c3edc

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

tabulate.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -429,39 +429,36 @@ def _afterpoint(string):
429429
return -1 # not a number
430430

431431

432-
def _padleft(width, s, has_invisible=True):
432+
def _padleft(width, s):
433433
"""Flush right.
434434
435435
>>> _padleft(6, '\u044f\u0439\u0446\u0430') == ' \u044f\u0439\u0446\u0430'
436436
True
437437
438438
"""
439-
iwidth = width + len(s) - len(_strip_invisible(s)) if has_invisible else width
440-
fmt = "{0:>%ds}" % iwidth
439+
fmt = "{0:>%ds}" % width
441440
return fmt.format(s)
442441

443442

444-
def _padright(width, s, has_invisible=True):
443+
def _padright(width, s):
445444
"""Flush left.
446445
447446
>>> _padright(6, '\u044f\u0439\u0446\u0430') == '\u044f\u0439\u0446\u0430 '
448447
True
449448
450449
"""
451-
iwidth = width + len(s) - len(_strip_invisible(s)) if has_invisible else width
452-
fmt = "{0:<%ds}" % iwidth
450+
fmt = "{0:<%ds}" % width
453451
return fmt.format(s)
454452

455453

456-
def _padboth(width, s, has_invisible=True):
454+
def _padboth(width, s):
457455
"""Center string.
458456
459457
>>> _padboth(6, '\u044f\u0439\u0446\u0430') == ' \u044f\u0439\u0446\u0430 '
460458
True
461459
462460
"""
463-
iwidth = width + len(s) - len(_strip_invisible(s)) if has_invisible else width
464-
fmt = "{0:^%ds}" % iwidth
461+
fmt = "{0:^%ds}" % width
465462
return fmt.format(s)
466463

467464

@@ -529,15 +526,13 @@ def _align_column(strings, alignment, minwidth=0, has_invisible=True):
529526
s_widths = list(map(width_fn, strings))
530527
maxwidth = max(max(s_widths), minwidth)
531528
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]
534530
else:
535531
# enable wide-character width corrections
536532
visible_widths = [maxwidth - (w - l) for w, l in zip(s_widths, s_lens)]
537533
# wcswidth and _visible_width don't count invisible characters;
538534
# 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)]
541536
return padded_strings
542537

543538

@@ -606,7 +601,9 @@ def _format(val, valtype, floatfmt, missingval="", has_invisible=True):
606601
return "{0}".format(val)
607602

608603

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
610607
if alignment == "left":
611608
return _padright(width, header)
612609
elif alignment == "center":
@@ -1062,7 +1059,7 @@ def tabulate(tabular_data, headers=(), tablefmt="simple",
10621059
t_cols = cols or [['']] * len(headers)
10631060
t_aligns = aligns or [stralign] * len(headers)
10641061
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))
10661063
for h, a, minw in zip(headers, t_aligns, minwidths)]
10671064
rows = list(zip(*cols))
10681065
else:

0 commit comments

Comments
 (0)