pub struct Segment {
pub text: String,
pub style: Style,
pub is_control: bool,
}Expand description
A piece of styled text, the fundamental rendering unit.
Every widget’s render method produces lines of segments.
Fields§
§text: StringThe text content.
style: StyleThe style applied to this segment.
is_control: boolWhether this is a control sequence (not visible text).
Implementations§
Source§impl Segment
impl Segment
Sourcepub fn styled(text: impl Into<String>, style: Style) -> Self
pub fn styled(text: impl Into<String>, style: Style) -> Self
Create a new segment with the given style.
Sourcepub fn control(text: impl Into<String>) -> Self
pub fn control(text: impl Into<String>) -> Self
Create a control segment (not rendered as visible text).
Sourcepub fn display_width(&self) -> usize
pub fn display_width(&self) -> usize
Display width in terminal cells (alias for width()).
Sourcepub fn grapheme_widths(&self) -> Vec<(String, usize)>
pub fn grapheme_widths(&self) -> Vec<(String, usize)>
Returns each grapheme cluster in this segment together with its display width.
Combining marks (zero-width) are grouped with their base character into a single grapheme cluster by the Unicode segmentation algorithm.
Sourcepub fn char_count(&self) -> usize
pub fn char_count(&self) -> usize
Returns the number of grapheme clusters in this segment.
This counts user-perceived characters, so a base character followed by combining diacritics counts as one.
Sourcepub fn truncate_to_width(&self, max_width: usize) -> Segment
pub fn truncate_to_width(&self, max_width: usize) -> Segment
Truncate this segment to at most max_width display columns.
If the segment is already within max_width, returns an identical segment.
If a wide character straddles the boundary, it is excluded (the result may
be slightly shorter than max_width).
Sourcepub fn pad_to_width(&self, target_width: usize) -> Segment
pub fn pad_to_width(&self, target_width: usize) -> Segment
Pad this segment with trailing spaces to reach target_width display columns.
If the segment is already at or wider than target_width, returns unchanged.
Sourcepub fn split_at(&self, offset: usize) -> (Segment, Segment)
pub fn split_at(&self, offset: usize) -> (Segment, Segment)
Split this segment at the given display-width offset.
Returns (left, right) where left has the specified display width. If the offset falls in the middle of a wide character, the left side is padded with a space and the right side gets a leading space.
Combining marks (zero-width diacritics) are kept attached to their base character: if the split point falls between a base character and its combining marks, the combining marks travel with the base.