StyledString

Struct StyledString 

Source
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>
where T: Deref<Target = str>,

Source

pub fn into_text(self) -> T

Returns the unstyled text behind this string.

Source

pub fn as_str(&self) -> StyledStr<'_>

Borrows a StyledStr from this string. This can be used to call more complex methods.

Source

pub fn text(&self) -> &str

Returns the unstyled text.

Source§

impl StyledString

Source

pub fn builder() -> StyledStringBuilder

Creates a builder for styled strings.

Source

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]]!"));
Source

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.

Source

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]]!❤")
);
Source

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

Source§

fn add_assign(&mut self, rhs: StyledStr<'_>)

Performs the += operation. Read more
Source§

impl<T: Clone> Clone for StyledString<T>

Source§

fn clone(&self) -> StyledString<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for StyledString<T>
where T: Deref<Target = str>,

Source§

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

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

impl<T: Default> Default for StyledString<T>

Source§

fn default() -> StyledString<T>

Returns the “default value” for a type. Read more
Source§

impl<T> Display for StyledString<T>
where T: Deref<Target = str>,

Source§

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

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

impl<'a> Extend<StyledStr<'a>> for StyledString

Source§

fn extend<I: IntoIterator<Item = StyledStr<'a>>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<'a, T> From<StyledStr<'a>> for StyledString<T>
where T: From<&'a str> + Deref<Target = str>,

Source§

fn from(str: StyledStr<'a>) -> Self

Converts to this type from the input type.
Source§

impl From<StyledString> for StyledStringBuilder

Source§

fn from(string: StyledString) -> Self

Converts to this type from the input type.
Source§

impl<'a> FromIterator<StyledStr<'a>> for StyledString

Source§

fn from_iter<I: IntoIterator<Item = StyledStr<'a>>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl FromStr for StyledString

Source§

type Err = ParseError

The associated error which can be returned from parsing.
Source§

fn from_str(raw: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<T> PartialEq<StyledStr<'_>> for StyledString<T>
where T: Deref<Target = str>,

Source§

fn eq(&self, other: &StyledStr<'_>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> PartialEq<StyledString<T>> for StyledStr<'_>
where T: Deref<Target = str>,

Source§

fn eq(&self, other: &StyledString<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialEq> PartialEq for StyledString<T>

Source§

fn eq(&self, other: &StyledString<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> StructuralPartialEq for StyledString<T>

Auto Trait Implementations§

§

impl<T> Freeze for StyledString<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for StyledString<T>
where T: RefUnwindSafe,

§

impl<T> Send for StyledString<T>
where T: Send,

§

impl<T> Sync for StyledString<T>
where T: Sync,

§

impl<T> Unpin for StyledString<T>
where T: Unpin,

§

impl<T> UnwindSafe for StyledString<T>
where T: UnwindSafe,

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.