Skip to content

Commit 6ea172b

Browse files
author
Daniel Aslau
committed
configure break long words and on hyphen for maxcolwidths
1 parent 83fd4fb commit 6ea172b

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

tabulate/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def _is_file(f):
3333

3434
# Whether or not to preserve leading/trailing whitespace in data.
3535
PRESERVE_WHITESPACE = False
36+
# TextWrapper breaks words longer than 'width'.
37+
BREAK_LONG_WORDS = True
38+
# TextWrapper is breaking hyphenated words.
39+
BREAK_ON_HYPHENS = True
3640

3741
_DEFAULT_FLOATFMT = "g"
3842
_DEFAULT_INTFMT = ""
@@ -1524,7 +1528,7 @@ def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True):
15241528
continue
15251529

15261530
if width is not None:
1527-
wrapper = _CustomTextWrap(width=width)
1531+
wrapper = _CustomTextWrap(width=width, break_long_words=BREAK_LONG_WORDS, break_on_hyphens=BREAK_ON_HYPHENS)
15281532
# Cast based on our internal type handling
15291533
# Any future custom formatting of types (such as datetimes)
15301534
# may need to be more explicit than just `str` of the object

test/test_output.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2973,3 +2973,36 @@ def test_preserve_whitespace():
29732973
expected = "\n".join(["h1 h2 h3", "---- ---- ----", "foo bar foo"])
29742974
result = tabulate(test_table, table_headers)
29752975
assert_equal(expected, result)
2976+
2977+
def test_break_long_words():
2978+
"Output: Default table output, with breakwords true."
2979+
table_headers = ["h1", "h2", "h3"]
2980+
test_table = [[" foo1", " bar2 ", "foo3"]]
2981+
2982+
# Table is not wrapped
2983+
tabulate_module.BREAK_LONG_WORDS = False
2984+
expected = "h1 h2 h3\n---- ---- ----\nfoo1 bar2 foo3"
2985+
result = tabulate(test_table, table_headers, maxcolwidths=3)
2986+
assert_equal(expected, result)
2987+
2988+
# Table is wrapped on 2 letters
2989+
tabulate_module.BREAK_LONG_WORDS = True
2990+
expected = "h1 h2 h3\n---- ---- ----\nf ba foo\noo1 r2 3"
2991+
result = tabulate(test_table, table_headers, maxcolwidths=3)
2992+
assert_equal(expected, result)
2993+
2994+
def test_break_on_hyphens():
2995+
"Output: Default table output, with break on hyphens true."
2996+
tabulate_module.BREAK_ON_HYPHENS = False
2997+
table_headers = ["h1", "h2", "h3"]
2998+
test_table = [[" foo-bar", " bar-bar ", "foo-foo"]]
2999+
# Table is wrapped on 2 letters
3000+
expected = "h1 h2 h3\n---- ---- -----\nfoo bar- foo-f\n-bar bar oo"
3001+
result = tabulate(test_table, table_headers, maxcolwidths=5)
3002+
assert_equal(expected, result)
3003+
3004+
# Table is no longer wrapped
3005+
tabulate_module.BREAK_ON_HYPHENS = True
3006+
expected = "h1 h2 h3\n---- ---- ----\nfoo- bar- foo-\nbar bar foo"
3007+
result = tabulate(test_table, table_headers, maxcolwidths=5)
3008+
assert_equal(expected, result)

0 commit comments

Comments
 (0)