|
3 | 3 |
|
4 | 4 | import datetime |
5 | 5 |
|
6 | | -from tabulate import _CustomTextWrap as CTW, tabulate |
| 6 | +from tabulate import _CustomTextWrap as CTW, tabulate, _strip_ansi |
7 | 7 | from textwrap import TextWrapper as OTW |
8 | 8 |
|
9 | 9 | from common import skip, assert_equal |
@@ -158,6 +158,42 @@ def test_wrap_color_line_splillover(): |
158 | 158 | assert_equal(expected, result) |
159 | 159 |
|
160 | 160 |
|
| 161 | +def test_wrap_color_line_longword(): |
| 162 | + """TextWrapper: Wrap a line - preserve internal color tags and wrap them to |
| 163 | + other lines when required, requires adding the colors tags to other lines as appropriate |
| 164 | + and avoiding splitting escape codes.""" |
| 165 | + data = "This_is_a_\033[31mtest_string_for_testing_TextWrap\033[0m_with_colors" |
| 166 | + |
| 167 | + expected = [ |
| 168 | + "This_is_a_\033[31mte\033[0m", |
| 169 | + "\033[31mst_string_fo\033[0m", |
| 170 | + "\033[31mr_testing_Te\033[0m", |
| 171 | + "\033[31mxtWrap\033[0m_with_", |
| 172 | + "colors", |
| 173 | + ] |
| 174 | + wrapper = CTW(width=12) |
| 175 | + result = wrapper.wrap(data) |
| 176 | + assert_equal(expected, result) |
| 177 | + |
| 178 | + |
| 179 | +def test_wrap_color_line_multiple_escapes(): |
| 180 | + data = '012345(\x1b[32ma\x1b[0mbc\x1b[32mdefghij\x1b[0m)' |
| 181 | + expected = [ |
| 182 | + "012345(\x1b[32ma\x1b[0mbc\x1b[32mdefg\x1b[0m", |
| 183 | + "\x1b[32mhij\x1b[0m)", |
| 184 | + ] |
| 185 | + wrapper = CTW(width=10) |
| 186 | + result = wrapper.wrap(data) |
| 187 | + assert_equal(expected, result) |
| 188 | + clean_data = _strip_ansi(data) |
| 189 | + for width in range(2, len(clean_data)): |
| 190 | + # Currently fails with 14, 15 and 16, because a escape code gets split at the end |
| 191 | + wrapper = CTW(width=width) |
| 192 | + result = wrapper.wrap(data) |
| 193 | + # print(width, result) |
| 194 | + # Comparing after stripping ANSI should be enough to catch broken escape codes |
| 195 | + assert_equal(clean_data, _strip_ansi("".join(result))) |
| 196 | + |
161 | 197 | def test_wrap_datetime(): |
162 | 198 | """TextWrapper: Show that datetimes can be wrapped without crashing""" |
163 | 199 | data = [ |
|
0 commit comments