Skip to main content

Text

Struct Text 

Source
pub struct Text { /* private fields */ }
Expand description

Styled text. Mirrors rich.text.Text.

Implementations§

Source§

impl Text

Source

pub fn new(plain: impl Into<String>) -> Self

Plain, unstyled text.

Source

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.

Source

pub fn justify(self, justify: Justify) -> Self

Set how lines are justified within the render width (builder form).

Source

pub fn set_justify(&mut self, justify: Justify)

Set how lines are justified within the render width.

Source

pub fn get_justify(&self) -> Justify

This text’s own justify method.

Source

pub fn overflow(self, overflow: Overflow) -> Self

Set what happens to lines wider than the render width (builder form).

Source

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.

Source

pub fn get_overflow(&self) -> Option<Overflow>

This text’s own overflow method, if it set one.

Source

pub fn no_wrap(self, no_wrap: bool) -> Self

Disable (or re-enable) wrapping for this text (builder form).

Source

pub fn set_no_wrap(&mut self, no_wrap: Option<bool>)

Disable (or re-enable) wrapping. Pass None to defer to the console options.

Source

pub fn get_no_wrap(&self) -> Option<bool>

This text’s own no-wrap setting, if it set one.

Source

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.

Source

pub fn blank_copy(&self) -> Text

An empty Text carrying this one’s style, justify, overflow and no-wrap. Port of Text.blank_copy.

Source

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.

Source

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.

Source

pub fn pad(&mut self, count: usize, character: char)

Pad both sides with count copies of character. Port of Text.pad.

Source

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.

Source

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.

Source

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.

Source

pub fn rstrip(&mut self)

Remove trailing whitespace. Port of Text.rstrip.

Source

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.

Source

pub fn expand_tabs(&mut self, tab_size: usize)

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn plain(&self) -> &str

The unstyled string content.

Source

pub fn spans(&self) -> &[Span]

The spans currently applied.

Source

pub fn cell_len(&self) -> usize

Length in terminal cells.

Source

pub fn is_empty(&self) -> bool

True when there is no content.

Source

pub fn append(&mut self, text: &str, style: Option<StyleType>)

Append more text, optionally under style (a resolved Style or a style name).

Source

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.

Source

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.

Source

pub fn set_base_style(&mut self, style: impl Into<StyleType>)

Set the whole-text base style, resolved or named.

Source

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.

Source

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__.

Source

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.

Source

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).

Source

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.

Source

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 Clone for Text

Source§

fn clone(&self) -> Text

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Text

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Text

Source§

fn default() -> Text

Returns the “default value” for a type. Read more
Source§

impl Renderable for Text

Source§

fn rich_render( &self, console: &Console, options: &ConsoleOptions, ) -> Vec<Segment>

Source§

fn measure(&self, _console: &Console, options: &ConsoleOptions) -> Measurement

The (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.

Auto Trait Implementations§

§

impl Freeze for Text

§

impl RefUnwindSafe for Text

§

impl Send for Text

§

impl Sync for Text

§

impl Unpin for Text

§

impl UnsafeUnpin for Text

§

impl UnwindSafe for Text

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.