pub struct Text { /* private fields */ }Expand description
Styled text. Mirrors rich.text.Text.
Implementations§
Source§impl Text
impl Text
Sourcepub fn styled(plain: impl Into<String>, style: impl Into<StyleType>) -> Self
pub fn styled(plain: impl Into<String>, style: impl Into<StyleType>) -> Self
Text with a base style, which may be a style name resolved at render
time (Text::styled("hi", "repr.number")) or a resolved Style.
Sourcepub fn justify(self, justify: Justify) -> Self
pub fn justify(self, justify: Justify) -> Self
Set how lines are justified within the render width (builder form).
Sourcepub fn set_justify(&mut self, justify: Justify)
pub fn set_justify(&mut self, justify: Justify)
Set how lines are justified within the render width.
Sourcepub fn get_justify(&self) -> Justify
pub fn get_justify(&self) -> Justify
This text’s own justify method.
Sourcepub fn overflow(self, overflow: Overflow) -> Self
pub fn overflow(self, overflow: Overflow) -> Self
Set what happens to lines wider than the render width (builder form).
Sourcepub fn set_overflow(&mut self, overflow: Option<Overflow>)
pub fn set_overflow(&mut self, overflow: Option<Overflow>)
Set what happens to lines wider than the render width. Pass None to
defer to the console options.
Sourcepub fn get_overflow(&self) -> Option<Overflow>
pub fn get_overflow(&self) -> Option<Overflow>
This text’s own overflow method, if it set one.
Sourcepub fn no_wrap(self, no_wrap: bool) -> Self
pub fn no_wrap(self, no_wrap: bool) -> Self
Disable (or re-enable) wrapping for this text (builder form).
Sourcepub fn set_no_wrap(&mut self, no_wrap: Option<bool>)
pub fn set_no_wrap(&mut self, no_wrap: Option<bool>)
Disable (or re-enable) wrapping. Pass None to defer to the console
options.
Sourcepub fn get_no_wrap(&self) -> Option<bool>
pub fn get_no_wrap(&self) -> Option<bool>
This text’s own no-wrap setting, if it set one.
Sourcepub fn truncate(
&mut self,
max_width: usize,
overflow: Option<Overflow>,
pad: bool,
)
pub fn truncate( &mut self, max_width: usize, overflow: Option<Overflow>, pad: bool, )
Shorten this text to at most max_width cells, optionally padding it out
to exactly max_width when it is shorter. Port of Text.truncate.
overflow defaults to this text’s own method, then to Overflow::Fold;
Overflow::Ignore leaves the text alone entirely. Note that Fold and
Crop behave identically here — folding is a property of wrapping, and
a line that has already been wrapped can only be cut.
Sourcepub fn blank_copy(&self) -> Text
pub fn blank_copy(&self) -> Text
An empty Text carrying this one’s style, justify, overflow and no-wrap.
Port of Text.blank_copy.
Sourcepub fn divide(&self, offsets: &[usize]) -> Vec<Text>
pub fn divide(&self, offsets: &[usize]) -> Vec<Text>
Cut this text at each byte offset in offsets, returning the pieces.
Port of Text.divide.
Every piece inherits the base style, justify, overflow and no-wrap, and
each span is re-based onto the pieces it covers. Spans that would come out
empty are dropped, matching upstream’s new_end > new_start.
Offsets are byte offsets (as everywhere else in this port’s span
arithmetic) and must fall on char boundaries.
Sourcepub fn split(
&self,
separator: &str,
include_separator: bool,
allow_blank: bool,
) -> Vec<Text>
pub fn split( &self, separator: &str, include_separator: bool, allow_blank: bool, ) -> Vec<Text>
Split on separator. Port of Text.split.
include_separator keeps the separator at the end of each piece.
allow_blank keeps the trailing empty piece that a text ending in the
separator would otherwise produce.
§Panics
If separator is empty, which upstream asserts against.
Sourcepub fn pad(&mut self, count: usize, character: char)
pub fn pad(&mut self, count: usize, character: char)
Pad both sides with count copies of character. Port of Text.pad.
Sourcepub fn pad_left(&mut self, count: usize, character: char)
pub fn pad_left(&mut self, count: usize, character: char)
Pad the left with count copies of character, shifting every span to
follow the text. Port of Text.pad_left.
Sourcepub fn pad_right(&mut self, count: usize, character: char)
pub fn pad_right(&mut self, count: usize, character: char)
Pad the right with count copies of character. Port of
Text.pad_right. Spans are untouched, so the padding is unstyled.
Sourcepub fn right_crop(&mut self, amount: usize)
pub fn right_crop(&mut self, amount: usize)
Drop the last amount bytes, clipping any span that reached into them.
Port of Text.right_crop.
Sourcepub fn rstrip_end(&mut self, size: usize)
pub fn rstrip_end(&mut self, size: usize)
Remove only as much trailing whitespace as it takes to get down to
size cells, leaving the rest. Port of Text.rstrip_end.
This is what lets a wrapped line keep the space that ended it while a line that overshot the width gives its padding back.
pub fn expand_tabs(&mut self, tab_size: usize)
Sourcepub fn join(&self, lines: &[Text]) -> Text
pub fn join(&self, lines: &[Text]) -> Text
Join lines with this text as the separator, carrying each piece’s base
style across as a covering span. Port of Text.join.
Sourcepub fn highlight_words(
&mut self,
words: &[&str],
style: impl Into<StyleType>,
case_sensitive: bool,
) -> Result<usize>
pub fn highlight_words( &mut self, words: &[&str], style: impl Into<StyleType>, case_sensitive: bool, ) -> Result<usize>
Style every occurrence of any of words. Port of Text.highlight_words,
returning the number of matches.
Sourcepub fn highlight_regex(
&mut self,
pattern: &str,
style: Option<StyleType>,
style_prefix: &str,
) -> Result<usize>
pub fn highlight_regex( &mut self, pattern: &str, style: Option<StyleType>, style_prefix: &str, ) -> Result<usize>
Style every match of pattern, returning the number of matches. Full port
of Text.highlight_regex.
style, when given, styles the whole match. Each named group is then
styled with {style_prefix}{name} as a style name, left for the theme
to resolve at render time — which is how a highlighter colours its groups
without ever seeing a console.
Groups that did not participate in the match, and zero-width ones, are skipped.
Sourcepub fn from_markup(markup_text: &str) -> Result<Text>
pub fn from_markup(markup_text: &str) -> Result<Text>
Build styled text from console markup. Port of Text.from_markup.
Tag names are stored on the spans and resolved when the text is rendered, so no theme is needed here.
Sourcepub fn append(&mut self, text: &str, style: Option<StyleType>)
pub fn append(&mut self, text: &str, style: Option<StyleType>)
Append more text, optionally under style (a resolved Style or a
style name).
Sourcepub fn append_text(self, other: &Text) -> Text
pub fn append_text(self, other: &Text) -> Text
Append another Text, carrying over its base style (as a covering span)
and all of its spans, shifted to their new offsets. Port of
Text.append_text. Consumes self and returns it for chaining.
Sourcepub fn stylize(&mut self, style: impl Into<StyleType>, start: usize, end: usize)
pub fn stylize(&mut self, style: impl Into<StyleType>, start: usize, end: usize)
Apply style to the byte range [start, end). Port of Text.stylize,
including its argument order.
style may be a resolved Style or a name ("repr.number") left for
the renderer to look up. Byte offsets, not char offsets; ASCII-only
callers such as highlighters are unaffected by the distinction.
A range that is empty or inverted is ignored, which is what gives us
upstream’s end > start skip for non-participating regex groups.
Sourcepub fn set_base_style(&mut self, style: impl Into<StyleType>)
pub fn set_base_style(&mut self, style: impl Into<StyleType>)
Set the whole-text base style, resolved or named.
Sourcepub fn render(&self, theme: &Theme, base_style: &Style) -> Vec<Segment>
pub fn render(&self, theme: &Theme, base_style: &Style) -> Vec<Segment>
Flatten into non-overlapping segments (newlines become Segment::line),
combining base_style, this text’s base style, and every covering span.
Does not wrap. Port of the core of Text.render.
Named span styles are resolved against theme.
Sourcepub fn measurement(&self) -> (usize, usize)
pub fn measurement(&self) -> (usize, usize)
The (minimum, maximum) cell width of this text: maximum is the widest
hard line, minimum the widest word. Port of Text.__rich_measure__.
Sourcepub fn render_lines(
&self,
theme: &Theme,
base_style: &Style,
width: Option<usize>,
) -> Vec<Vec<Segment>>
pub fn render_lines( &self, theme: &Theme, base_style: &Style, width: Option<usize>, ) -> Vec<Vec<Segment>>
Render into visual lines, wrapping each hard line to width cells when
Some, and justifying per this text’s own justify.
Sourcepub fn render_lines_justified(
&self,
theme: &Theme,
base_style: &Style,
width: Option<usize>,
justify: Justify,
) -> Vec<Vec<Segment>>
pub fn render_lines_justified( &self, theme: &Theme, base_style: &Style, width: Option<usize>, justify: Justify, ) -> Vec<Vec<Segment>>
Like render_lines but with an explicit justify
(used by the console to apply options.justify).
Sourcepub fn render_lines_wrapped(
&self,
theme: &Theme,
base_style: &Style,
width: Option<usize>,
justify: Justify,
overflow: Overflow,
no_wrap: bool,
) -> Vec<Vec<Segment>>
pub fn render_lines_wrapped( &self, theme: &Theme, base_style: &Style, width: Option<usize>, justify: Justify, overflow: Overflow, no_wrap: bool, ) -> Vec<Vec<Segment>>
The full wrap-justify-truncate pipeline, with every knob resolved by the
caller. Port of Text.wrap.
Lines are split on \n, wrapped to width (folding over-long words only
when overflow is Overflow::Fold), justified, and finally truncated
to width. Overflow::Ignore skips wrapping and truncation both, so
lines may come back wider than width.
Sourcepub fn render_joined_wrapped(
&self,
theme: &Theme,
base_style: &Style,
width: usize,
justify: Justify,
overflow: Overflow,
no_wrap: bool,
) -> Vec<Segment>
pub fn render_joined_wrapped( &self, theme: &Theme, base_style: &Style, width: usize, justify: Justify, overflow: Overflow, no_wrap: bool, ) -> Vec<Segment>
As render_lines_wrapped, flattened into a
single segment stream with Segment::line between visual lines.
Trait Implementations§
Source§impl Renderable for Text
impl Renderable for Text
fn rich_render( &self, console: &Console, options: &ConsoleOptions, ) -> Vec<Segment>
Source§fn measure(&self, _console: &Console, options: &ConsoleOptions) -> Measurement
fn measure(&self, _console: &Console, options: &ConsoleOptions) -> Measurement
(minimum, maximum) cell width this renderable wants. The default
assumes the renderable fills the available width (e.g. Panel, Table);
Text overrides it with its content width so the top-level print path can
shrink to fit. Port of __rich_measure__ / Measurement.get.