Struct Style

pub struct Style { /* private fields */ }

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

impl Style

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

Get the foreground color

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

Get the background color

const fn get_underline_color(self) -> Option<Color>
const fn get_effects(self) -> Effects
const fn is_plain(self) -> bool

Check if no styling is enabled

impl Style

const fn bold(self) -> Self

Apply bold effect

Examples

let style = anstyle::Style::new().bold();
const fn dimmed(self) -> Self

Apply dimmed effect

Examples

let style = anstyle::Style::new().dimmed();
const fn italic(self) -> Self

Apply italic effect

Examples

let style = anstyle::Style::new().italic();
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();
const fn invert(self) -> Self

Apply invert effect

Examples

let style = anstyle::Style::new().invert();
const fn hidden(self) -> Self

Apply hidden effect

Examples

let style = anstyle::Style::new().hidden();
const fn strikethrough(self) -> Self

Apply strikethrough effect

Examples

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

impl Style

const fn new() -> Self

No effects enabled

Examples

let style = anstyle::Style::new();
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()));
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()));
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()));
const fn effects(self, effects: Effects) -> Self

Set text effects

Examples

let style = anstyle::Style::new().effects(anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE);
fn render(self) -> impl Display + Copy

Render the ANSI code

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

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

Write the ANSI code

fn render_reset(self) -> impl Display + Copy

Renders the relevant [Reset][crate::Reset] code

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

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

Write the relevant [Reset][crate::Reset] code

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

Trait Implementations

impl BitOr<Effects> for Style

type Output = Style;
fn bitor(self, rhs: Effects) -> Self

impl BitOrAssign<Effects> for Style

fn bitor_assign(&mut self, other: Effects)

impl Clone for Style

fn clone(&self) -> Self

impl Copy for Style

impl Debug for Style

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

impl Default for Style

fn default() -> Self

impl Display for Style

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

impl Eq for Style

impl From<Effects> for Style

fn from(effects: Effects) -> Self

impl Hash for Style

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

impl Ord for Style

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

impl PartialEq for Style

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

impl PartialEq<Effects> for Style

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

impl PartialOrd for Style

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

impl StructuralPartialEq for Style

impl Sub<Effects> for Style

type Output = Style;
fn sub(self, other: Effects) -> Self

impl SubAssign<Effects> for Style

fn sub_assign(&mut self, other: Effects)

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

impl<T> Any for Style where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Style where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Style where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for Style where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for Style

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Style where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T> ToString for Style where T: Display + ?Sized,

fn to_string(&self) -> String

impl<T, U> Into<U> for Style where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for Style where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Style where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>