Title: Multiline shortening
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.4
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: barry, ezio.melotti, georg.brandl, pitrou, python-dev, r.david.murray, serhiy.storchaka, steven.daprano, vajrasky
Priority: normal Keywords: needs review, patch

Created on 2013-08-13 10:12 by serhiy.storchaka, last changed 2013-10-16 10:09 by serhiy.storchaka. This issue is now closed.

File name Uploaded Description Edit
textwrap_max_lines.patch serhiy.storchaka, 2013-09-13 07:47 review
textwrap_max_lines_2.patch serhiy.storchaka, 2013-10-06 08:29 review
Messages (12)
msg195065 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-08-13 10:12
Functions in the textwrap module works with multiline text except a newly added (in issue18585) 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.
msg195066 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-08-13 10:15
Sounds like a reasonable enhancement to me.
msg195074 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2013-08-13 13:30
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 />').
msg195075 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-08-13 13:49
fill(...) is just '\n'.join(wrap(...)). Directly use wrap() if you need nonstandard newlines.
msg197557 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-13 07:47
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.
msg198891 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-10-03 12:08
Could anyone please review the patch?
msg199046 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-10-06 08:29
In updated patch fixed a bug with final spaces.
msg200010 - (view) Author: Roundup Robot (python-dev) Date: 2013-10-15 18:24
New changeset 2e8c424dc638 by Serhiy Storchaka in branch 'default':
Issue #18725: The textwrap module now supports truncating multiline text.
msg200011 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-10-15 18:25
Antoine has approved this on IRC.

Thank you Ezio and Antoine for your reviews.
msg200046 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2013-10-16 09:47
Serhiy, you forgot to add shorten to __all__.
msg200049 - (view) Author: Roundup Robot (python-dev) Date: 2013-10-16 10:08
New changeset 0bd257cd3e88 by Serhiy Storchaka in branch 'default':
Add shorten to __all_ (issues #18585 and #18725).
msg200051 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-10-16 10:09
Thank you Vajrasky. It's Antoine forgot. ;)
Date User Action Args
2013-10-16 10:09:30serhiy.storchakasetmessages: + msg200051
2013-10-16 10:08:46python-devsetmessages: + msg200049
2013-10-16 09:47:44vajraskysetmessages: + msg200046
2013-10-15 18:25:55serhiy.storchakasetstatus: open -> closed
resolution: fixed
messages: + msg200011

stage: patch review -> resolved
2013-10-15 18:24:37python-devsetmessages: + msg200010
2013-10-06 08:29:49serhiy.storchakasetfiles: + textwrap_max_lines_2.patch
assignee: serhiy.storchaka
messages: + msg199046
2013-10-03 12:08:33serhiy.storchakasetkeywords: + needs review

messages: + msg198891
2013-09-13 07:47:47serhiy.storchakasetfiles: + textwrap_max_lines.patch
keywords: + patch
messages: + msg197557

stage: test needed -> patch review
2013-08-13 13:49:57serhiy.storchakasetmessages: + msg195075
2013-08-13 13:30:04vajraskysetmessages: + msg195074
2013-08-13 10:38:18serhiy.storchakasetstage: test needed
2013-08-13 10:15:19pitrousetmessages: + msg195066
2013-08-13 10:12:58serhiy.storchakacreate