PATH:
opt
/
imunify360
/
venv
/
lib
/
python3.11
/
site-packages
/
pip
/
_vendor
/
rich
from __future__ import annotations import re from typing import Iterable from ._loop import loop_last from .cells import cell_len, chop_cells re_word = re.compile(r"\s*\S+\s*") def words(text: str) -> Iterable[tuple[int, int, str]]: """Yields each word from the text as a tuple containing (start_index, end_index, word). A "word" in this context may include the actual word and any whitespace to the right. """ position = 0 word_match = re_word.match(text, position) while word_match is not None: start, end = word_match.span() word = word_match.group(0) yield start, end, word word_match = re_word.match(text, end) def divide_line(text: str, width: int, fold: bool = True) -> list[int]: """Given a string of text, and a width (measured in cells), return a list of cell offsets which the string should be split at in order for it to fit within the given width. Args: text: The text to examine. width: The available cell width. fold: If True, words longer than `width` will be folded onto a new line. Returns: A list of indices to break the line at. """ break_positions: list[int] = [] # offsets to insert the breaks at append = break_positions.append cell_offset = 0 _cell_len = cell_len for start, _end, word in words(text): word_length = _cell_len(word.rstrip()) remaining_space = width - cell_offset word_fits_remaining_space = remaining_space >= word_length if word_fits_remaining_space: # Simplest case - the word fits within the remaining width for this line. cell_offset += _cell_len(word) else: # Not enough space remaining for this word on the current line. if word_length > width: # The word doesn't fit on any line, so we can't simply # place it on the next line... if fold: # Fold the word across multiple lines. folded_word = chop_cells(word, width=width) for last, line in loop_last(folded_word): if start: append(start) if last: cell_offset = _cell_len(line) else: start += len(line) else: # Folding isn't allowed, so crop the word. if start: append(start) cell_offset = _cell_len(word) elif cell_offset and start: # The word doesn't fit within the remaining space on the current # line, but it *can* fit on to the next (empty) line. append(start) cell_offset = _cell_len(word) return break_positions if __name__ == "__main__": # pragma: no cover from .console import Console console = Console(width=10) console.print("12345 abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ 12345") print(chop_cells("abcdefghijklmnopqrstuvwxyz", 10)) console = Console(width=20) console.rule() console.print("TextualはPythonの高速アプリケーション開発フレームワークです") console.rule() console.print("アプリケーションは1670万色を使用でき")
[-] jupyter.py
[edit]
[-] errors.py
[edit]
[-] progress_bar.py
[edit]
[-] _log_render.py
[edit]
[-] pretty.py
[edit]
[-] spinner.py
[edit]
[-] _inspect.py
[edit]
[-] _windows_renderer.py
[edit]
[-] segment.py
[edit]
[-] repr.py
[edit]
[-] live_render.py
[edit]
[-] live.py
[edit]
[-] _windows.py
[edit]
[-] panel.py
[edit]
[-] default_styles.py
[edit]
[-] ansi.py
[edit]
[-] _fileno.py
[edit]
[-] highlighter.py
[edit]
[+]
..
[-] constrain.py
[edit]
[-] measure.py
[edit]
[-] _win32_console.py
[edit]
[-] themes.py
[edit]
[-] layout.py
[edit]
[-] color.py
[edit]
[-] _timer.py
[edit]
[-] _palettes.py
[edit]
[-] _emoji_codes.py
[edit]
[-] __main__.py
[edit]
[-] _extension.py
[edit]
[-] emoji.py
[edit]
[-] theme.py
[edit]
[-] _pick.py
[edit]
[-] style.py
[edit]
[-] screen.py
[edit]
[-] syntax.py
[edit]
[-] py.typed
[edit]
[-] abc.py
[edit]
[-] region.py
[edit]
[-] palette.py
[edit]
[-] _export_format.py
[edit]
[-] protocol.py
[edit]
[-] text.py
[edit]
[-] prompt.py
[edit]
[-] status.py
[edit]
[+]
__pycache__
[-] align.py
[edit]
[-] color_triplet.py
[edit]
[-] bar.py
[edit]
[-] control.py
[edit]
[-] styled.py
[edit]
[-] progress.py
[edit]
[-] console.py
[edit]
[-] scope.py
[edit]
[-] traceback.py
[edit]
[-] _null_file.py
[edit]
[-] rule.py
[edit]
[-] logging.py
[edit]
[-] box.py
[edit]
[-] table.py
[edit]
[-] terminal_theme.py
[edit]
[-] _spinners.py
[edit]
[-] filesize.py
[edit]
[-] padding.py
[edit]
[-] __init__.py
[edit]
[-] _wrap.py
[edit]
[-] columns.py
[edit]
[-] _cell_widths.py
[edit]
[-] markup.py
[edit]
[-] containers.py
[edit]
[-] json.py
[edit]
[-] diagnose.py
[edit]
[-] _loop.py
[edit]
[-] _ratio.py
[edit]
[-] _emoji_replace.py
[edit]
[-] pager.py
[edit]
[-] tree.py
[edit]
[-] file_proxy.py
[edit]
[-] _stack.py
[edit]
[-] cells.py
[edit]