Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-56708: support custom len function in textwrap.wrap#28136
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
Open
xi wants to merge 12 commits into python:mainChoose a base branch from xi:textwrap-len
base:main
Could not load branches
Branch not found: {{refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading. Please reload this page.
Open
Changes from all commits
Commits
Show all changes
12 commits Select commit Hold shift + click to select a range
df3f367 bpo-12499: textwrap.wrap: add control for fonts with different charac…
xi 97a2ec8 react to feedback
xi db82b6c typo
xi f457e20 fix missing len occurences
xi e164780 add more tests
xi 09ce4ce fix idle test
xi bf8dad5 optimize text_len function
xi 68e8098 fixup
xi e5d6d88 _find_width_index and _handle_long_word change
tiptenbrink 7b32d0b Apply changes, ensure min 1 char on line
tiptenbrink b811049 Merge pull request #2 from tiptenbrink/textwrap-len
xi 95fcafd Merge branch 'main' into textwrap-len
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -122,7 +122,8 @@ def __init__(self, | ||
| tabsize=8, | ||
| *, | ||
| max_lines=None, | ||
| placeholder=' [...]'): | ||
| placeholder=' [...]', | ||
| text_len=len): | ||
merwok marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| self.width = width | ||
| self.initial_indent = initial_indent | ||
| self.subsequent_indent = subsequent_indent | ||
| @@ -135,6 +136,7 @@ def __init__(self, | ||
| self.tabsize = tabsize | ||
| self.max_lines = max_lines | ||
| self.placeholder = placeholder | ||
| self.text_len = text_len | ||
| # -- Private methods ----------------------------------------------- | ||
| @@ -194,6 +196,25 @@ def _fix_sentence_endings(self, chunks): | ||
| else: | ||
| i += 1 | ||
| def _find_width_index(self, text, width): | ||
| """_find_length_index(text : string, width : int) | ||
| Find at which index the text has the required width, since when using a | ||
| different text_len, this index will not be equal to the required width. | ||
| """ | ||
| # When using default len as self.text_len, the required index and width | ||
| # will be equal, this prevents calculation time. | ||
| if self.text_len(text[:width]) == width: | ||
| # For character widths greater than one, width can be more than the | ||
| # number of characters | ||
| return min(width, len(text)) | ||
| cur_text = '' | ||
| for i, c in enumerate(text): | ||
| cur_text += c | ||
| cur_width = self.text_len(cur_text) | ||
| if cur_width > width: | ||
| return max(i, 1) | ||
| def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): | ||
| """_handle_long_word(chunks : [string], | ||
| cur_line : [string], | ||
| @@ -214,10 +235,11 @@ def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): | ||
| if self.break_long_words and space_left > 0: | ||
| end = space_left | ||
| chunk = reversed_chunks[-1] | ||
| if self.break_on_hyphens and len(chunk) > space_left: | ||
| end = self._find_width_index(chunk, space_left) | ||
| if self.break_on_hyphens and self.text_len(chunk) > space_left: | ||
| # break after last hyphen, but only if there are | ||
| # non-hyphens before it | ||
| hyphen = chunk.rfind('-', 0, space_left) | ||
| hyphen = chunk.rfind('-', 0, end) | ||
| if hyphen > 0 and any(c != '-' for c in chunk[:hyphen]): | ||
| end = hyphen + 1 | ||
| cur_line.append(chunk[:end]) | ||
| @@ -256,7 +278,8 @@ def _wrap_chunks(self, chunks): | ||
| indent = self.subsequent_indent | ||
| else: | ||
| indent = self.initial_indent | ||
| if len(indent) + len(self.placeholder.lstrip()) > self.width: | ||
| if (self.text_len(indent) + | ||
| self.text_len(self.placeholder.lstrip()) > self.width): | ||
| raise ValueError("placeholder too large for max width") | ||
| # Arrange in reverse order so items can be efficiently popped | ||
| @@ -277,15 +300,15 @@ def _wrap_chunks(self, chunks): | ||
| indent = self.initial_indent | ||
| # Maximum width for this line. | ||
| width = self.width - len(indent) | ||
| width = self.width - self.text_len(indent) | ||
| # First chunk on line is whitespace -- drop it, unless this | ||
| # is the very beginning of the text (ie. no lines started yet). | ||
| if self.drop_whitespace and chunks[-1].strip() == '' and lines: | ||
| del chunks[-1] | ||
| while chunks: | ||
| l = len(chunks[-1]) | ||
| l = self.text_len(chunks[-1]) | ||
| # Can at least squeeze this chunk onto the current line. | ||
| if cur_len + l <= width: | ||
| @@ -298,13 +321,13 @@ def _wrap_chunks(self, chunks): | ||
| # The current line is full, and the next chunk is too big to | ||
| # fit on *any* line (not just this one). | ||
| if chunks and len(chunks[-1]) > width: | ||
| if chunks and self.text_len(chunks[-1]) > width: | ||
| self._handle_long_word(chunks, cur_line, cur_len, width) | ||
| cur_len = sum(map(len, cur_line)) | ||
| cur_len = sum(map(self.text_len, cur_line)) | ||
| # If the last chunk on this line is all whitespace, drop it. | ||
| if self.drop_whitespace and cur_line and cur_line[-1].strip() == '': | ||
| cur_len -= len(cur_line[-1]) | ||
| cur_len -= self.text_len(cur_line[-1]) | ||
| del cur_line[-1] | ||
| if cur_line: | ||
| @@ -320,16 +343,17 @@ def _wrap_chunks(self, chunks): | ||
| else: | ||
| while cur_line: | ||
| if (cur_line[-1].strip() and | ||
| cur_len + len(self.placeholder) <= width): | ||
| cur_len + self.text_len(self.placeholder) <= width): | ||
| cur_line.append(self.placeholder) | ||
| lines.append(indent + ''.join(cur_line)) | ||
| break | ||
| cur_len -= len(cur_line[-1]) | ||
| cur_len -= self.text_len(cur_line[-1]) | ||
| del cur_line[-1] | ||
| else: | ||
| if lines: | ||
| prev_line = lines[-1].rstrip() | ||
| if (len(prev_line) + len(self.placeholder) <= | ||
| if (self.text_len(prev_line) + | ||
| self.text_len(self.placeholder) <= | ||
| self.width): | ||
| lines[-1] = prev_line + self.placeholder | ||
| break | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.