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. 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.is_control (bool, optional) – Boolean that marks segment as containing non-printable 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
- 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, style: Optional[rich.style.Style] = None) → rich.segment.Segment[source]¶ Create a Segment with control codes.
- Parameters
text (str) – Text containing non-printable control codes.
style (Optional[style]) – Optional style.
- Returns
A Segment instance with
is_control=True
.- Return type
-
classmethod
filter_control
(segments: Iterable[rich.segment.Segment], is_control=False) → Iterable[rich.segment.Segment][source]¶ Filter segments by
is_control
attribute.
-
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
¶ True if the segment contains control codes, otherwise False.
-
classmethod
line
(is_control: bool = False) → rich.segment.Segment[source]¶ Make a new line segment.
-
classmethod
make_control
(segments: Iterable[rich.segment.Segment]) → Iterable[rich.segment.Segment][source]¶ Convert all segments in to control segments.
- Returns
Segments with is_control=True
- Return type
Iterable[Segments]
-
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.
-
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
- 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.
-
classmethod
strip_links
(segments: Iterable[rich.segment.Segment]) → Iterable[rich.segment.Segment][source]¶ 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.