Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
_textwrap.py50 linesDownload Raw Back to click
1import textwrap2import typing as t3from contextlib import contextmanager4 5 6class TextWrapper(textwrap.TextWrapper):7    def _handle_long_word(8        self,9        reversed_chunks: t.List[str],10        cur_line: t.List[str],11        cur_len: int,12        width: int,13    ) -> None:14        space_left = max(width - cur_len, 1)15 16        if self.break_long_words:17            last = reversed_chunks[-1]18            cut = last[:space_left]19            res = last[space_left:]20            cur_line.append(cut)21            reversed_chunks[-1] = res22        elif not cur_line:23            cur_line.append(reversed_chunks.pop())24 25    @contextmanager26    def extra_indent(self, indent: str) -> t.Iterator[None]:27        old_initial_indent = self.initial_indent28        old_subsequent_indent = self.subsequent_indent29        self.initial_indent += indent30        self.subsequent_indent += indent31 32        try:33            yield34        finally:35            self.initial_indent = old_initial_indent36            self.subsequent_indent = old_subsequent_indent37 38    def indent_only(self, text: str) -> str:39        rv = []40 41        for idx, line in enumerate(text.splitlines()):42            indent = self.initial_indent43 44            if idx > 0:45                indent = self.subsequent_indent46 47            rv.append(f"{indent}{line}")48 49        return "\n".join(rv)50 
codekingpro/portable-devtools · Team Ai