MCPcopy
hub / github.com/pallets/click / _wrap_chunks

Method _wrap_chunks

src/click/_textwrap.py:66–162  ·  src/click/_textwrap.py::TextWrapper._wrap_chunks

Wrap chunks counting widths in visible characters. Mirrors the algorithm of :meth:`textwrap.TextWrapper._wrap_chunks` with every width measurement routed through :func:`click._compat.term_len` instead of :func:`len`, so ANSI escape bytes in chunks, indents, or the pl

(self, chunks: list[str])

Source from the content-addressed store, hash-verified

64 cur_line.append(reversed_chunks.pop())
65
66 def _wrap_chunks(self, chunks: list[str]) -> list[str]:
67 class="st">"""Wrap chunks counting widths in visible characters.
68
69 Mirrors the algorithm of :meth:`textwrap.TextWrapper._wrap_chunks`
70 with every width measurement routed through
71 :func:`click._compat.term_len` instead of :func:`len`, so ANSI escape
72 bytes in chunks, indents, or the placeholder do not inflate the count.
73
74 .. seealso::
75 :class:`textwrap.TextWrapper` in the Python standard library documentation:
76 https://docs.python.org/3/library/textwrap.htmlclass="cm">#textwrap.TextWrapper
77
78 Reference implementation in CPython:
79 https://github.com/python/cpython/blob/main/Lib/textwrap.py
80 class="st">"""
81 lines: list[str] = []
82 if self.width <= 0:
83 raise ValueError(fclass="st">"invalid width {self.width!r} (must be > 0)")
84 if self.max_lines is not None:
85 if self.max_lines > 1:
86 indent = self.subsequent_indent
87 else:
88 indent = self.initial_indent
89 if term_len(indent) + term_len(self.placeholder.lstrip()) > self.width:
90 raise ValueError(class="st">"placeholder too large for max width")
91
92 chunks.reverse()
93
94 while chunks:
95 cur_line: list[str] = []
96 cur_len = 0
97
98 if lines:
99 indent = self.subsequent_indent
100 else:
101 indent = self.initial_indent
102
103 width = self.width - term_len(indent)
104
105 if self.drop_whitespace and chunks[-1].strip() == class="st">"" and lines:
106 del chunks[-1]
107
108 while chunks:
109 n = term_len(chunks[-1])
110
111 if cur_len + n <= width:
112 cur_line.append(chunks.pop())
113 cur_len += n
114
115 else:
116 break
117
118 if chunks and term_len(chunks[-1]) > width:
119 self._handle_long_word(chunks, cur_line, cur_len, width)
120 cur_len = sum(map(term_len, cur_line))
121
122 if self.drop_whitespace and cur_line and cur_line[-1].strip() == class="st">"":
123 cur_len -= term_len(cur_line[-1])

Callers

nothing calls this directly

Calls 2

_handle_long_wordMethod · 0.95
term_lenFunction · 0.85

Tested by

no test coverage detected