Skip to main content

## Style

# Struct Style

Source

    pub struct Style { /* private fields */ }

Expand description

ANSI Text styling

You can print a `Style` to render the corresponding ANSI code. Using the alternate flag `#` will render the ANSI reset code, if needed. Together, this makes it convenient to render styles using inline format arguments.

## §Examples

    let style = anstyle::Style::new().bold();

    let value = 42;
    println!("{style}{value}{style:#}");

## Implementations§

Source§

### impl Style

#### §Core

Source

#### pub const fn new() -> Style

No effects enabled

##### §Examples

    let style = anstyle::Style::new();

Source

#### pub const fn fg_color(self, fg: Option<Color>) -> Style

Set foreground color

##### §Examples

    let style = anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Red.into()));

Source

#### pub const fn bg_color(self, bg: Option<Color>) -> Style

Set background color

##### §Examples

    let style = anstyle::Style::new().bg_color(Some(anstyle::AnsiColor::Red.into()));

Source

#### pub const fn underline_color(self, underline: Option<Color>) -> Style

Set underline color

##### §Examples

    let style = anstyle::Style::new().underline_color(Some(anstyle::AnsiColor::Red.into()));

Source

#### pub const fn effects(self, effects: Effects) -> Style

Set text effects

##### §Examples

    let style = anstyle::Style::new().effects(anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE);

Source

#### pub fn render(self) -> impl Display + Copy

Render the ANSI code

`Style` also implements `Display` directly, so calling this method is optional.

Source

#### pub fn write_to(self, write: &mut dyn Write) -> Result<(), Error>

Available on crate feature `std` only.

Write the ANSI code

Source

#### pub fn render_reset(self) -> impl Display + Copy

Renders the relevant `Reset` code

Unlike `Reset::render`, this will elide the code if there is nothing to reset.

Source

#### pub fn write_reset_to(self, write: &mut dyn Write) -> Result<(), Error>

Available on crate feature `std` only.

Write the relevant `Reset` code

Unlike `Reset::render`, this will elide the code if there is nothing to reset.

Source§

### impl Style

#### §Convenience

Source

#### pub const fn bold(self) -> Style

Apply `bold` effect

##### §Examples

    let style = anstyle::Style::new().bold();

Source

#### pub const fn dimmed(self) -> Style

Apply `dimmed` effect

##### §Examples

    let style = anstyle::Style::new().dimmed();

Source

#### pub const fn italic(self) -> Style

Apply `italic` effect

##### §Examples

    let style = anstyle::Style::new().italic();

Source

#### pub const fn underline(self) -> Style

Apply `underline` effect

##### §Examples

    let style = anstyle::Style::new().underline();

Source

#### pub const fn blink(self) -> Style

Apply `blink` effect

##### §Examples

    let style = anstyle::Style::new().blink();

Source

#### pub const fn invert(self) -> Style

Apply `invert` effect

##### §Examples

    let style = anstyle::Style::new().invert();

Source

#### pub const fn hidden(self) -> Style

Apply `hidden` effect

##### §Examples

    let style = anstyle::Style::new().hidden();

Source

#### pub const fn strikethrough(self) -> Style

Apply `strikethrough` effect

##### §Examples

    let style = anstyle::Style::new().strikethrough();

Source§

### impl Style

#### §Reflection

Source

#### pub const fn get_fg_color(self) -> Option<Color>

Get the foreground color

Source

#### pub const fn get_bg_color(self) -> Option<Color>

Get the background color

Source

#### pub const fn get_underline_color(self) -> Option<Color>

Source

#### pub const fn get_effects(self) -> Effects

Source

#### pub const fn is_plain(self) -> bool

Check if no styling is enabled

## Trait Implementations§

Source§

### impl BitOr<Effects> for Style

#### §Examples

    let style = anstyle::Style::new() | anstyle::Effects::BOLD.into();

Source§

#### type Output = Style

The resulting type after applying the `|` operator.
Source§

#### fn bitor(self, rhs: Effects) -> Style

Performs the `|` operation. Read more
Source§

### impl BitOrAssign<Effects> for Style

#### §Examples

    let mut style = anstyle::Style::new();
    style |= anstyle::Effects::BOLD.into();

Source§

#### fn bitor_assign(&mut self, other: Effects)

Performs the `|=` operation. Read more
Source§

### impl Clone for Style

Source§

#### fn clone(&self) -> Style

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from `source`. Read more
Source§

### impl Copy for Style

Source§

### impl Debug for Style

Source§

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

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

### impl Default for Style

Source§

#### fn default() -> Style

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

### impl Display for Style

Source§

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

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

### impl Eq for Style

Source§

### impl From<Effects> for Style

#### §Examples

    let style: anstyle::Style = anstyle::Effects::BOLD.into();

Source§

#### fn from(effects: Effects) -> Style

Converts to this type from the input type.
Source§

### impl Hash for Style

Source§

#### fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given `Hasher`. Read more
1.3.0 · Source§

#### fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given `Hasher`. Read more
Source§

### impl Ord for Style

Source§

#### fn cmp(&self, other: &Style) -> Ordering

This method returns an `Ordering` between `self` and `other`. Read more
1.21.0 (const: unstable) · Source§

#### fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

#### fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

#### fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

### impl PartialEq for Style

Source§

#### fn eq(&self, other: &Style) -> bool

Equality operator `==`. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator `!=`. Read more
Source§

### impl PartialEq<Effects> for Style

#### §Examples

    let effects = anstyle::Effects::BOLD;
    assert_eq!(anstyle::Style::new().effects(effects), effects);
    assert_ne!(anstyle::Effects::UNDERLINE | effects, effects);
    assert_ne!(anstyle::RgbColor(0, 0, 0).on_default() | effects, effects);

Source§

#### fn eq(&self, other: &Effects) -> bool

Equality operator `==`. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator `!=`. Read more
Source§

### impl PartialOrd for Style

Source§

#### fn partial_cmp(&self, other: &Style) -> Option<Ordering>

This method returns an ordering between `self` and `other` values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for `self` and `other`) and is used by the `<` operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for `self` and `other`) and is used by the `<=` operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for `self` and `other`) and is used by the `>` operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for `self` and `other`) and is used by the `>=` operator. Read more
Source§

### impl StructuralPartialEq for Style

Source§

### impl Sub<Effects> for Style

#### §Examples

    let style = anstyle::Style::new().bold().underline() - anstyle::Effects::BOLD.into();

Source§

#### type Output = Style

The resulting type after applying the `-` operator.
Source§

#### fn sub(self, other: Effects) -> Style

Performs the `-` operation. Read more
Source§

### impl SubAssign<Effects> for Style

#### §Examples

    let mut style = anstyle::Style::new().bold().underline();
    style -= anstyle::Effects::BOLD.into();

Source§

#### fn sub_assign(&mut self, other: Effects)

Performs the `-=` operation. Read more

## Auto Trait Implementations§

§

### impl Freeze for Style

§

### impl RefUnwindSafe for Style

§

### impl Send for Style

§

### impl Sync for Style

§

### impl Unpin for Style

§

### impl UnsafeUnpin for Style

§

### impl UnwindSafe for Style

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