Struct Style

Source
pub struct Style {
    pub foreground: Option<Colour>,
    pub background: Option<Colour>,
    pub is_bold: bool,
    pub is_dimmed: bool,
    pub is_italic: bool,
    pub is_underline: bool,
    pub is_blink: bool,
    pub is_reverse: bool,
    pub is_hidden: bool,
    pub is_strikethrough: bool,
}
Expand description

A style is a collection of properties that can format a string using ANSI escape codes.

§Examples

use yansi_term::{Style, Colour};

let style = Style::new().bold().on(Colour::Black);
println!("{}", style.paint("Bold on black"));

Fields§

§foreground: Option<Colour>

The style’s foreground colour, if it has one.

§background: Option<Colour>

The style’s background colour, if it has one.

§is_bold: bool

Whether this style is bold.

§is_dimmed: bool

Whether this style is dimmed.

§is_italic: bool

Whether this style is italic.

§is_underline: bool

Whether this style is underlined.

§is_blink: bool

Whether this style is blinking.

§is_reverse: bool

Whether this style has reverse colours.

§is_hidden: bool

Whether this style is hidden.

§is_strikethrough: bool

Whether this style is struckthrough.

Implementations§

Source§

impl Style

Source

pub fn write_prefix(&self, f: &mut Formatter<'_>) -> Result<bool, Error>

Write any bytes that go before a piece of text to the given writer.

Source

pub fn write_reset(f: &mut Formatter<'_>) -> Result

Write any bytes that go after a piece of text to the given writer.

Source§

impl Style

Source

pub fn new() -> Style

Creates a new Style with no properties set.

§Examples
use yansi_term::Style;

let style = Style::new();
println!("{}", style.paint("hi"));
Source

pub fn bold(self) -> Self

Returns a Style with the bold property set.

§Examples
use yansi_term::Style;

let style = Style::new().bold();
println!("{}", style.paint("hey"));
Source

pub fn dimmed(self) -> Self

Returns a Style with the dimmed property set.

§Examples
use yansi_term::Style;

let style = Style::new().dimmed();
println!("{}", style.paint("sup"));
Source

pub fn italic(self) -> Self

Returns a Style with the italic property set.

§Examples
use yansi_term::Style;

let style = Style::new().italic();
println!("{}", style.paint("greetings"));
Source

pub fn underline(self) -> Self

Returns a Style with the underline property set.

§Examples
use yansi_term::Style;

let style = Style::new().underline();
println!("{}", style.paint("salutations"));

Returns a Style with the blink property set.

§Examples
use yansi_term::Style;

let style = Style::new().blink();
println!("{}", style.paint("wazzup"));
Source

pub fn reverse(self) -> Self

Returns a Style with the reverse property set.

§Examples
use yansi_term::Style;

let style = Style::new().reverse();
println!("{}", style.paint("aloha"));
Source

pub fn hidden(self) -> Self

Returns a Style with the hidden property set.

§Examples
use yansi_term::Style;

let style = Style::new().hidden();
println!("{}", style.paint("ahoy"));
Source

pub fn strikethrough(self) -> Self

Returns a Style with the strikethrough property set.

§Examples
use yansi_term::Style;

let style = Style::new().strikethrough();
println!("{}", style.paint("yo"));
Source

pub fn fg(self, foreground: Colour) -> Self

Returns a Style with the foreground colour property set.

§Examples
use yansi_term::{Style, Colour};

let style = Style::new().fg(Colour::Yellow);
println!("{}", style.paint("hi"));
Source

pub fn on(self, background: Colour) -> Self

Returns a Style with the background colour property set.

§Examples
use yansi_term::{Style, Colour};

let style = Style::new().on(Colour::Blue);
println!("{}", style.paint("eyyyy"));
Source

pub fn is_plain(&self) -> bool

Return true if this Style has no actual styles, and can be written without any control characters.

§Examples
use yansi_term::Style;

assert_eq!(true,  Style::default().is_plain());
assert_eq!(false, Style::default().bold().is_plain());
Source§

impl Style

Source

pub fn paint<'a>(self, input: &'a str) -> impl Display + 'a

Paints the given text with this style

Source

pub fn paint_fn<F: FnOnce(&mut Formatter<'_>) -> Result>( self, f: F, ) -> impl Display

Paints the given format function with this style

Trait Implementations§

Source§

impl Clone for Style

Source§

fn clone(&self) -> Style

Returns a duplicate 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 From<Colour> for Style

Source§

fn from(colour: Colour) -> Style

You can turn a Colour into a Style with the foreground colour set with the From trait.

use yansi_term::{Style, Colour};
let green_foreground = Style::default().fg(Colour::Green);
assert_eq!(green_foreground, Colour::Green.normal());
assert_eq!(green_foreground, Colour::Green.into());
assert_eq!(green_foreground, Style::from(Colour::Green));
Source§

impl PartialEq for Style

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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