Struct anstyle::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() -> Self

No effects enabled

§Examples
let style = anstyle::Style::new();
source

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

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>) -> Self

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>) -> Self

Set underline color

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

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

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<()>

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<()>

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) -> Self

Apply bold effect

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

pub const fn dimmed(self) -> Self

Apply dimmed effect

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

pub const fn italic(self) -> Self

Apply italic effect

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

pub const fn underline(self) -> Self

Apply underline effect

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

Apply blink effect

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

pub const fn invert(self) -> Self

Apply invert effect

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

pub const fn hidden(self) -> Self

Apply hidden effect

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

pub const fn strikethrough(self) -> Self

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();
§

type Output = Style

The resulting type after applying the | operator.
source§

fn bitor(self, rhs: Effects) -> Self

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 copy 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 Debug for Style

source§

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

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

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

impl From<Effects> for Style

§Examples

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

fn from(effects: Effects) -> Self

Converts to this type from the input type.
source§

impl Hash for Style

source§

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

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 · source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · source§

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

Restrict a value to a certain interval. 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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq for Style

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Sub<Effects> for Style

§Examples

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

type Output = Style

The resulting type after applying the - operator.
source§

fn sub(self, other: Effects) -> Self

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
source§

impl Copy for Style

source§

impl Eq for Style

source§

impl StructuralPartialEq for Style

Auto Trait Implementations§

§

impl Freeze for Style

§

impl RefUnwindSafe for Style

§

impl Send for Style

§

impl Sync for Style

§

impl Unpin 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> 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,

§

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§

default 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>,

§

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

§

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.