pub struct Segment<'a> {
pub text: Cow<'a, str>,
pub style: Option<Style>,
pub control: Option<SmallVec<[ControlCode; 2]>>,
}Expand description
A segment of styled text.
Segments are the atomic units of text rendering. They can contain:
- Regular text with optional styling
- Control codes for non-textual actions
Text is stored as Cow<str> to allow zero-copy for static strings
while still supporting owned data when needed.
Fields§
§text: Cow<'a, str>The text content (may be empty for control-only segments).
style: Option<Style>Optional style applied to this segment.
control: Option<SmallVec<[ControlCode; 2]>>Optional control codes (stack-allocated for common cases).
Implementations§
Source§impl<'a> Segment<'a>
impl<'a> Segment<'a>
Sourcepub fn styled(s: impl Into<Cow<'a, str>>, style: Style) -> Self
pub fn styled(s: impl Into<Cow<'a, str>>, style: Style) -> Self
Create a new styled text segment.
Sourcepub fn control(code: ControlCode) -> Self
pub fn control(code: ControlCode) -> Self
Create a control segment (no text, just control codes).
Sourcepub fn is_control(&self) -> bool
pub fn is_control(&self) -> bool
Check if this is a control-only segment.
Sourcepub fn is_newline(&self) -> bool
pub fn is_newline(&self) -> bool
Check if this segment contains a newline control code.
Sourcepub fn cell_length(&self) -> usize
pub fn cell_length(&self) -> usize
Get the display width in terminal cells.
Control segments have zero width. Text width is calculated using Unicode width rules.
Sourcepub fn cell_length_with<F>(&self, width_fn: F) -> usize
pub fn cell_length_with<F>(&self, width_fn: F) -> usize
Calculate cell length with a specific width function.
This allows custom width calculations (e.g., for testing or terminal-specific behavior).
Sourcepub fn split_at_cell(&self, cell_pos: usize) -> (Self, Self)
pub fn split_at_cell(&self, cell_pos: usize) -> (Self, Self)
Split the segment at a cell position.
Returns (left, right) where:
leftcontains content up to (but not exceeding)cell_poscellsrightcontains the remaining content
The split respects grapheme cluster boundaries to avoid breaking emoji, combining characters, or other complex graphemes.
§Panics
Does not panic; if cell_pos is beyond the segment length,
returns (self, empty).
Sourcepub fn with_style(self, style: Style) -> Self
pub fn with_style(self, style: Style) -> Self
Apply a style to this segment.
Sourcepub fn into_owned(self) -> Segment<'static>
pub fn into_owned(self) -> Segment<'static>
Convert to an owned segment (no lifetime constraints).
Sourcepub fn with_control(self, code: ControlCode) -> Self
pub fn with_control(self, code: ControlCode) -> Self
Add a control code to this segment.
Trait Implementations§
impl<'a> Eq for Segment<'a>
impl<'a> StructuralPartialEq for Segment<'a>
Auto Trait Implementations§
impl<'a> Freeze for Segment<'a>
impl<'a> RefUnwindSafe for Segment<'a>
impl<'a> Send for Segment<'a>
impl<'a> Sync for Segment<'a>
impl<'a> Unpin for Segment<'a>
impl<'a> UnwindSafe for Segment<'a>
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§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.