Skip to content

Commit bb6a5c4

Browse files
committed
recognize colored numeric values properly
1 parent a7673f4 commit bb6a5c4

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

tabulate.py

+8
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,16 @@ def _type(string):
158158
True
159159
>>> _type("1") is type(1)
160160
True
161+
>>> _type(u'\x1b[31m42\x1b[0m') is type(42)
162+
True
163+
>>> _type('\x1b[31m42\x1b[0m') is type(42)
164+
True
161165
162166
"""
167+
168+
if isinstance(string, _text_type) or isinstance(string, _binary_type):
169+
string = _strip_invisible(string)
170+
163171
if string is None:
164172
return _none_type
165173
elif _isint(string):

test_regression.py

+10
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,13 @@ def test_ansi_color_in_table_cells():
1616
correcttable = u'| test | test | test |\n|:-------|:-------|:-------|\n| test | \x1b[31mtest\x1b[0m | \x1b[32mtest\x1b[0m |'
1717
print("expected: %r\n\ngot: %r\n" % (correcttable, formattedtable))
1818
assert correcttable == formattedtable
19+
20+
21+
def test_alignment_of_colored_cells():
22+
"Align ANSI-colored values as if they were colorless."
23+
colortable = [('test', 42, '\x1b[31m42\x1b[0m'), ('test', 101, '\x1b[32m101\x1b[0m')]
24+
colorheaders = ('test', '\x1b[34mtest\x1b[0m', 'test')
25+
formatted = tabulate(colortable, colorheaders, 'grid')
26+
expected = u'+--------+--------+--------+\n| test | \x1b[34mtest\x1b[0m | test |\n+========+========+========+\n| test | 42 | \x1b[31m42\x1b[0m |\n+--------+--------+--------+\n| test | 101 | \x1b[32m101\x1b[0m |\n+--------+--------+--------+'
27+
print("expected: %r\n\ngot: %r\n" % (expected, formatted))
28+
assert expected == formatted

0 commit comments

Comments
 (0)