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 (const: unstable) · 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)Source§impl<'a, T> From<StyledStr<'a>> for StyledString<T>
impl<'a, T> From<StyledStr<'a>> for StyledString<T>
Source§impl From<StyledString> for StyledStringBuilder
impl From<StyledString> for StyledStringBuilder
Source§fn from(string: StyledString) -> Self
fn from(string: StyledString) -> Self
Source§impl<'a> FromIterator<StyledStr<'a>> for StyledString
impl<'a> FromIterator<StyledStr<'a>> for StyledString
Source§impl FromStr for StyledString
impl FromStr for StyledString
Source§impl<T: PartialEq> PartialEq for StyledString<T>
impl<T: PartialEq> PartialEq for StyledString<T>
Source§impl<T> PartialEq<StyledStr<'_>> for StyledString<T>
impl<T> PartialEq<StyledStr<'_>> for StyledString<T>
Source§impl<T> PartialEq<StyledString<T>> for StyledStr<'_>
impl<T> PartialEq<StyledString<T>> for StyledStr<'_>
impl<T: PartialEq> 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> UnsafeUnpin for StyledString<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for StyledString<T>where
T: UnwindSafe,
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<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);