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

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

class rich.segment.Segment(text: str = '', style: Optional[] = 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.

  • 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[] = None, pad: bool = True) List[rich.segment.Segment][source]

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

  • 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.


A line of segments with the desired length.

Return type


classmethod apply_style(segments: Iterable[rich.segment.Segment], style: Optional[] = None, post_style: Optional[] = 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 + + post_style.

  • 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.


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

Return type


property cell_length: int

Get cell length of segment.

property control

Optional sequence of control codes.

classmethod divide(segments: Iterable[rich.segment.Segment], cuts: Iterable[int]) Iterable[List[rich.segment.Segment]][source]

Divides an iterable of segments in to portions.


cuts (Iterable[int]) – Cell positions where to divide.


[Iterable[List[Segment]]] – An iterable of Segments in List.

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

Filter segments by is_control attribute.

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

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


And iterable of Segment instances.

Return type


classmethod get_line_length(line: List[rich.segment.Segment]) int[source]

Get the length of list of segments.


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


The length of the line.

Return type


classmethod get_shape(lines: List[List[rich.segment.Segment]]) Tuple[int, int][source]

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


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


Width and height in characters.

Return type

Tuple[int, int]

property is_control: bool

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.


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


Segment – Segments with colorless style.

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

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


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.


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.


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


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

Return type


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

  • 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.


An iterable of lines of segments.

Return type


split_cells(cut: int) Tuple[rich.segment.Segment, rich.segment.Segment][source]

Split segment in to two segments at the specified column.

If the cut point falls in the middle of a 2-cell wide character then it is replaced by two spaces, to preserve the display width of the parent segment.


Two segments.

Return type

Tuple[Segment, 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.


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


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

Remove all links from an iterable of styles.


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


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.


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


Segment – Segments with styles replace with None

property style

An optional style.

property text

Raw text.

class rich.segment.Segments(segments: Iterable[rich.segment.Segment], new_lines: bool = False)[source]

A simple renderable to render an iterable of segments. This class may be useful if you want to print segments outside of a __rich_console__ method.

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

  • new_lines (bool, optional) – Add new lines between segments. Defaults to False.