MCPcopy
hub / github.com/Textualize/rich / split_and_crop_lines

Method split_and_crop_lines

rich/segment.py:307–351  ·  view source on GitHub ↗

Split segments in to lines, and crop lines greater than a given length. Args: segments (Iterable[Segment]): An iterable of segments, probably generated from console.render. length (int): Desired line length. style (Style, optional): Style

(
        cls,
        segments: Iterable["Segment"],
        length: int,
        style: Optional[Style] = None,
        pad: bool = True,
        include_new_lines: bool = True,
    )

Source from the content-addressed store, hash-verified

305
306 @classmethod
307 def split_and_crop_lines(
308 cls,
309 segments: Iterable["Segment"],
310 length: int,
311 style: Optional[Style] = None,
312 pad: bool = True,
313 include_new_lines: bool = True,
314 ) -> Iterable[List["Segment"]]:
315 """Split segments in to lines, and crop lines greater than a given length.
316
317 Args:
318 segments (Iterable[Segment]): An iterable of segments, probably
319 generated from console.render.
320 length (int): Desired line length.
321 style (Style, optional): Style to use for any padding.
322 pad (bool): Enable padding of lines that are less than `length`.
323
324 Returns:
325 Iterable[List[Segment]]: An iterable of lines of segments.
326 """
327 line: List[Segment] = []
328 append = line.append
329
330 adjust_line_length = cls.adjust_line_length
331 new_line_segment = cls("\n")
332
333 for segment in segments:
334 if "\n" in segment.text and not segment.control:
335 text, segment_style, _ = segment
336 while text:
337 _text, new_line, text = text.partition("\n")
338 if _text:
339 append(cls(_text, segment_style))
340 if new_line:
341 cropped_line = adjust_line_length(
342 line, length, style=style, pad=pad
343 )
344 if include_new_lines:
345 cropped_line.append(new_line_segment)
346 yield cropped_line
347 line.clear()
348 else:
349 append(segment)
350 if line:
351 yield adjust_line_length(line, length, style=style, pad=pad)
352
353 @classmethod
354 def adjust_line_length(

Callers 5

render_linesMethod · 0.80
printMethod · 0.80
logMethod · 0.80
export_svgMethod · 0.80

Calls 2

appendMethod · 0.45
clearMethod · 0.45

Tested by 1