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
impl Segment
Sourcepub fn styled(text: impl Into<Cow<'static, str>>, style: Style) -> Self
pub fn styled(text: impl Into<Cow<'static, str>>, style: Style) -> Self
Create a new styled segment.
Sourcepub fn styled_with_meta(
text: impl Into<Cow<'static, str>>,
style: Style,
meta: StyleMeta,
) -> Self
pub fn styled_with_meta( text: impl Into<Cow<'static, str>>, style: Style, meta: StyleMeta, ) -> Self
Create a new styled segment with metadata.
Sourcepub fn new_with_meta(
text: impl Into<Cow<'static, str>>,
meta: StyleMeta,
) -> Self
pub fn new_with_meta( text: impl Into<Cow<'static, str>>, meta: StyleMeta, ) -> Self
Create a new segment with metadata and no style.
Sourcepub fn control(control: ControlType) -> Self
pub fn control(control: ControlType) -> Self
Create a control segment.
Sourcepub fn is_control(&self) -> bool
pub fn is_control(&self) -> bool
Check if this segment is a control segment.
Sourcepub fn apply_style(&self, style: &Style) -> Self
pub fn apply_style(&self, style: &Style) -> Self
Apply a style to this segment, combining with any existing style.
Sourcepub fn split_cells(&self, cut: usize) -> (Segment, Segment)
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");Sourcepub fn split_lines(
segments: impl IntoIterator<Item = Segment>,
) -> Vec<Vec<Segment>>
pub fn split_lines( segments: impl IntoIterator<Item = Segment>, ) -> Vec<Vec<Segment>>
Sourcepub fn split_and_crop_lines(
segments: impl IntoIterator<Item = Segment>,
length: usize,
style: Option<Style>,
pad: bool,
include_new_lines: bool,
) -> Vec<Vec<Segment>>
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 thanlength.include_new_lines- Whether to append newline segments to each line.
§Returns
A vector of lines, each cropped/padded to the desired length.
Sourcepub fn adjust_line_length(
line: &[Segment],
length: usize,
style: Option<Style>,
pad: bool,
) -> Vec<Segment>
pub fn adjust_line_length( line: &[Segment], length: usize, style: Option<Style>, pad: bool, ) -> Vec<Segment>
Sourcepub fn get_last_style(line: &[Segment]) -> Option<Style>
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.
Sourcepub fn simplify(segments: impl IntoIterator<Item = Segment>) -> Segments
pub fn simplify(segments: impl IntoIterator<Item = Segment>) -> Segments
Sourcepub fn divide(
segments: impl IntoIterator<Item = Segment>,
cuts: &[usize],
) -> Vec<Vec<Segment>>
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.
Sourcepub fn apply_style_to_segments(
segments: impl IntoIterator<Item = Segment>,
style: Option<Style>,
post_style: Option<Style>,
) -> Segments
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.
Sourcepub fn filter_control(
segments: impl IntoIterator<Item = Segment>,
is_control: bool,
) -> Segments
pub fn filter_control( segments: impl IntoIterator<Item = Segment>, is_control: bool, ) -> Segments
Sourcepub fn strip_styles(segments: impl IntoIterator<Item = Segment>) -> Segments
pub fn strip_styles(segments: impl IntoIterator<Item = Segment>) -> Segments
Sourcepub fn get_line_length(line: &[Segment]) -> usize
pub fn get_line_length(line: &[Segment]) -> usize
Sourcepub fn set_shape(
lines: &[Vec<Segment>],
width: usize,
height: Option<usize>,
style: Option<Style>,
new_lines: bool,
) -> Vec<Vec<Segment>>
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.
Sourcepub fn align_top(
lines: &[Vec<Segment>],
width: usize,
height: usize,
style: Option<Style>,
new_lines: bool,
) -> Vec<Vec<Segment>>
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”.
Sourcepub fn align_bottom(
lines: &[Vec<Segment>],
width: usize,
height: usize,
style: Option<Style>,
new_lines: bool,
) -> Vec<Vec<Segment>>
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.
Sourcepub fn align_middle(
lines: &[Vec<Segment>],
width: usize,
height: usize,
style: Option<Style>,
new_lines: bool,
) -> Vec<Vec<Segment>>
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.
Sourcepub fn split_lines_terminator(
segments: impl IntoIterator<Item = Segment>,
) -> Vec<(Vec<Segment>, bool)>
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.
Sourcepub fn strip_links(segments: impl IntoIterator<Item = Segment>) -> Segments
pub fn strip_links(segments: impl IntoIterator<Item = Segment>) -> Segments
Remove link metadata from segments.
This is equivalent to Python Rich’s Segment.strip_links.
Sourcepub fn remove_color(segments: impl IntoIterator<Item = Segment>) -> Segments
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 FromIterator<Segment> for Segments
impl FromIterator<Segment> for Segments
impl Eq for Segment
impl StructuralPartialEq for Segment
Auto Trait Implementations§
impl Freeze for Segment
impl RefUnwindSafe for Segment
impl Send for Segment
impl Sync for Segment
impl Unpin for Segment
impl UnwindSafe for Segment
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.