Skip to content

Commit 51f15fa

Browse files
authored
Merge pull request #218 from adan79/master
Configure break long words and on hyphen for maxcolwidths
2 parents 4042e61 + bf3c2ef commit 51f15fa

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,31 @@ the lines being wrapped would probably be significantly longer than this.
10631063

10641064
```
10651065

1066+
Text is preferably wrapped on whitespaces and right after the hyphens in hyphenated words.
1067+
1068+
break_long_words (default: True) If true, then words longer than width will be broken in order to ensure that no lines are longer than width.
1069+
If it is false, long words will not be broken, and some lines may be longer than width.
1070+
(Long words will be put on a line by themselves, in order to minimize the amount by which width is exceeded.)
1071+
1072+
break_on_hyphens (default: True) If true, wrapping will occur preferably on whitespaces and right after hyphens in compound words, as it is customary in English.
1073+
If false, only whitespaces will be considered as potentially good places for line breaks.
1074+
1075+
```pycon
1076+
>>> print(tabulate([["John Smith", "Middle-Manager"]], headers=["Name", "Title"], tablefmt="grid", maxcolwidths=[None, 5], break_long_words=False))
1077+
+------------+---------+
1078+
| Name | Title |
1079+
+============+=========+
1080+
| John Smith | Middle- |
1081+
| | Manager |
1082+
+------------+---------+
1083+
>>> print(tabulate([["John Smith", "Middle-Manager"]], headers=["Name", "Title"], tablefmt="grid", maxcolwidths=[None, 5], break_long_words=False, break_on_hyphens=False))
1084+
+------------+----------------+
1085+
| Name | Title |
1086+
+============+================+
1087+
| John Smith | Middle-Manager |
1088+
+------------+----------------+
1089+
```
1090+
10661091
### Adding Separating lines
10671092
One might want to add one or more separating lines to highlight different sections in a table.
10681093

tabulate/__init__.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ def _is_file(f):
3333
# minimum extra space in headers
3434
MIN_PADDING = 2
3535

36+
# Whether or not to preserve leading/trailing whitespace in data.
37+
PRESERVE_WHITESPACE = False
38+
39+
# TextWrapper breaks words longer than 'width'.
40+
_BREAK_LONG_WORDS = True
41+
# TextWrapper is breaking hyphenated words.
42+
_BREAK_ON_HYPHENS = True
43+
44+
3645
_DEFAULT_FLOATFMT = "g"
3746
_DEFAULT_INTFMT = ""
3847
_DEFAULT_MISSINGVAL = ""
@@ -1629,7 +1638,7 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"):
16291638
return rows, headers, headers_pad
16301639

16311640

1632-
def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True):
1641+
def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True, break_long_words=_BREAK_LONG_WORDS, break_on_hyphens=_BREAK_ON_HYPHENS):
16331642
if len(list_of_lists):
16341643
num_cols = len(list_of_lists[0])
16351644
else:
@@ -1646,7 +1655,7 @@ def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True):
16461655
continue
16471656

16481657
if width is not None:
1649-
wrapper = _CustomTextWrap(width=width)
1658+
wrapper = _CustomTextWrap(width=width, break_long_words=break_long_words, break_on_hyphens=break_on_hyphens)
16501659
casted_cell = str(cell)
16511660
wrapped = [
16521661
"\n".join(wrapper.wrap(line))
@@ -1705,6 +1714,8 @@ def tabulate(
17051714
headersalign=None,
17061715
rowalign=None,
17071716
maxheadercolwidths=None,
1717+
break_long_words=_BREAK_LONG_WORDS,
1718+
break_on_hyphens=_BREAK_ON_HYPHENS,
17081719
):
17091720
"""Format a fixed width table for pretty printing.
17101721
@@ -2247,7 +2258,7 @@ def tabulate(
22472258

22482259
numparses = _expand_numparse(disable_numparse, num_cols)
22492260
list_of_lists = _wrap_text_to_colwidths(
2250-
list_of_lists, maxcolwidths, numparses=numparses
2261+
list_of_lists, maxcolwidths, numparses=numparses, break_long_words=break_long_words, break_on_hyphens=break_on_hyphens
22512262
)
22522263

22532264
if maxheadercolwidths is not None:
@@ -2261,7 +2272,7 @@ def tabulate(
22612272

22622273
numparses = _expand_numparse(disable_numparse, num_cols)
22632274
headers = _wrap_text_to_colwidths(
2264-
[headers], maxheadercolwidths, numparses=numparses
2275+
[headers], maxheadercolwidths, numparses=numparses, break_long_words=break_long_words, break_on_hyphens=break_on_hyphens
22652276
)[0]
22662277

22672278
# empty values in the first column of RST tables should be escaped (issue #82)

test/test_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def test_tabulate_signature():
5656
("headersalign", None),
5757
("rowalign", None),
5858
("maxheadercolwidths", None),
59+
("break_long_words", True),
60+
("break_on_hyphens", True),
5961
]
6062
_check_signature(tabulate, expected_sig)
6163

test/test_output.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3319,3 +3319,32 @@ def test_preserve_whitespace():
33193319
expected = "\n".join(["h1 h2 h3", "---- ---- ----", "foo bar foo"])
33203320
result = tabulate(test_table, table_headers, preserve_whitespace=False)
33213321
assert_equal(expected, result)
3322+
3323+
def test_break_long_words():
3324+
"Output: Default table output, with breakwords true."
3325+
table_headers = ["h1", "h2", "h3"]
3326+
test_table = [[" foo1", " bar2 ", "foo3"]]
3327+
3328+
# Table is not wrapped on 3 letters due to long word
3329+
expected = "h1 h2 h3\n---- ---- ----\nfoo1 bar2 foo3"
3330+
result = tabulate(test_table, table_headers, maxcolwidths=3, break_long_words=False)
3331+
assert_equal(expected, result)
3332+
3333+
# Table max width is 3 letters
3334+
expected = "h1 h2 h3\n---- ---- ----\nf ba foo\noo1 r2 3"
3335+
result = tabulate(test_table, table_headers, maxcolwidths=3, break_long_words=True)
3336+
assert_equal(expected, result)
3337+
3338+
def test_break_on_hyphens():
3339+
"Output: Default table output, with break on hyphens true."
3340+
table_headers = ["h1", "h2", "h3"]
3341+
test_table = [[" foo-bar", " bar-bar ", "foo-foo"]]
3342+
# Table max width is 5, long lines breaks on hyphens
3343+
expected = "h1 h2 h3\n---- ---- -----\nfoo bar- foo-f\n-bar bar oo"
3344+
result = tabulate(test_table, table_headers, maxcolwidths=5, break_on_hyphens=False)
3345+
assert_equal(expected, result)
3346+
3347+
# Table data is no longer breaks on hyphens
3348+
expected = "h1 h2 h3\n---- ---- ----\nfoo- bar- foo-\nbar bar foo"
3349+
result = tabulate(test_table, table_headers, maxcolwidths=5, break_on_hyphens=True)
3350+
assert_equal(expected, result)

0 commit comments

Comments
 (0)