Skip to content

Commit f89db53

Browse files
committed
fix astanin#42: broken decimal alignment of ANSI-colored numberes
1 parent a9b9765 commit f89db53

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

tabulate.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,10 @@ def _align_column(strings, alignment, minwidth=0, has_invisible=True):
442442
strings = [s.strip() for s in strings]
443443
padfn = _padboth
444444
elif alignment == "decimal":
445-
decimals = [_afterpoint(s) for s in strings]
445+
if has_invisible:
446+
decimals = [_afterpoint(_strip_invisible(s)) for s in strings]
447+
else:
448+
decimals = [_afterpoint(s) for s in strings]
446449
maxdecimals = max(decimals)
447450
strings = [s + (maxdecimals - decs) * " "
448451
for s, decs in zip(strings, decimals)]

test/test_regression.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,15 @@ def test_ansi_color_for_decimal_numbers():
194194
'------- ---'])
195195
result = tabulate(table)
196196
assert_equal(result, expected)
197+
198+
199+
def test_alignment_of_decimal_numbers_with_ansi_color():
200+
"Regression: alignment for decimal numbers with ANSI color (issue #42)"
201+
v1 = "\033[95m" + "12.34" + "\033[0m"
202+
v2 = "\033[95m" + "1.23456" + "\033[0m"
203+
table = [[v1], [v2]]
204+
expected = "\n".join([
205+
'\x1b[95m12.34\x1b[0m',
206+
' \x1b[95m1.23456\x1b[0m'])
207+
result = tabulate(table, tablefmt="plain")
208+
assert_equal(result, expected)

0 commit comments

Comments
 (0)