rich.segment

class rich.segment.ControlType(value)[source]

Non-printable control codes which typically translate to ANSI codes.

class rich.segment.Segment(text: str = '', style: Optional[rich.style.Style] = None, control: Optional[Sequence[Union[Tuple[rich.segment.ControlType], Tuple[rich.segment.ControlType, int], Tuple[rich.segment.ControlType, int, int]]]] = None)[source]

A piece of text with associated style. Segments are produced by the Console render process and are ultimately converted in to strings to be written to the terminal.

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

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

  • control (Tuple[ControlCode..], optional) – Optional sequence of control codes.

classmethod adjust_line_length(line: List[rich.segment.Segment], length: int, style: Optional[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[rich.segment.Segment], style: Optional[rich.style.Style] = None, post_style: Optional[rich.style.Style] = None)Iterable[rich.segment.Segment][source]

Apply style(s) to an iterable of segments.

Returns an iterable of segments where the style is replaced by style + segment.style + post_style.

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

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

  • post_style (Style, optional) – Style to apply on top of segment style. 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.

property control

Optional sequence of control codes.

classmethod filter_control(segments: Iterable[rich.segment.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[rich.segment.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[rich.segment.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

Check if the segment contains control codes.

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

Make a new line segment.

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

Remove all color from an iterable of segments.

Parameters

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

Yields

Segment – Segments with colorless style.

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

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

Args:

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. new_lines (bool, optional): Padded lines should include “

“. Defaults to False.

Returns:

List[List[Segment]]: New list of lines that fits width x height.

classmethod simplify(segments: Iterable[rich.segment.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 of 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[rich.segment.Segment], length: int, style: Optional[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[rich.segment.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.

Remove all links from an iterable of styles.

Parameters

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

Yields

Segment – Segments with link removed.

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

Remove all styles from an iterable of segments.

Parameters

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

Yields

Segment – Segments with styles replace with None

property style

An optional style.

property text

Raw text.