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: JustifyMethodText justification method.
overflow: OverflowMethodOverflow handling method.
no_wrap: boolDisable wrapping.
end: StringString to append after text (default “\n”).
tab_size: usizeTab expansion size (default 8).
Implementations§
Source§impl Text
impl Text
Sourcepub fn from_ansi(text: &str) -> Self
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.
Sourcepub fn from_ansi_with_options(text: &str, options: &FromAnsiOptions) -> Self
pub fn from_ansi_with_options(text: &str, options: &FromAnsiOptions) -> Self
Create a Text object from ANSI escape codes with explicit options.
Sourcepub fn new(text: impl Into<String>) -> Self
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.
Sourcepub fn assemble(pieces: &[(&str, Option<Style>)]) -> Self
pub fn assemble(pieces: &[(&str, Option<Style>)]) -> Self
Create Text by assembling multiple styled pieces.
Sourcepub fn append_styled(&mut self, text: &str, style: Style)
pub fn append_styled(&mut self, text: &str, style: Style)
Append styled text.
Sourcepub fn append_text(&mut self, other: &Text)
pub fn append_text(&mut self, other: &Text)
Append another Text object, merging spans.
Sourcepub fn stylize(&mut self, start: usize, end: usize, style: Style)
pub fn stylize(&mut self, start: usize, end: usize, style: Style)
Apply a style to a character range.
Sourcepub fn stylize_all(&mut self, style: Style)
pub fn stylize_all(&mut self, style: Style)
Apply style to entire text.
Sourcepub fn highlight_regex(
&mut self,
pattern: &str,
style: &Style,
) -> Result<(), Error>
pub fn highlight_regex( &mut self, pattern: &str, style: &Style, ) -> Result<(), Error>
Highlight text matching a pattern with a style.
Sourcepub fn highlight_words(
&mut self,
words: &[&str],
style: &Style,
case_sensitive: bool,
)
pub fn highlight_words( &mut self, words: &[&str], style: &Style, case_sensitive: bool, )
Highlight specific words with a style.
Sourcepub fn slice(&self, start: usize, end: usize) -> Self
pub fn slice(&self, start: usize, end: usize) -> Self
Get a slice of the text as a new Text object.
Sourcepub fn join<'a, I>(&self, items: I) -> Selfwhere
I: IntoIterator<Item = &'a Self>,
pub fn join<'a, I>(&self, items: I) -> Selfwhere
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");Sourcepub fn split_lines(&self) -> Vec<Self>
pub fn split_lines(&self) -> Vec<Self>
Split text at newlines.
Sourcepub fn divide(&self, offsets: &[usize]) -> Vec<Self>
pub fn divide(&self, offsets: &[usize]) -> Vec<Self>
Divide text at specified character offsets.
Sourcepub fn expand_tabs(&self, tab_size: usize) -> Self
pub fn expand_tabs(&self, tab_size: usize) -> Self
Expand tabs to spaces.
Sourcepub fn truncate(
&mut self,
max_width: usize,
overflow: OverflowMethod,
pad: bool,
)
pub fn truncate( &mut self, max_width: usize, overflow: OverflowMethod, pad: bool, )
Truncate text to a maximum cell width.
Sourcepub fn pad(&mut self, width: usize, align: JustifyMethod)
pub fn pad(&mut self, width: usize, align: JustifyMethod)
Pad text to a specific width.
Sourcepub fn to_lowercase(&self) -> Self
pub fn to_lowercase(&self) -> Self
Convert text to lowercase.
Sourcepub fn to_uppercase(&self) -> Self
pub fn to_uppercase(&self) -> Self
Convert text to uppercase.
Trait Implementations§
Source§impl AddAssign for Text
impl AddAssign for Text
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreimpl Eq for Text
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.