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

Enable automatic word wrap based on desired maximum column width #117

Merged
merged 5 commits into from Mar 4, 2021

Conversation

magelisk
Copy link

@magelisk magelisk commented Mar 3, 2021

This address as submitted issue #90 that allows automatically word wrapping long text string. From an API standpoint, this is automatically enabled by simply specifying the maximum column width for a given column. I selected max column width instead of just column width to remove any confusion where a user might think that specifying colwidth might be setting a guaranteed size, which is not the behavior here (though I could see it as a possible future update?)

The "magic" here is a customized version of the textwrap.TextWrapper class to not use just len for strings and to also preserve ANSI color codes around wrapped lines. This is a non-trivial bit of code (even though heavily copy/pasted) with possible regression concerns, so I put it into a new test file to help tack this closely. Hopefully this fits within this projects idioms.

To address the 'tests that need to be covered' from the issue discussion:

  • colwidths parameter can be specified as a number (the same for all column)
    • Done
  • colwidths parameter can be shorter than the number of column (don't wrap text in the remaining columns)
    • Done
  • the feature works when all columns are text
    • Done
  • the feature works when some columns are numeric
    • Done - I think. I think it behaves as sensibly as it can (ignores numbers), and it also works with disable_numparse. Let me know if you had something else in mind,
  • the feature works when some columns contain wide characters (Japanese/Chinese/Korean)
    • Done
  • the feature works when some columns contain invisible control characters (ASCII escape codes, color, hyperlinks)
    • Done
  • the feature works reasonably well or is disabled in formats which cannot support it (HTML? LaTeX?)
    • This has to be explicitly enabled by setting maxcolwidth, so I believe this meets the ability to ensure it does not impact these formats
  • when bidirectional script support is implemented first, this feature should work correctly when there's mixed left-to-right and right-to-left text in a cell (English + Arabic/Hebrew/...)
    • N/A

Here's a small sample program I used to quickly demonstrate all the big capability updates

from tabulate import tabulate
all_data = [
    [('1', 'John Smith', '\033[31mThis is a rather long description that might look better if it is wrapped a bit\033[0m')],
    [('1', 'John Smith', '\033[31m약간 감싸면 더 잘 보일 수있는 다소 긴 설명입니다 설명입니다 설명입니다 설명입니다 설명\033[0m')],
    [('1', 'John Smith', '\033[31m\033[43mThis is a rather long description that might look better if it is wrapped a bit\033[0m')],
    [('1', 'John Smith', 'This is a rather \033[31mlong description\033[0m that might look better \033[93mif it is wrapped\033[0m a bit')]
]
for data in all_data:
    headers=("Issue Id", "Author", "Description")
    print(tabulate(data,  headers, tablefmt="grid", maxcolwidths=[None, None, 30]))

@astanin
Copy link
Owner

astanin commented Mar 4, 2021

The pull request looks good, nothing that catches my eye. I'm merging it. Thank you, Matt.

@astanin astanin merged commit b2c26bc into astanin:master Mar 4, 2021
@tejasjadhav
Copy link

Any idea which version will this change get published in?

@astanin
Copy link
Owner

astanin commented Mar 22, 2021

@tejasjadhav Next version (0.8.10). There is not much else to release yet. You may install the git version if you want.

@tejasjadhav
Copy link

@astanin understood 👍 I'll use the Git version till then.

@csp197
Copy link

csp197 commented Apr 5, 2021

@astanin - I know its already been asked, but any chance that this PR can get pushed out soon?

@nickelser
Copy link

nickelser commented Apr 29, 2021

getting an error using the git version & maxcolwidths with datetime objects:

    print(tabulate.tabulate(result.table, headers="keys", tablefmt="psql", maxcolwidths=80))
  File "<...>/lib/python3.9/site-packages/tabulate.py", line 1592, in tabulate
    list_of_lists = _wrap_text_to_colwidths(
  File "<...>/python3.9/site-packages/tabulate.py", line 1233, in _wrap_text_to_colwidths
    wrapped = wrapper.wrap(cell)
  File "/usr/local/Cellar/python@3.9/3.9.1_8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/textwrap.py", line 351, in wrap
    chunks = self._split_chunks(text)
  File "/usr/local/Cellar/python@3.9/3.9.1_8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/textwrap.py", line 337, in _split_chunks
    text = self._munge_whitespace(text)
  File "/usr/local/Cellar/python@3.9/3.9.1_8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/textwrap.py", line 154, in _munge_whitespace
    text = text.expandtabs(self.tabsize)
AttributeError: 'datetime.timedelta' object has no attribute 'expandtabs'

i think text needs a str() or similar.

@Barathwaja
Copy link

Hi @astanin ,
Is there an ETA when this feature will be released?

Thanks.

@AtomicNess123
Copy link

Has this been released?

@krpatter-intc
Copy link
Contributor

Tried this out from source, and I noticed the header does not honor the maxcolwidths...is there any thoughts to perhaps supporting that capability as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants