Moved textwrap functions into a delayed import to speed up execution time if help is not needed.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 830ab09 · Browse files · 4 files·+54 added−52 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+54 added−52 removed
- other:+54 added−52 removed
Provenance
- Commit
da0a164- Tree
8c9b431- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
830ab09
4 files changed+54 added−52 removed
modified:click/_compat.py+4 added
481 unmodified lines482 def strip_ansi(value):483 return _ansi_re.sub('', value)484 485 486+def term_len(x):487+ return len(strip_ansi(x))488+489+490 def isatty(stream):491 try:492 return stream.isatty()493 except Exception:36 unmodified linesmodified:click/_termui_impl.py+3 added-3 removed
12 unmodified lines13 import sys14 import time15 import math16 from ._compat import _default_text_stdout, range_type, PY2, isatty, \17- open_stream, strip_ansi, get_best_encoding17+ open_stream, strip_ansi, term_len, get_best_encoding18 from .utils import echo19 from .exceptions import ClickException20 21 168 unmodified lines190 # Update width in case the terminal has been resized191 if self.autowidth:192 old_width = self.width193 self.width = 0194- clutter_length = len(strip_ansi(self.format_progress_line()))194+ clutter_length = term_len(self.format_progress_line())195 new_width = max(0, get_terminal_size()[0] - clutter_length)196 if new_width < old_width:197 self.file.write(BEFORE_BAR)198 self.file.write(' ' * self.max_width)5 unmodified lines204 clear_width = self.max_width205 206 self.file.write(BEFORE_BAR)207 line = self.format_progress_line()208- line_len = len(strip_ansi(line))208+ line_len = term_len(line)209 if self.max_width is None or self.max_width < line_len:210 self.max_width = line_len211 # Use echo here so that we get colorama support.212 echo(line, file=self.file, nl=False)275 unmodified linesadded:click/_textwrap.py+45 added
1+import textwrap2+from contextlib import contextmanager3+from ._compat import term_len4+5+6+class TextWrapper(textwrap.TextWrapper):7+8+ def _cutdown(self, ucstr, space_left):9+ l = 010+ for i in xrange(len(ucstr)):11+ l += term_len(ucstr[i])12+ if space_left < l:13+ return (ucstr[:i], ucstr[i:])14+ return ucstr, ''15+16+ def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):17+ space_left = max(width - cur_len, 1)18+19+ if self.break_long_words:20+ cut, res = self._cutdown(reversed_chunks[-1], space_left)21+ cur_line.append(cut)22+ reversed_chunks[-1] = res23+ elif not cur_line:24+ cur_line.append(reversed_chunks.pop())25+26+ @contextmanager27+ def extra_indent(self, indent):28+ old_initial_indent = self.initial_indent29+ old_subsequent_indent = self.subsequent_indent30+ self.initial_indent += indent31+ self.subsequent_indent += indent32+ try:33+ yield34+ finally:35+ self.initial_indent = old_initial_indent36+ self.subsequent_indent = old_subsequent_indent37+38+ def indent_only(self, text):39+ rv = []40+ for idx, line in enumerate(text.splitlines()):41+ indent = self.initial_indent42+ if idx > 0:43+ indent = self.subsequent_indent44+ rv.append(indent + line)45+ return '\n'.join(rv)modified:click/formatting.py+2 added-49 removed
1-import textwrap1 from contextlib import contextmanager3-2 from .termui import get_terminal_size5-from ._compat import strip_ansi3+from ._compat import term_len4 5 8-def term_len(x):9- return len(strip_ansi(x))10-11-6 def measure_table(rows):7 widths = {}8 for row in rows:9 for idx, col in enumerate(row):6 unmodified lines16 row = tuple(row)17 yield row + ('',) * (col_count - len(row))18 19 26-class TextWrapper(textwrap.TextWrapper):27-28- def _cutdown(self, ucstr, space_left):29- l = 030- for i in xrange(len(ucstr)):31- l += term_len(ucstr[i])32- if space_left < l:33- return (ucstr[:i], ucstr[i:])34- return ucstr, ''35-36- def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):37- space_left = max(width - cur_len, 1)38-39- if self.break_long_words:40- cut, res = self._cutdown(reversed_chunks[-1], space_left)41- cur_line.append(cut)42- reversed_chunks[-1] = res43- elif not cur_line:44- cur_line.append(reversed_chunks.pop())45-46- @contextmanager47- def extra_indent(self, indent):48- old_initial_indent = self.initial_indent49- old_subsequent_indent = self.subsequent_indent50- self.initial_indent += indent51- self.subsequent_indent += indent52- try:53- yield54- finally:55- self.initial_indent = old_initial_indent56- self.subsequent_indent = old_subsequent_indent57-58- def indent_only(self, text):59- rv = []60- for idx, line in enumerate(text.splitlines()):61- indent = self.initial_indent62- if idx > 0:63- indent = self.subsequent_indent64- rv.append(indent + line)65- return '\n'.join(rv)66-67-20 def wrap_text(text, width=78, initial_indent='', subsequent_indent='',21 preserve_paragraphs=False):22 """A helper function that intelligently wraps text. By default it23 assumes that it operates on a single paragraph of text but if the12 unmodified lines36 each consecutive line.37 :param preserve_paragraphs: if this flag is set then the wrapping will38 intelligently handle paragraphs.39 """40+ from ._textwrap import TextWrapper41 text = text.expandtabs()42 wrapper = TextWrapper(width, initial_indent=initial_indent,43 subsequent_indent=subsequent_indent,44 replace_whitespace=False)174 unmodified lines