StyledString

Struct StyledString 

Source
pub struct StyledString { /* private fields */ }
Expand description

A String containing Ansi styles, which can be overridden.

Instances of StyledString:

Note: this library provides a similarly-named Styled<T>, which offers the same functionality as StyledString, but stores an unformatted std::fmt::Display rather than a formatted String. This may be less convenient for certain use cases, but is more efficient since it avoids the runtime overhead of StyledString.

§Discussion

When Ansi styles are formatted into codes insides a String, the references to those styles are lost and they are “baked-into” the String’s contents. This means one loses the ability to subsequently override those styles by nesting that String inside a parent Styled<T>:

§Example

use ansiconst::styled_format_args;

let red: String = styled_format_args!(Red, "Red").to_string();

assert_eq!("\x1B[31mRed\x1B[39m", red);

let sadly_still_red = styled_format_args!(Ansi::no_ansi(), "{}", red).to_string();

assert_eq!("\x1B[31mRed\x1B[39m", sadly_still_red);

StyledString solves this problem by keeping track of the contained Ansi styles inside a formatted String, such that those styles can subsequently be overridden:

§Example

use ansiconst::{styled_format, styled_format_args, StyledString};

let red: StyledString = styled_format!(Red, "Red");

assert_eq!("\x1B[31mRed\x1B[39m", red.to_string());

let no_longer_red = styled_format_args!(Ansi::no_ansi(), "{}", red).to_string();

assert_eq!("Red", no_longer_red);

The flexibility afforded by StyledString comes at the expense of additional runtime overhead. Therefore in performance critical situations, it is better not to create StyledStrings, but instead to use styled_format_args! to style output at the moment when it is displayed.

Like Styled<T>, StyledString uses thread_local! to pass style information between nested styles and outer styles during formatting.

Trait Implementations§

Source§

impl Display for StyledString

Source§

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

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

impl<T: Display> From<&Styled<T>> for StyledString

Source§

fn from(styled: &Styled<T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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