Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure break long words and on hyphen for maxcolwidths #218

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,31 @@ the lines being wrapped would probably be significantly longer than this.
+------------+---------+
```

Text is preferably wrapped on whitespaces and right after the hyphens in hyphenated words.

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.
If it is false, long words will not be broken, and some lines may be longer than width.
(Long words will be put on a line by themselves, in order to minimize the amount by which width is exceeded.)

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.
If false, only whitespaces will be considered as potentially good places for line breaks.

```pycon
>>> print(tabulate([["John Smith", "Middle-Manager"]], headers=["Name", "Title"], tablefmt="grid", maxcolwidths=[None, 5], break_long_words=False))
+------------+---------+
| Name | Title |
+============+=========+
| John Smith | Middle- |
| | Manager |
+------------+---------+
>>> print(tabulate([["John Smith", "Middle-Manager"]], headers=["Name", "Title"], tablefmt="grid", maxcolwidths=[None, 5], break_long_words=False, break_on_hyphens=False))
+------------+----------------+
| Name | Title |
+============+================+
| John Smith | Middle-Manager |
+------------+----------------+
```

### Adding Separating lines
One might want to add one or more separating lines to highlight different sections in a table.

Expand Down
15 changes: 11 additions & 4 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ def _is_file(f):
# Whether or not to preserve leading/trailing whitespace in data.
PRESERVE_WHITESPACE = False

# TextWrapper breaks words longer than 'width'.
_BREAK_LONG_WORDS = True
# TextWrapper is breaking hyphenated words.
_BREAK_ON_HYPHENS = True

_DEFAULT_FLOATFMT = "g"
_DEFAULT_INTFMT = ""
_DEFAULT_MISSINGVAL = ""
Expand Down Expand Up @@ -1507,7 +1512,7 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"):
return rows, headers


def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True):
def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True, break_long_words=_BREAK_LONG_WORDS, break_on_hyphens=_BREAK_ON_HYPHENS):
if len(list_of_lists):
num_cols = len(list_of_lists[0])
else:
Expand All @@ -1524,7 +1529,7 @@ def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True):
continue

if width is not None:
wrapper = _CustomTextWrap(width=width)
wrapper = _CustomTextWrap(width=width, break_long_words=break_long_words, break_on_hyphens=break_on_hyphens)
# Cast based on our internal type handling
# Any future custom formatting of types (such as datetimes)
# may need to be more explicit than just `str` of the object
Expand Down Expand Up @@ -1584,6 +1589,8 @@ def tabulate(
maxcolwidths=None,
rowalign=None,
maxheadercolwidths=None,
break_long_words=_BREAK_LONG_WORDS,
break_on_hyphens=_BREAK_ON_HYPHENS,
):
"""Format a fixed width table for pretty printing.

Expand Down Expand Up @@ -2082,7 +2089,7 @@ def tabulate(

numparses = _expand_numparse(disable_numparse, num_cols)
list_of_lists = _wrap_text_to_colwidths(
list_of_lists, maxcolwidths, numparses=numparses
list_of_lists, maxcolwidths, numparses=numparses, break_long_words=break_long_words, break_on_hyphens=break_on_hyphens
)

if maxheadercolwidths is not None:
Expand All @@ -2096,7 +2103,7 @@ def tabulate(

numparses = _expand_numparse(disable_numparse, num_cols)
headers = _wrap_text_to_colwidths(
[headers], maxheadercolwidths, numparses=numparses
[headers], maxheadercolwidths, numparses=numparses, break_long_words=break_long_words, break_on_hyphens=break_on_hyphens
)[0]

# empty values in the first column of RST tables should be escaped (issue #82)
Expand Down
2 changes: 2 additions & 0 deletions test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def test_tabulate_signature():
("maxcolwidths", None),
("rowalign", None),
("maxheadercolwidths", None),
("break_long_words", True),
("break_on_hyphens", True),
]
_check_signature(tabulate, expected_sig)

Expand Down
29 changes: 29 additions & 0 deletions test/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -2973,3 +2973,32 @@ def test_preserve_whitespace():
expected = "\n".join(["h1 h2 h3", "---- ---- ----", "foo bar foo"])
result = tabulate(test_table, table_headers)
assert_equal(expected, result)

def test_break_long_words():
"Output: Default table output, with breakwords true."
table_headers = ["h1", "h2", "h3"]
test_table = [[" foo1", " bar2 ", "foo3"]]

# Table is not wrapped on 3 letters due to long word
expected = "h1 h2 h3\n---- ---- ----\nfoo1 bar2 foo3"
result = tabulate(test_table, table_headers, maxcolwidths=3, break_long_words=False)
assert_equal(expected, result)

# Table max width is 3 letters
expected = "h1 h2 h3\n---- ---- ----\nf ba foo\noo1 r2 3"
result = tabulate(test_table, table_headers, maxcolwidths=3, break_long_words=True)
assert_equal(expected, result)

def test_break_on_hyphens():
"Output: Default table output, with break on hyphens true."
table_headers = ["h1", "h2", "h3"]
test_table = [[" foo-bar", " bar-bar ", "foo-foo"]]
# Table max width is 5, long lines breaks on hyphens
expected = "h1 h2 h3\n---- ---- -----\nfoo bar- foo-f\n-bar bar oo"
result = tabulate(test_table, table_headers, maxcolwidths=5, break_on_hyphens=False)
assert_equal(expected, result)

# Table data is no longer breaks on hyphens
expected = "h1 h2 h3\n---- ---- ----\nfoo- bar- foo-\nbar bar foo"
result = tabulate(test_table, table_headers, maxcolwidths=5, break_on_hyphens=True)
assert_equal(expected, result)