Skip to content
Myr

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

4 files+54 added−52 removed

modified:click/_compat.py+4 added
481 unmodified lines
482def strip_ansi(value):483 return _ansi_re.sub('', value)484485486def term_len(x):487 return len(strip_ansi(x))488489490def isatty(stream):491 try:492 return stream.isatty()493 except Exception:
36 unmodified lines
modified:click/_termui_impl.py+3 added-3 removed
12 unmodified lines
13import sys14import time15import math16from ._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_encoding18from .utils import echo19from .exceptions import ClickException2021
168 unmodified lines
190 # 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 lines
204 clear_width = self.max_width205206 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 lines
added:click/_textwrap.py+45 added
1import textwrap2from contextlib import contextmanager3from ._compat import term_len456class TextWrapper(textwrap.TextWrapper):78 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, ''1516 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):17 space_left = max(width - cur_len, 1)1819 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())2526 @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_indent3738 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
1import textwrap1from contextlib import contextmanager32from .termui import get_terminal_size5from ._compat import strip_ansi3from ._compat import term_len458def term_len(x):9 return len(strip_ansi(x))10116def measure_table(rows):7 widths = {}8 for row in rows:9 for idx, col in enumerate(row):
6 unmodified lines
16 row = tuple(row)17 yield row + ('',) * (col_count - len(row))181926class TextWrapper(textwrap.TextWrapper):2728 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, ''3536 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):37 space_left = max(width - cur_len, 1)3839 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())4546 @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_indent5758 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)666720def 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 the
12 unmodified lines
36 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