pub struct Segment {
pub text: CompactString,
pub style: Option<Style>,
pub control: bool,
}Expand description
A span of text with an optional style. Mirrors rich.segment.Segment.
Fields§
§text: CompactStringThe text: a CompactString, kept inline up
to 24 bytes, so a short run allocates nothing. It reads as a &str.
style: Option<Style>§control: boolWhether this segment carries terminal control codes rather than content.
Implementations§
Source§impl Segment
impl Segment
Sourcepub fn new(text: impl Into<CompactString>, style: Option<Style>) -> Self
pub fn new(text: impl Into<CompactString>, style: Option<Style>) -> Self
A plain content segment.
Sourcepub fn control(text: impl Into<CompactString>) -> Self
pub fn control(text: impl Into<CompactString>) -> Self
A control segment (carries no visible width).
Sourcepub fn cell_length(&self) -> usize
pub fn cell_length(&self) -> usize
The number of terminal cells this segment occupies (0 for control).
Sourcepub fn simplify(segments: &[Segment]) -> Vec<Segment>
pub fn simplify(segments: &[Segment]) -> Vec<Segment>
Merge adjacent segments that share the same style and control flag.
Port of Segment.simplify.
Sourcepub fn remove_color(segments: &[Segment]) -> Vec<Segment>
pub fn remove_color(segments: &[Segment]) -> Vec<Segment>
Remove all colour from a segment stream, keeping every other attribute.
Port of Segment.remove_color.
Sourcepub fn apply_style(segments: &[Segment], style: &Style) -> Vec<Segment>
pub fn apply_style(segments: &[Segment], style: &Style) -> Vec<Segment>
Apply style as a base under each segment’s own style (that segment’s
style wins on top). Control segments are left untouched. Port of
Segment.apply_style (the style-only path).
Line-break segments ("\n") are also left unstyled: upstream’s
line-oriented print pipeline re-emits row separators plain, so styling
them would add stray SGR runs around every newline.
Sourcepub fn split_lines(segments: &[Segment]) -> Vec<Vec<Segment>>
pub fn split_lines(segments: &[Segment]) -> Vec<Vec<Segment>>
Split a flat segment stream into lines, breaking on \n.
Port of Segment.split_lines. Newline characters are consumed (not kept
in the output); a trailing newline yields a final empty line only if
there was content after the last break — or an explicit empty segment,
which is how a renderable whose last line is empty says so (the
port’s streams separate lines rather than end them).
Sourcepub fn set_shape(
lines: Vec<Vec<Segment>>,
width: usize,
height: usize,
) -> Vec<Vec<Segment>>
pub fn set_shape( lines: Vec<Vec<Segment>>, width: usize, height: usize, ) -> Vec<Vec<Segment>>
Shape a set of lines into exactly height rows of width cells: crop
extra rows, pad each row to width, and append blank rows to reach
height. Port of Segment.set_shape (style=None, new_lines=False).
Sourcepub fn fold_lines_words(segments: &[Segment], width: usize) -> Vec<Segment>
pub fn fold_lines_words(segments: &[Segment], width: usize) -> Vec<Segment>
Fold every line to at most width cells, breaking at word boundaries
the way upstream’s word wrapping does.
fold_lines breaks wherever the row happens to fill
up, which splits identifiers and words mid-character-run
(epsilon, z / eta, eta, theta)). Upstream’s word_wrap=True routes
through _wrap.divide_line, which we already port for Text — this
applies the same break offsets to a styled segment run, so styles survive
the split.
A word longer than width is still folded mid-word; there is nowhere
else to break it.
Sourcepub fn fold_lines(segments: &[Segment], width: usize) -> Vec<Segment>
pub fn fold_lines(segments: &[Segment], width: usize) -> Vec<Segment>
Fold every line to at most width cells, carrying the overflow onto
continuation lines instead of discarding it.
crop_lines is the display backstop and throws the
remainder away — correct for a renderable that has already wrapped
itself, and data loss for one that emits a long line verbatim. Styles are
preserved across the split.
This breaks wherever the row happens to fill up, so it splits words. That
is right only for content upstream folds without word wrapping. A
renderable whose upstream counterpart goes through Text.wrap wants
fold_lines_words instead: reaching for this
one is what made --json print over t / he lazy where rich prints
over / the lazy.
Sourcepub fn crop_lines(segments: &[Segment], width: usize) -> Vec<Segment>
pub fn crop_lines(segments: &[Segment], width: usize) -> Vec<Segment>
Crop every line in a segment stream to at most width cells, discarding
the excess and leaving short lines alone.
Port of Segment.split_and_crop_lines with pad=False, which is what
Console.print(crop=True) applies to the finished stream. It is the only
thing standing between an Overflow::Ignore
text and a line that runs off the side of the terminal.
As upstream, any non-control segment containing \n is split there —
a print end such as "!!\n" arrives as one segment, and cropping it
whole would lose its newline — and each newline is re-emitted as a bare
Segment::line. A line is cropped only when it is wider than width;
then everything past the edge goes, zero-width characters and control
segments included (adjust_line_length).
Source§impl Segment
impl Segment
Sourcepub fn is_control(&self) -> bool
pub fn is_control(&self) -> bool
Whether the segment carries control codes. Port of
Segment.is_control.
Sourcepub fn split_cells(&self, cut: usize) -> (Segment, Segment)
pub fn split_cells(&self, cut: usize) -> (Segment, Segment)
Split into two segments at cell cut. A double-width character
that straddles the cut becomes a space on each side. Port of
Segment.split_cells.
Sourcepub fn apply_style_with(
segments: &[Segment],
style: Option<&Style>,
post_style: Option<&Style>,
) -> Vec<Segment>
pub fn apply_style_with( segments: &[Segment], style: Option<&Style>, post_style: Option<&Style>, ) -> Vec<Segment>
Apply style under each segment’s own style and post_style over
it; control segments lose their style. Port of Segment.apply_style
with both arguments (apply_style is the
port’s line-aware form of the style-only path).
Sourcepub fn filter_control(segments: &[Segment], is_control: bool) -> Vec<Segment>
pub fn filter_control(segments: &[Segment], is_control: bool) -> Vec<Segment>
Keep only the control segments (is_control) or only the others.
Port of Segment.filter_control.
Sourcepub fn split_lines_terminator(segments: &[Segment]) -> Vec<(Vec<Segment>, bool)>
pub fn split_lines_terminator(segments: &[Segment]) -> Vec<(Vec<Segment>, bool)>
Split into lines on \n, each with whether a newline ended it; a
last line without one comes only when it holds something. Port of
Segment.split_lines_terminator.
Sourcepub fn split_and_crop_lines(
segments: &[Segment],
length: usize,
style: Option<&Style>,
pad: bool,
include_new_lines: bool,
) -> Vec<Vec<Segment>>
pub fn split_and_crop_lines( segments: &[Segment], length: usize, style: Option<&Style>, pad: bool, include_new_lines: bool, ) -> Vec<Vec<Segment>>
Split into lines on \n and fit each to length cells, padding with
style when pad is set and ending each terminated line with a
newline segment when include_new_lines is. Port of
Segment.split_and_crop_lines.
Sourcepub fn adjust_line_length_with(
line: &[Segment],
length: usize,
style: Option<Style>,
pad: bool,
) -> Vec<Segment>
pub fn adjust_line_length_with( line: &[Segment], length: usize, style: Option<Style>, pad: bool, ) -> Vec<Segment>
adjust_line_length with upstream’s pad
argument: a short line is padded only when it is set. Port of
Segment.adjust_line_length.
Sourcepub fn get_line_length(line: &[Segment]) -> usize
pub fn get_line_length(line: &[Segment]) -> usize
The cell length of a line, control segments excluded. Port of
Segment.get_line_length.
Sourcepub fn get_shape(lines: &[Vec<Segment>]) -> (usize, usize)
pub fn get_shape(lines: &[Vec<Segment>]) -> (usize, usize)
The width of the widest line and the number of lines. Port of
Segment.get_shape.
Sourcepub fn set_shape_with(
lines: &[Vec<Segment>],
width: usize,
height: Option<usize>,
style: Option<&Style>,
new_lines: bool,
) -> Vec<Vec<Segment>>
pub fn set_shape_with( lines: &[Vec<Segment>], width: usize, height: Option<usize>, style: Option<&Style>, new_lines: bool, ) -> Vec<Vec<Segment>>
Every line adjusted to width, then blank lines of style added up
to height (the line count when None); blank lines end in \n
with new_lines. Port of Segment.set_shape, which, unlike
set_shape, does not drop lines past height.
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>>
Lines aligned to the top of height rows: blank lines of style
below. Port of Segment.align_top.
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>>
Lines aligned to the bottom of height rows: blank lines above. Port
of Segment.align_bottom.
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>>
Lines in the middle of height rows, the odd blank line below. Port
of Segment.align_middle.
Sourcepub fn strip_links(segments: &[Segment]) -> Vec<Segment>
pub fn strip_links(segments: &[Segment]) -> Vec<Segment>
The segments with every hyperlink removed. Port of
Segment.strip_links.
Sourcepub fn strip_styles(segments: &[Segment]) -> Vec<Segment>
pub fn strip_styles(segments: &[Segment]) -> Vec<Segment>
The segments with no styles at all. Port of Segment.strip_styles.
Trait Implementations§
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 UnsafeUnpin 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.