pub struct StyledString<T = String> { /* private fields */ }Expand description
Heap-allocated styled string.
StyledString represents the owned string variant in contrast to StyledStr, which is borrowed.
Since conversion to a StyledStr is cheap, some immutable methods are accessible via StyledStr
only (e.g., iterating over style spans or splitting a string).
This allows to define these methods as const fns and/or to propagate the borrowed data lifetime correctly.
A StyledString can be parsed from rich syntax via the FromStr trait,
from a string with ANSI escapes via StyledString::from_ansi(), or manually constructed via StyledStringBuilder.
Implementations§
Source§impl<T> StyledString<T>
impl<T> StyledString<T>
Source§impl StyledString
impl StyledString
Sourcepub fn builder() -> StyledStringBuilder
pub fn builder() -> StyledStringBuilder
Creates a builder for styled strings.
Sourcepub fn from_ansi(ansi_str: &str) -> Result<Self, AnsiError>
pub fn from_ansi(ansi_str: &str) -> Result<Self, AnsiError>
Parses a string from a string with embedded ANSI escape sequences.
§Errors
Returns an error if the input is not a valid ANSI escaped string.
§Examples
let str = StyledString::from_ansi(
"\u{1b}[1;32mHello,\u{1b}[m world\u{1b}[3m!\u{1b}[m",
)?;
assert_eq!(str, styled!("[[bold green]]Hello,[[/]] world[[it]]!"));Sourcepub fn from_ansi_bytes(ansi_bytes: &[u8]) -> Result<Self, AnsiError>
pub fn from_ansi_bytes(ansi_bytes: &[u8]) -> Result<Self, AnsiError>
Parses a string from bytes with embedded ANSI escape sequences. This is similar to
Self::from_ansi(), just using bytes as an input.
§Errors
Returns an error if the input is not a valid ANSI escaped string.
Sourcepub fn push_str(&mut self, other: StyledStr<'_>)
pub fn push_str(&mut self, other: StyledStr<'_>)
Pushes another styled string at the end of this one.
Use StyledStringBuilder for more complex string manipulations.
§Examples
let mut styled = styled!("[[bold green!]]Hello").to_owned();
styled.push_str(styled!("[[it]]!"));
// `push_str()` is also available via `+=` operator:
styled += styled!("[[it]]❤");
assert_eq!(
styled,
styled!("[[bold green!]]Hello[[it]]!❤")
);Sourcepub fn pop(&mut self) -> Option<(char, Style)>
pub fn pop(&mut self) -> Option<(char, Style)>
Pops a single char from the end of the string.
§Examples
let mut styled = styled!("[[bold green!]]Hello[[it]]!❤").to_owned();
let (ch, style) = styled.pop().unwrap();
assert_eq!(ch, '❤');
assert_eq!(style, Style::new().italic());
assert_eq!(styled, styled!("[[bold green!]]Hello[[it]]!"));
styled.pop().unwrap();
assert_eq!(styled, styled!("[[bold green!]]Hello"));Trait Implementations§
Source§impl AddAssign<StyledStr<'_>> for StyledString
impl AddAssign<StyledStr<'_>> for StyledString
Source§fn add_assign(&mut self, rhs: StyledStr<'_>)
fn add_assign(&mut self, rhs: StyledStr<'_>)
+= operation. Read moreSource§impl<T: Clone> Clone for StyledString<T>
impl<T: Clone> Clone for StyledString<T>
Source§fn clone(&self) -> StyledString<T>
fn clone(&self) -> StyledString<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T> Debug for StyledString<T>
impl<T> Debug for StyledString<T>
Source§impl<T: Default> Default for StyledString<T>
impl<T: Default> Default for StyledString<T>
Source§fn default() -> StyledString<T>
fn default() -> StyledString<T>
Source§impl<T> Display for StyledString<T>
impl<T> Display for StyledString<T>
Source§impl<'a> Extend<StyledStr<'a>> for StyledString
impl<'a> Extend<StyledStr<'a>> for StyledString
Source§fn extend<I: IntoIterator<Item = StyledStr<'a>>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = StyledStr<'a>>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)