Struct yansi::Style

source ·
#[repr(packed(1))]
pub struct Style { /* private fields */ }
Expand description

Represents a set of styling options.

See the crate level documentation for usage information.

Method Glossary

The Style structure exposes many methods for convenience. The majority of these methods are shared with Paint.

Foreground Color Constructors

Return a new Style structure with a foreground color applied.

Setters

Set a style property on a given Style structure.

These methods can be chained:

use yansi::{Style, Color::{Red, Magenta}};

Style::new(Red).bg(Magenta).underline().invert().italic().dimmed().bold();

Converters

Convert a Style into another structure.

Getters

Return information about a Style structure.

Raw Formatters

Write the raw ANSI codes for a given Style to any fmt::Write.

Implementations§

source§

impl Style

source

pub fn new(color: Color) -> Style

Default style with the foreground set to color and no other set properties.

use yansi::Style;

let plain = Style::default();
assert_eq!(plain, Style::default());
source

pub fn fg(self, color: Color) -> Style

Sets the foreground to color.

use yansi::{Color, Style};

let red_fg = Style::default().fg(Color::Red);
source

pub fn bg(self, color: Color) -> Style

Sets the background to color.

use yansi::{Color, Style};

let red_bg = Style::default().bg(Color::Red);
source

pub fn mask(self) -> Style

Sets self to be masked.

An item with masked styling is not written out when painting is disabled during Display or Debug invocations. When painting is enabled, masking has no effect.

use yansi::Style;

let masked = Style::default().mask();

// "Whoops! " will only print when coloring is enabled.
println!("{}Something happened.", masked.paint("Whoops! "));
source

pub fn wrap(self) -> Style

Sets self to be wrapping.

A wrapping Style converts all color resets written out by the internal value to the styling of itself. This allows for seamless color wrapping of other colored text.

Performance

In order to wrap an internal value, the internal value must first be written out to a local buffer and examined. As a result, displaying a wrapped value is likely to result in a heap allocation and copy.

use yansi::{Paint, Style, Color};

let inner = format!("{} and {}", Paint::red("Stop"), Paint::green("Go"));
let wrapping = Style::new(Color::Blue).wrap();

// 'Hey!' will be unstyled, "Stop" will be red, "and" will be blue, and
// "Go" will be green. Without a wrapping `Paint`, "and" would be
// unstyled.
println!("Hey! {}", wrapping.paint(inner));
source

pub fn bold(self) -> Style

Enables the bold style on self.

use yansi::Paint;

println!("Using bold: {}", Paint::new("hi").bold());
source

pub fn dimmed(self) -> Style

Enables the dimmed style on self.

use yansi::Paint;

println!("Using dimmed: {}", Paint::new("hi").dimmed());
source

pub fn italic(self) -> Style

Enables the italic style on self.

use yansi::Paint;

println!("Using italic: {}", Paint::new("hi").italic());
source

pub fn underline(self) -> Style

Enables the underline style on self.

use yansi::Paint;

println!("Using underline: {}", Paint::new("hi").underline());

Enables the blink style on self.

use yansi::Paint;

println!("Using blink: {}", Paint::new("hi").blink());
source

pub fn invert(self) -> Style

Enables the invert style on self.

use yansi::Paint;

println!("Using invert: {}", Paint::new("hi").invert());
source

pub fn hidden(self) -> Style

Enables the hidden style on self.

use yansi::Paint;

println!("Using hidden: {}", Paint::new("hi").hidden());
source

pub fn strikethrough(self) -> Style

Enables the strikethrough style on self.

use yansi::Paint;

println!("Using strikethrough: {}", Paint::new("hi").strikethrough());
source

pub fn paint<T>(self, item: T) -> Paint<T>

Constructs a new Paint structure that encapsulates item with the style set to self.

use yansi::{Style, Color};

let alert = Style::new(Color::Red).bold().underline();
println!("Alert: {}", alert.paint("This thing happened!"));
source

pub fn fg_color(&self) -> Color

Returns the foreground color of self.

use yansi::{Style, Color};

let plain = Style::default();
assert_eq!(plain.fg_color(), Color::Unset);

let red = plain.fg(Color::Red);
assert_eq!(red.fg_color(), Color::Red);
source

