You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Functions in the textwrap module works with multiline text except a newly added (in bpo-18585) the shorten() function. Wrapping and shortening a multiline text using existing textwrap function is not a trivial job.
I propose to add two new parameters to the TextWrap class and wrap() and fill() functions: max_lines and placeholder. If the max_lines argument is specified then wrapped text truncated to max_lines and the last line shortened to the width argument.
>>print(textwrap('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', width=40, max_lines=3))
Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore (...)
The shorten() function then will be just a particular case of fill() with max_lines=1.
Here is a patch. It get rid of TextWrap.shorten() because TextWrap.fill() supersedes it and because "placeholder" now a parameter of TextWrap. Module level shorten() is left but I doubt about it.
Activity
serhiy-storchaka commentedon Aug 13, 2013
Functions in the textwrap module works with multiline text except a newly added (in bpo-18585) the shorten() function. Wrapping and shortening a multiline text using existing textwrap function is not a trivial job.
I propose to add two new parameters to the TextWrap class and wrap() and fill() functions: max_lines and placeholder. If the max_lines argument is specified then wrapped text truncated to max_lines and the last line shortened to the width argument.
The shorten() function then will be just a particular case of fill() with max_lines=1.
pitrou commentedon Aug 13, 2013
Sounds like a reasonable enhancement to me.
vajrasky commentedon Aug 13, 2013
What about newline keyword argument?
Are we forcing the newline to be '\n'? Alternate newlines will be useful for Windows platform ('\r\n') and HTML platform ('<br />').
serhiy-storchaka commentedon Aug 13, 2013
fill(...) is just '\n'.join(wrap(...)). Directly use wrap() if you need nonstandard newlines.
serhiy-storchaka commentedon Sep 13, 2013
Here is a patch. It get rid of TextWrap.shorten() because TextWrap.fill() supersedes it and because "placeholder" now a parameter of TextWrap. Module level shorten() is left but I doubt about it.
serhiy-storchaka commentedon Oct 3, 2013
Could anyone please review the patch?
serhiy-storchaka commentedon Oct 6, 2013
In updated patch fixed a bug with final spaces.
python-dev commentedon Oct 15, 2013
New changeset 2e8c424dc638 by Serhiy Storchaka in branch 'default':
Issue bpo-18725: The textwrap module now supports truncating multiline text.
http://hg.python.org/cpython/rev/2e8c424dc638
serhiy-storchaka commentedon Oct 15, 2013
Antoine has approved this on IRC.
Thank you Ezio and Antoine for your reviews.
vajrasky commentedon Oct 16, 2013
Serhiy, you forgot to add shorten to __all__.
python-dev commentedon Oct 16, 2013
New changeset 0bd257cd3e88 by Serhiy Storchaka in branch 'default':
Add shorten to __all_ (issues bpo-18585 and bpo-18725).
http://hg.python.org/cpython/rev/0bd257cd3e88
serhiy-storchaka commentedon Oct 16, 2013
Thank you Vajrasky. It's Antoine forgot. ;)