Skip to main content

Text

Struct Text 

Source
pub struct Text {
    pub justify: JustifyMethod,
    pub overflow: OverflowMethod,
    pub no_wrap: bool,
    pub end: String,
    pub tab_size: usize,
    /* private fields */
}
Expand description

Rich text with styled spans.

Text represents styled text where different regions can have different styles. Styles are applied via Span objects which can overlap - when they do, later spans take precedence.

Fields§

§justify: JustifyMethod

Text justification method.

§overflow: OverflowMethod

Overflow handling method.

§no_wrap: bool

Disable wrapping.

§end: String

String to append after text (default “\n”).

§tab_size: usize

Tab expansion size (default 8).

Implementations§

Source§

impl Text

Source

pub fn from_ansi(text: &str) -> Self

Create a Text object from a string containing ANSI escape codes.

Python reference: rich.text.Text.from_ansi + rich.ansi.AnsiDecoder.

Source

pub fn from_ansi_with_options(text: &str, options: &FromAnsiOptions) -> Self

Create a Text object from ANSI escape codes with explicit options.

Source

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

Create a new Text from plain text.

This does NOT parse Rich markup. If you pass "[bold]text[/]", the literal markup will be preserved in the text. To parse markup into styled spans, use crate::markup::render or crate::markup::render_or_plain and pass the resulting Text.

Source

pub fn styled(text: impl Into<String>, style: Style) -> Self

Create a styled Text.

Source

pub fn assemble(pieces: &[(&str, Option<Style>)]) -> Self

Create Text by assembling multiple styled pieces.

Source

pub fn plain(&self) -> &str

Get the plain text content.

Source

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

Get the spans.

Source

pub fn len(&self) -> usize

Get the character length.

Source

pub fn is_empty(&self) -> bool

Check if the text is empty.

Source

pub fn cell_len(&self) -> usize

Get the cell width (for terminal display).

Source

pub fn style(&self) -> &Style

Get the base style.

Source

pub fn set_style(&mut self, style: Style)

Set the base style.

Source

pub fn append(&mut self, text: &str)

Append plain text.

Source

pub fn append_styled(&mut self, text: &str, style: Style)

Append styled text.

Source

pub fn append_text(&mut self, other: &Text)

Append another Text object, merging spans.

Source

pub fn stylize(&mut self, start: usize, end: usize, style: Style)

Apply a style to a character range.

Source

pub fn stylize_all(&mut self, style: Style)

Apply style to entire text.

Source

pub fn highlight_regex( &mut self, pattern: &str, style: &Style, ) -> Result<(), Error>

Highlight text matching a pattern with a style.

Source

pub fn highlight_words( &mut self, words: &[&str], style: &Style, case_sensitive: bool, )

Highlight specific words with a style.

Source

pub fn slice(&self, start: usize, end: usize) -> Self

Get a slice of the text as a new Text object.

Source

pub fn join<'a, I>(&self, items: I) -> Self
where I: IntoIterator<Item = &'a Self>,

Join an iterator of Text objects with this text as separator.

Creates a new Text by concatenating all items with this text inserted between each pair. Similar to str::join() but for styled Text.

§Examples
let separator = Text::new(", ");
let items = vec![Text::new("a"), Text::new("b"), Text::new("c")];
let joined = separator.join(&items);
assert_eq!(joined.plain(), "a, b, c");
Source

pub fn split_lines(&self) -> Vec<Self>

Split text at newlines.

Source

pub fn divide(&self, offsets: &[usize]) -> Vec<Self>

Divide text at specified character offsets.

Source

pub fn expand_tabs(&self, tab_size: usize) -> Self

Expand tabs to spaces.

Source

pub fn truncate( &mut self, max_width: usize, overflow: OverflowMethod, pad: bool, )

Truncate text to a maximum cell width.

Source

pub fn pad(&mut self, width: usize, align: JustifyMethod)

Pad text to a specific width.

Source

pub fn strip(&self) -> Self

Strip leading and trailing whitespace.

Source

pub fn to_lowercase(&self) -> Self

Convert text to lowercase.

Source

pub fn to_uppercase(&self) -> Self

Convert text to uppercase.

Source

pub fn render<'a>(&'a self, end: &'a str) -> Vec<Segment<'a>>

Render text to segments.

Source

pub fn wrap(&self, width: usize) -> Vec<Self>

Word wrap text to fit within a width.

Trait Implementations§

Source§

impl Add for Text

Source§

type Output = Text

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign for Text

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
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 Display for Text

Source§

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

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

impl Eq for Text

Source§

impl From<&str> for Text

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Text

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Text

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Renderable for Text

Source§

fn render<'a>( &'a self, _console: &Console, _options: &ConsoleOptions, ) -> Vec<Segment<'a>>

Render the object to a list of segments.

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.