pub fn bg_color(&self) -> Color

Returns the foreground color of self.

use yansi::{Style, Color};

let plain = Style::default();
assert_eq!(plain.bg_color(), Color::Unset);

let white = plain.bg(Color::White);
assert_eq!(white.bg_color(), Color::White);
source

pub fn is_masked(&self) -> bool

Returns true if self is masked.

use yansi::Style;

let plain = Style::default();
assert!(!plain.is_masked());

let masked = plain.mask();
assert!(masked.is_masked());
source

pub fn is_wrapping(&self) -> bool

Returns true if self is wrapping.

use yansi::Style;

let plain = Style::default();
assert!(!plain.is_wrapping());

let wrapping = plain.wrap();
assert!(wrapping.is_wrapping());
source

pub fn is_bold(&self) -> bool

Returns true if the bold property is set on self.

use yansi::Style;

let plain = Style::default();
assert!(!plain.is_bold());

let styled = plain.bold();
assert!(styled.is_bold());
source

pub fn is_dimmed(&self) -> bool

Returns true if the dimmed property is set on self.

use yansi::Style;

let plain = Style::default();
assert!(!plain.is_dimmed());

let styled = plain.dimmed();
assert!(styled.is_dimmed());
source

pub fn is_italic(&self) -> bool

Returns true if the italic property is set on self.

use yansi::Style;

let plain = Style::default();
assert!(!plain.is_italic());

let styled = plain.italic();
assert!(styled.is_italic());
source

pub fn is_underline(&self) -> bool

Returns true if the underline property is set on self.

use yansi::Style;

let plain = Style::default();
assert!(!plain.is_underline());

let styled = plain.underline();
assert!(styled.is_underline());

Returns true if the blink property is set on self.

use yansi::Style;

let plain = Style::default();
assert!(!plain.is_blink());

let styled = plain.blink();
assert!(styled.is_blink());
source

pub fn is_invert(&self) -> bool

Returns true if the invert property is set on self.

use yansi::Style;

let plain = Style::default();
assert!(!plain.is_invert());

let styled = plain.invert();
assert!(styled.is_invert());
source

pub fn is_hidden(&self) -> bool

Returns true if the hidden property is set on self.

use yansi::Style;

let plain = Style::default();
assert!(!plain.is_hidden());

let styled = plain.hidden();
assert!(styled.is_hidden());
source

pub fn is_strikethrough(&self) -> bool

Returns true if the strikethrough property is set on self.

use yansi::Style;

let plain = Style::default();
assert!(!plain.is_strikethrough());

let styled = plain.strikethrough();
assert!(styled.is_strikethrough());
source

pub fn fmt_prefix(&self, f: &mut dyn Write) -> Result

Writes the ANSI code prefix for the currently set styles.

This method is intended to be used inside of fmt::Display and fmt::Debug implementations for custom or specialized use-cases. Most users should use Paint for all painting needs.

This method writes the ANSI code prefix irrespective of whether painting is currently enabled or disabled. To write the prefix only if painting is enabled, condition a call to this method on Paint::is_enabled().

Example
use std::fmt;
use yansi::Style;

struct CustomItem {
    item: u32,
    style: Style
}

impl fmt::Display for CustomItem {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.style.fmt_prefix(f)?;
        write!(f, "number: {}", self.item)?;
        self.style.fmt_suffix(f)
    }
}
source

pub fn fmt_suffix(&self, f: &mut dyn Write) -> Result

Writes the ANSI code suffix for the currently set styles.

This method is intended to be used inside of fmt::Display and fmt::Debug implementations for custom or specialized use-cases. Most users should use Paint for all painting needs.

This method writes the ANSI code suffix irrespective of whether painting is currently enabled or disabled. To write the suffix only if painting is enabled, condition a call to this method on Paint::is_enabled().

Example
use std::fmt;
use yansi::Style;

struct CustomItem {
    item: u32,
    style: Style
}

impl fmt::Display for CustomItem {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.style.fmt_prefix(f)?;
        write!(f, "number: {}", self.item)?;
        self.style.fmt_suffix(f)
    }
}

Trait Implementations§

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 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 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 Copy for Style

source§

impl Eq for Style

source§

impl StructuralEq for Style

Auto Trait Implementations§

§

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