Skip to main content

Text

Struct Text 

Source
pub struct Text {
    pub plain: String,
    pub spans: Vec<Span>,
    pub style: Style,
    pub justify: JustifyMethod,
    pub end: String,
    pub overflow: OverflowMethod,
    pub no_wrap: bool,
}
Expand description

A renderable piece of text with optional style spans.

Fields§

§plain: String

The plain text content.

§spans: Vec<Span>

Style spans applied over the text.

§style: Style

Default style for the entire text.

§justify: JustifyMethod

Justification method.

§end: String

End string (appended during rendering).

§overflow: OverflowMethod

Overflow method.

§no_wrap: bool

If true, don’t wrap.

Implementations§

Source§

impl Text

Source

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

Create a new Text with the given plain content.

Source

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

Create a styled Text.

Source

pub fn style(self, style: Style) -> Self

Builder: set the default style.

Source

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

Builder: set the justification.

Source

pub fn end(self, end: impl Into<String>) -> Self

Builder: set the end string.

Source

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

Builder: set overflow method.

Source

pub fn append(&mut self, text: impl Into<Text>, style: Option<Style>)

Append another Text or string to this one, with an optional style.

Source

pub fn append_styled(&mut self, text: impl Into<String>, style: Style)

Append a plain string with a style.

Source

pub fn cell_len(&self) -> usize

Get the cell length of the plain text.

Source

pub fn style_at(&self, position: usize) -> Style

Get the style at a given position, combining spans.

Source

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

Truncate the text to the given maximum width.

Source

pub fn expand_tabs(&mut self)

Expand tab characters to spaces.

Source

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

Split the text into lines.

Source

pub fn render(&self) -> String

Render the text, applying styles and returning a styled string.

Source

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

Pad the text on both sides with count copies of character.

Source

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

Pad the left side only.

Source

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

Pad the right side only.

Source

pub fn align(&mut self, method: AlignMethod, width: usize)

Align the text within a given width using the specified method.

Source

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

Apply a style to a range of text. Equivalent to Python’s Text.stylize().

Source

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

Highlight all regex matches with the given style. Returns count of matches. Equivalent to Python’s Text.highlight_regex().

Source

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

Simple word-wrap: split text into lines, each not exceeding width cells. Returns a Vec<Text>, one per wrapped line. Equivalent to Python’s Text.wrap().

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

Source§

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

Formats the value using the given formatter. Read more
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 From<Text> for TextType

Source§

fn from(t: Text) -> Self

Converts to this type from the input type.
Source§

impl Renderable for Text

Allows a Text object to be used as a renderable.

Source§

fn render(&self, _options: &ConsoleOptions) -> RenderResult

Render this object into a RenderResult using the provided options. Read more
Source§

fn measure(&self, _options: &ConsoleOptions) -> Option<Measurement>

Optional width-measurement hook (equivalent to __rich_measure__). Override to provide min/max width constraints for layout.

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