Skip to content
Closed
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/python/black
rev: 22.3.0
rev: 25.1.0
hooks:
- id: black
args: [--safe]
Expand Down
34 changes: 27 additions & 7 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1588,9 +1588,11 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"):
if headers == "keys":
headers = field_names
rows = [
[getattr(row, f) for f in field_names]
if not _is_separating_line(row)
else row
(
[getattr(row, f) for f in field_names]
if not _is_separating_line(row)
else row
)
for row in rows
]

Expand Down Expand Up @@ -1638,7 +1640,13 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"):
return rows, headers, headers_pad


def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True, break_long_words=_BREAK_LONG_WORDS, break_on_hyphens=_BREAK_ON_HYPHENS):
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 @@ -1655,7 +1663,11 @@ def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True, break_long
continue

if width is not None:
wrapper = _CustomTextWrap(width=width, break_long_words=break_long_words, break_on_hyphens=break_on_hyphens)
wrapper = _CustomTextWrap(
width=width,
break_long_words=break_long_words,
break_on_hyphens=break_on_hyphens,
)
casted_cell = str(cell)
wrapped = [
"\n".join(wrapper.wrap(line))
Expand Down Expand Up @@ -2258,7 +2270,11 @@ def tabulate(

numparses = _expand_numparse(disable_numparse, num_cols)
list_of_lists = _wrap_text_to_colwidths(
list_of_lists, maxcolwidths, numparses=numparses, break_long_words=break_long_words, break_on_hyphens=break_on_hyphens
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 @@ -2272,7 +2288,11 @@ def tabulate(

numparses = _expand_numparse(disable_numparse, num_cols)
headers = _wrap_text_to_colwidths(
[headers], maxheadercolwidths, numparses=numparses, break_long_words=break_long_words, break_on_hyphens=break_on_hyphens
[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
4 changes: 1 addition & 3 deletions test/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""API properties.

"""
"""API properties."""

from tabulate import tabulate, tabulate_formats, simple_separated_format
from common import skip
Expand Down
4 changes: 1 addition & 3 deletions test/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""Command-line interface.

"""
"""Command-line interface."""

import os
import sys
Expand Down
29 changes: 15 additions & 14 deletions test/test_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ def test_wrap_text_wide_chars():
except ImportError:
skip("test_wrap_text_wide_chars is skipped")

rows = [["청자청자청자청자청자", "약간 감싸면 더 잘 보일 수있는 다소 긴 설명입니다"]]
rows = [
["청자청자청자청자청자", "약간 감싸면 더 잘 보일 수있는 다소 긴 설명입니다"]
]
widths = [5, 20]
expected = [
[
Expand Down Expand Up @@ -215,10 +217,8 @@ def test_wrap_text_to_colwidths_single_ansi_colors_full_cell():
when it is at the beginning and end of full cell"""
data = [
[
(
"\033[31mThis is a rather long description that might"
" look better if it is wrapped a bit\033[0m"
)
"\033[31mThis is a rather long description that might"
" look better if it is wrapped a bit\033[0m"
]
]
result = T._wrap_text_to_colwidths(data, [30])
Expand All @@ -244,7 +244,12 @@ def test_wrap_text_to_colwidths_colors_wide_char():
except ImportError:
skip("test_wrap_text_to_colwidths_colors_wide_char is skipped")

data = [[("\033[31m약간 감싸면 더 잘 보일 수있는 다소 긴" " 설명입니다 설명입니다 설명입니다 설명입니다 설명\033[0m")]]
data = [
[
"\033[31m약간 감싸면 더 잘 보일 수있는 다소 긴"
" 설명입니다 설명입니다 설명입니다 설명입니다 설명\033[0m"
]
]
result = T._wrap_text_to_colwidths(data, [30])

expected = [
Expand All @@ -267,10 +272,8 @@ def test_wrap_text_to_colwidths_multi_ansi_colors_full_cell():
(e.g. text and background colors)"""
data = [
[
(
"\033[31m\033[43mThis is a rather long description that"
" might look better if it is wrapped a bit\033[0m"
)
"\033[31m\033[43mThis is a rather long description that"
" might look better if it is wrapped a bit\033[0m"
]
]
result = T._wrap_text_to_colwidths(data, [30])
Expand All @@ -294,10 +297,8 @@ def test_wrap_text_to_colwidths_multi_ansi_colors_in_subset():
when they are around subsets of the cell"""
data = [
[
(
"This is a rather \033[31mlong description\033[0m that"
" might look better \033[93mif it is wrapped\033[0m a bit"
)
"This is a rather \033[31mlong description\033[0m that"
" might look better \033[93mif it is wrapped\033[0m a bit"
]
]
result = T._wrap_text_to_colwidths(data, [30])
Expand Down
2 changes: 2 additions & 0 deletions test/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -3320,6 +3320,7 @@ def test_preserve_whitespace():
result = tabulate(test_table, table_headers, preserve_whitespace=False)
assert_equal(expected, result)


def test_break_long_words():
"Output: Default table output, with breakwords true."
table_headers = ["h1", "h2", "h3"]
Expand All @@ -3335,6 +3336,7 @@ def test_break_long_words():
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"]
Expand Down
Loading