Skip to main content

Segment

Struct Segment 

Source
pub struct Segment {
    pub text: Cow<'static, str>,
    pub style: Option<Style>,
    pub meta: Option<StyleMeta>,
    pub control: Option<ControlType>,
}
Expand description

A segment of text with optional style and control codes.

This is the fundamental unit of output in Rich. All renderables produce sequences of Segments.

Uses Cow<'static, str> for text to allow both owned and static strings without lifetime complexity in the API. This is a deliberate tradeoff favoring API simplicity over zero-copy for borrowed non-static input.

Fields§

§text: Cow<'static, str>

The text content.

§style: Option<Style>

Optional style to apply.

§meta: Option<StyleMeta>

Optional style metadata (hyperlinks, Textual handlers, etc.).

§control: Option<ControlType>

Optional control code (if set, text is typically empty).

Implementations§

Source§

impl Segment

Source

pub fn new(text: impl Into<Cow<'static, str>>) -> Self

Create a new text segment.

Source

pub fn styled(text: impl Into<Cow<'static, str>>, style: Style) -> Self

Create a new styled segment.

Source

pub fn styled_with_meta( text: impl Into<Cow<'static, str>>, style: Style, meta: StyleMeta, ) -> Self

Create a new styled segment with metadata.

Source

pub fn new_with_meta( text: impl Into<Cow<'static, str>>, meta: StyleMeta, ) -> Self

Create a new segment with metadata and no style.

Source

pub fn control(control: ControlType) -> Self

Create a control segment.

Source

pub fn line() -> Self

Create a newline segment.

Source

pub fn is_control(&self) -> bool

Check if this segment is a control segment.

Source

pub fn cell_len(&self) -> usize

Get the cell width of this segment’s text.

Source

pub fn apply_style(&self, style: &Style) -> Self

Apply a style to this segment, combining with any existing style.

Source

pub fn split_cells(&self, cut: usize) -> (Segment, Segment)

Split segment into two segments at the specified cell position.

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.

§Arguments
  • cut - Cell offset within the segment to cut at.
§Returns

A tuple of two segments: (before, after).

§Example
use rich_rs::Segment;

let seg = Segment::new("hello");
let (before, after) = seg.split_cells(3);
assert_eq!(&*before.text, "hel");
assert_eq!(&*after.text, "lo");
Source

pub fn split_lines( segments: impl IntoIterator<Item = Segment>, ) -> Vec<Vec<Segment>>

Split a sequence of segments into lines on newline characters.

§Arguments
  • segments - Segments potentially containing newlines.
§Returns

A vector of lines, where each line is a vector of segments.

Source

pub fn split_and_crop_lines( segments: impl IntoIterator<Item = Segment>, length: usize, style: Option<Style>, pad: bool, include_new_lines: bool, ) -> Vec<Vec<Segment>>

Split segments into lines and crop/pad each line to a specific length.

§Arguments
  • segments - An iterable of segments to process.
  • length - Desired line length in cells.
  • style - Style to use for padding.
  • pad - Whether to pad lines shorter than length.
  • include_new_lines - Whether to append newline segments to each line.
§Returns

A vector of lines, each cropped/padded to the desired length.

Source

pub fn adjust_line_length( line: &[Segment], length: usize, style: Option<Style>, pad: bool, ) -> Vec<Segment>

Adjust a line to a given width by cropping or padding.

§Arguments
  • line - A slice of segments representing a single line.
  • length - Desired width in cells.
  • style - Style to use for padding.
  • pad - Whether to pad lines shorter than length.
§Returns

A new vector of segments with the desired length.

Source

pub fn get_last_style(line: &[Segment]) -> Option<Style>

Get the last non-control style in a line.

This is useful for determining the “end of line” style when padding with spaces, so background colors extend to the full width.

Source

pub fn simplify(segments: impl IntoIterator<Item = Segment>) -> Segments

Simplify segments by merging adjacent segments with the same style.

§Arguments
  • segments - An iterable of segments to simplify.
§Returns

A Segments collection with adjacent same-style segments merged.

Source

pub fn divide( segments: impl IntoIterator<Item = Segment>, cuts: &[usize], ) -> Vec<Vec<Segment>>

Divide segments at multiple cell positions.

§Arguments
  • segments - Segments to divide.
  • cuts - Cell positions where to divide (must be sorted in ascending order).
§Returns

A vector of segment vectors, one for each division. Always includes a trailing partition containing any remaining content after the last cut.

§Panics (debug mode only)

Debug-asserts that cuts are sorted in ascending order.

Source

pub fn apply_style_to_segments( segments: impl IntoIterator<Item = Segment>, style: Option<Style>, post_style: Option<Style>, ) -> Segments

Apply style to all segments.

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

§Arguments
  • segments - Segments to process.
  • style - Base style to apply first.
  • post_style - Style to apply after segment’s own style.
§Returns

A new Segments collection with styles applied.

Source

pub fn filter_control( segments: impl IntoIterator<Item = Segment>, is_control: bool, ) -> Segments

Filter segments by control status.

§Arguments
  • segments - Segments to filter.
  • is_control - If true, keep only control segments; if false, keep only non-control segments.
§Returns

A new Segments collection with only matching segments.

Source

pub fn strip_styles(segments: impl IntoIterator<Item = Segment>) -> Segments

Remove all styles from segments.

§Arguments
  • segments - Segments to process.
§Returns

A new Segments collection with all styles removed.

Source

pub fn get_line_length(line: &[Segment]) -> usize

Get the total cell width of a line of segments.

§Arguments
  • line - A slice of segments representing a single line (no newlines).
§Returns

The total cell width of all non-control segments.

Source

pub fn get_shape(lines: &[Vec<Segment>]) -> (usize, usize)

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

§Arguments
  • lines - A list of lines (no newline characters).
§Returns

A tuple of (width, height) representing the enclosing rectangle.

Source

pub fn set_shape( lines: &[Vec<Segment>], width: usize, height: Option<usize>, style: Option<Style>, new_lines: bool, ) -> Vec<Vec<Segment>>

Set the shape of a list of lines to a specific rectangle.

§Arguments
  • lines - A list of lines.
  • width - Desired width.
  • height - Desired height (if None, uses current height).
  • style - Style for padding.
  • new_lines - Whether padded lines should include newline characters.
§Returns

A new list of lines with the specified shape.

Source

pub fn align_top( lines: &[Vec<Segment>], width: usize, height: usize, style: Option<Style>, new_lines: bool, ) -> Vec<Vec<Segment>>

Align lines to the top by padding the bottom with blank lines.

§Arguments
  • lines - A list of lines.
  • width - Desired width of blank lines in cells.
  • height - Desired total height.
  • style - Style for padding.
  • new_lines - Whether blank lines should include “\n”.
Source

pub fn align_bottom( lines: &[Vec<Segment>], width: usize, height: usize, style: Option<Style>, new_lines: bool, ) -> Vec<Vec<Segment>>

Align lines to the bottom by padding the top with blank lines.

Source

pub fn align_middle( lines: &[Vec<Segment>], width: usize, height: usize, style: Option<Style>, new_lines: bool, ) -> Vec<Vec<Segment>>

Align lines to the middle by padding top and bottom with blank lines.

Source

pub fn split_lines_terminator( segments: impl IntoIterator<Item = Segment>, ) -> Vec<(Vec<Segment>, bool)>

Split segments into lines, preserving a boolean flag indicating whether a newline character was encountered (true) or end of content (false).

This is equivalent to Python Rich’s Segment.split_lines_terminator.

Remove link metadata from segments.

This is equivalent to Python Rich’s Segment.strip_links.

Source

pub fn remove_color(segments: impl IntoIterator<Item = Segment>) -> Segments

Remove color information from segments, keeping text and attributes.

This is equivalent to Python Rich’s Segment.remove_color.

Trait Implementations§

Source§

impl Clone for Segment

Source§

fn clone(&self) -> Segment

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Segment

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Segment

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for Segment

Source§

impl From<&'static str> for Segment

Source§

fn from(s: &'static str) -> Self

Converts to this type from the input type.
Source§

impl From<Segment> for Segments

Source§

fn from(segment: Segment) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Segment

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<Segment> for Segments

Source§

fn from_iter<I: IntoIterator<Item = Segment>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl PartialEq for Segment

Source§

fn eq(&self, other: &Segment) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Segment

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.