rich.segment

class rich.segment.Segment(text: str = '', style: Optional[rich.style.Style] = None, is_control: bool = False)[source]

A piece of text with associated style.

Parameters
  • text (str) – A piece of text.

  • style (Style, optional) – An optional style to apply to the text.

  • is_control (bool, optional) – Boolean that marks segment as containing non-printable control codes.

classmethod adjust_line_length(line: List[Segment], length: int, style: rich.style.Style = None, pad: bool = True) → List[rich.segment.Segment][source]

Adjust a line to a given width (cropping or padding as required).

Parameters
  • segments (Iterable[Segment]) – A list of segments in a single line.

  • length (int) – The desired width of the line.

  • style (Style, optional) – The style of padding if used (space on the end). Defaults to None.

  • pad (bool, optional) – Pad lines with spaces if they are shorter than length. Defaults to True.

Returns

A line of segments with the desired length.

Return type

List[Segment]

classmethod apply_style(segments: Iterable[Segment], style: rich.style.Style = None) → Iterable[rich.segment.Segment][source]

Apply a style to an iterable of segments.

Parameters
  • segments (Iterable[Segment]) – Segments to process.

  • style (Style, optional) – A style to apply. Defaults to None.

Returns

A new iterable of segments (possibly the same iterable).

Return type

Iterable[Segments]

property cell_length

Get cell length of segment.

classmethod control(text: str)rich.segment.Segment[source]

Create a Segment with control codes.

Parameters

text (str) – Text containing non-printable control codes.

Returns

A Segment instance with is_control=True.

Return type

Segment

classmethod filter_control(segments: Iterable[Segment], is_control=False) → Iterable[rich.segment.Segment][source]

Filter segments by is_control attribute.

Parameters
  • segments (Iterable[Segment]) – An iterable of Segment instances.

  • is_control (bool, optional) – is_control flag to match in search.

Returns

And iterable of Segment instances.

Return type

Iterable[Segment]

classmethod get_line_length(line: List[Segment]) → int[source]

Get the length of list of segments.

Parameters

line (List[Segment]) – A line encoded as a list of Segments (assumes no ‘n’ characters),

Returns

The length of the line.

Return type

int

classmethod get_shape(lines: List[List[Segment]]) → Tuple[int, int][source]

Get the shape (enclosing rectangle) of a list of lines.

Parameters

lines (List[List[Segment]]) – A list of lines (no ‘n’ characters).

Returns

Width and height in characters.

Return type

Tuple[int, int]

property is_control

True if the segment contains control codes, otherwise False.

classmethod line()rich.segment.Segment[source]

Make a new line segment.

classmethod set_shape(lines: List[List[Segment]], width: int, height: int = None, style: rich.style.Style = None) → List[List[rich.segment.Segment]][source]

Set the shape of a list of lines (enclosing rectangle).

Parameters
  • lines (List[List[Segment]]) – A list of lines.

  • width (int) – Desired width.

  • height (int, optional) – Desired height or None for no change.

  • style (Style, optional) – Style of any padding added. Defaults to None.

Returns

New list of lines that fits width x height.

Return type

List[List[Segment]]

classmethod simplify(segments: Iterable[Segment]) → Iterable[rich.segment.Segment][source]

Simplify an iterable of segments by combining contiguous segments with the same style.

Parameters

segments (Iterable[Segment]) – An iterable segments.

Returns

A possibly smaller iterable of segments that will render the same way.

Return type

Iterable[Segment]

classmethod split_and_crop_lines(segments: Iterable[Segment], length: int, style: rich.style.Style = None, pad: bool = True, include_new_lines: bool = True) → Iterable[List[rich.segment.Segment]][source]

Split segments in to lines, and crop lines greater than a given length.

Parameters
  • segments (Iterable[Segment]) – An iterable of segments, probably generated from console.render.

  • length (int) – Desired line length.

  • style (Style, optional) – Style to use for any padding.

  • pad (bool) – Enable padding of lines that are less than length.

Returns

An iterable of lines of segments.

Return type

Iterable[List[Segment]]

classmethod split_lines(segments: Iterable[Segment]) → Iterable[List[rich.segment.Segment]][source]

Split a sequence of segments in to a list of lines.

Parameters

segments (Iterable[Segment]) – Segments potentially containing line feeds.

Yields

Iterable[List[Segment]] – Iterable of segment lists, one per line.

property style

An optional style.

property text

Raw text.