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,
    pub tab_size: usize,
    pub indent_guides: 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.

§tab_size: usize

Tab width for expand_tabs (default 8).

§indent_guides: bool

Whether to show indent guide lines.

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(style: Style) -> Self

Create a styled Text with a default style but no content.

Source

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

Parse an ANSI-escaped string into a styled Text.

Extracts SGR (Select Graphic Rendition) codes as style spans and strips all other ANSI escape sequences.

Source

pub fn from_markup(markup: &str) -> Self

Parse a BBCode-like markup string into a styled Text. Delegates to crate::markup::render().

Source

pub fn get_style(&self) -> &Style

Get a reference to the default style.

Source

pub fn get_style_mut(&mut self) -> &mut Style

Get a mutable reference to the default style.

Source

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

Get a reference to the spans slice.

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 no_wrap(self, value: bool) -> Self

Builder: set no_wrap flag.

Source

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

Builder: set tab size for expand_tabs.

Source

pub fn with_indent_guides(self, show: bool) -> Self

Builder: show indent guides.

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_tokens(&mut self, tokens: Vec<(String, Style)>)

Append a sequence of (text, style) tokens.

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 get_style_at_offset(&self, offset: usize) -> Style

Get the combined style at a specific byte offset. Iterates through spans that cover the offset and combines them.

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 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 stylize_before(&mut self, style: Style, start: usize, end: Option<usize>)

Like [stylize] but inserts the span at the beginning of the spans list, giving it lower priority than existing spans.

Source

pub fn apply_meta(&mut self, meta: Vec<u8>, spans: &[Span])

Apply metadata to the text at the given spans. Sets the style.meta field on overlapping spans.

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 blank_copy(&self) -> Self

Return a new Text with the same style settings but empty content.

Source

pub fn copy_styles(&self) -> Self

Return a copy with only the plain text (no spans, default style).

Source

pub fn detect_indentation(&self) -> (String, usize)

Detect the leading whitespace indentation. Returns (indent_string, indent_count) where indent_count is the number of occurrences of the first indent character.

Source

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

Split the text at the given byte offsets into multiple pieces, preserving style spans.

Source

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

Apply a style to the entire text by adding a full-length span.

Source

pub fn fit(&self, width: usize) -> Text

Truncate or pad to exactly fit width cells. Uses existing truncate + align methods.

Source

pub fn set_length(&mut self, length: usize)

Pad with spaces or truncate to exact length cells.

Source

pub fn remove_suffix(&mut self, suffix: &str) -> bool

Remove a suffix from the text, returning true if it was present.

Source

pub fn rstrip_end(&mut self, end: &str) -> bool

Remove a trailing end string, returning true if it was present.

Source

pub fn right_crop(&mut self, offset: usize) -> Text

Crop all text beyond offset. Returns the cropped (removed) portion.

Source

pub fn rstrip(&mut self) -> &mut Self

Remove trailing whitespace.

Source

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

Split at every character boundary, producing per-character Text pieces.

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

Source

pub fn render(&self) -> String

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

Source

pub fn markup(&self) -> String

Serialize the text to a BBCode-like markup string. Each span is wrapped in [style]...[/] tags.

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.

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() -> Self

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 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<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
where T: Real + Zero + Arithmetics + Clone, Swp: WhitePoint<T>, Dwp: WhitePoint<T>, D: AdaptFrom<S, Swp, Dwp, T>,

Source§

fn adapt_into_using<M>(self, method: M) -> D
where M: TransformMatrix<T>,

Convert the source color to the destination color using the specified method.
Source§

fn adapt_into(self) -> D

Convert the source color to the destination color using the bradford method by default.
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, C> ArraysFrom<C> for T
where C: IntoArrays<T>,

Source§

fn arrays_from(colors: C) -> T

Cast a collection of colors into a collection of arrays.
Source§

impl<T, C> ArraysInto<C> for T
where C: FromArrays<T>,

Source§

fn arrays_into(self) -> C

Cast this collection of arrays into a collection of colors.
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<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for U
where T: FromCam16Unclamped<WpParam, U>,

Source§

type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
Source§

fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
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, C> ComponentsFrom<C> for T
where C: IntoComponents<T>,

Source§

fn components_from(colors: C) -> T

Cast a collection of colors into a collection of color components.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromAngle<T> for T

Source§

fn from_angle(angle: T) -> T

Performs a conversion from angle.
Source§

impl<T, U> FromStimulus<U> for T
where U: IntoStimulus<T>,

Source§

fn from_stimulus(other: U) -> T

Converts other into Self, while performing the appropriate scaling, rounding and clamping.
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, U> IntoAngle<U> for T
where U: FromAngle<T>,

Source§

fn into_angle(self) -> U

Performs a conversion into T.
Source§

impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for U
where T: Cam16FromUnclamped<WpParam, U>,

Source§

type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
Source§

fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
Source§

impl<T, U> IntoColor<U> for T
where U: FromColor<T>,

Source§

fn into_color(self) -> U

Convert into T with values clamped to the color defined bounds Read more
Source§

impl<T, U> IntoColorUnclamped<U> for T
where U: FromColorUnclamped<T>,

Source§

fn into_color_unclamped(self) -> U

Convert into T. The resulting color might be invalid in its color space Read more
Source§

impl<T> IntoStimulus<T> for T

Source§

fn into_stimulus(self) -> T

Converts self into T, while performing the appropriate scaling, rounding and clamping.
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, C> TryComponentsInto<C> for T
where C: TryFromComponents<T>,

Source§

type Error = <C as TryFromComponents<T>>::Error

The error for when try_into_colors fails to cast.
Source§

fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>

Try to cast this collection of color components into a collection of colors. 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.
Source§

impl<T, U> TryIntoColor<U> for T
where U: TryFromColor<T>,

Source§

fn try_into_color(self) -> Result<U, OutOfBounds<U>>

Convert into T, returning ok if the color is inside of its defined range, otherwise an OutOfBounds error is returned which contains the unclamped color. Read more
Source§

impl<C, U> UintsFrom<C> for U
where C: IntoUints<U>,

Source§

fn uints_from(colors: C) -> U

Cast a collection of colors into a collection of unsigned integers.
Source§

impl<C, U> UintsInto<C> for U
where C: FromUints<U>,

Source§

fn uints_into(self) -> C

Cast this collection of unsigned integers into a collection of colors.