Skip to main content

Style

Struct Style 

Source
pub struct Style(/* private fields */);
Expand description

A combinatorial style such as bold, italics, dimmed, etc.

§Creation

Style::default() returns a Style with no style switches activated and is the default method of creating a plain Style.

§Style from a set of Styless / Styles iterator

Style implements FromIter<Styles> which means that it is possible to do the following:

let style = Style::from_iter([Styles::Bold, Styles::Italic, Styles::Strikethrough]);
for styles in [Styles::Bold, Styles::Italic, Styles::Strikethrough] {
    assert!(style.contains(styles));
}

As you can see, this is a good thing to keep in mind, although for most cases, where you’re not setting styles dynamically and are simply creating a pre-defined set of styles, using Default and then using the builder-style methods is likely prettier.

let many_styles = Style::default()
    .bold()
    .underline()
    .italic()
    .blink();

§Implementation of logical bitwise operators

Style implements bitwise logical operations that operate on the held style switches collectively. By far the most common and useful is the bitwise ‘or’ operator | which combines two styles, merging their combined styles into one. Example:

let only_bold = Style::from(Styles::Bold);
// This line is actually an example of `Styles`'s bitwise logic impls but still.
let underline_and_italic = Styles::Underline | Styles::Italic;
let all_three = only_bold | underline_and_italic;

assert!(all_three.contains(Styles::Bold)
    && all_three.contains(Styles::Underline)
    && all_three.contains(Styles::Italic));

This functionality also allows for easily turning off styles of one Styles using another by combining the & and ! operators.

let mut very_loud_style = Style::default()
    .bold()
    .underline()
    .italic()
    .strikethrough()
    .hidden();

// Oops! Some of those should not be in there!
// This Style now has all styles _except_ the two we don't want
// (hidden and strikethough).
let remove_mask =
    !Style::from_iter([Styles::Hidden, Styles::Strikethrough]);
very_loud_style &= remove_mask;

// `very_loud_style` no longer contains the undesired style
// switches...
assert!(!very_loud_style.contains(Styles::Hidden)
    && !very_loud_style.contains(Styles::Strikethrough));
// ...but it retains everything else!
assert!(very_loud_style.contains(Styles::Bold));

Implementations§

Source§

impl Style

Source

pub fn contains(self, style: Styles) -> bool

Check if the current style has one of Styles switched on.

let colored = "".bold().italic();
assert_eq!(colored.style.contains(Styles::Bold), true);
assert_eq!(colored.style.contains(Styles::Italic), true);
assert_eq!(colored.style.contains(Styles::Dimmed), false);
Source

pub fn add(&mut self, two: Styles)

Adds the two style switch to this Style.

let cstr = "".red().bold();
let mut style = cstr.style;
style.add(Styles::Italic);
let mut cstr2 = "".blue();
cstr2.style = style;

assert!(cstr2.style.contains(Styles::Bold));
assert!(cstr2.style.contains(Styles::Italic));
assert_eq!(cstr2.fgcolor, Some(Color::Blue));
Source

pub fn remove(&mut self, two: Styles)

Turns off a style switch.

use colored::*;
let cstr = "".red().bold().italic();
let mut style = cstr.style;
style.remove(Styles::Italic);
let mut cstr2 = "".blue();
cstr2.style = style;
assert!(cstr2.style.contains(Styles::Bold));
assert!(!cstr2.style.contains(Styles::Italic));
assert_eq!(cstr2.fgcolor, Some(Color::Blue));
Source

pub fn bold(self) -> Self

Makes this Style include Bold.

Source

pub fn dimmed(self) -> Self

Makes this Style include Dimmed.

Source

pub fn underline(self) -> Self

Makes this Style include Underline.

Source

pub fn reversed(self) -> Self

Makes this Style include Reversed.

Source

pub fn italic(self) -> Self

Makes this Style include Italic.

Makes this Style include Blink.

Source

pub fn hidden(self) -> Self

Makes this Style include Hidden.

Source

pub fn strikethrough(self) -> Self

Makes this Style include Strikethrough.

Trait Implementations§

Source§

impl BitAnd for Style

Source§

type Output = Style

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Style) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAnd<&Style> for &Style

Source§

type Output = <Style as BitAnd>::Output

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: &Style) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAnd<&Style> for &Styles

Source§

type Output = <Styles as BitAnd>::Output

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: &Style) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAnd<&Style> for Style

Source§

type Output = <Style as BitAnd>::Output

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: &Style) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAnd<&Style> for Styles

Source§

type Output = <Styles as BitAnd>::Output

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: &Style) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAnd<&Styles> for &Style

Source§

type Output = <Style as BitAnd>::Output

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: &Styles) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAnd<&Styles> for Style

Source§

type Output = <Style as BitAnd>::Output

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: &Styles) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAnd<Style> for &Style

Source§

type Output = <Style as BitAnd>::Output

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Style) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAnd<Style> for &Styles

Source§

type Output = <Styles as BitAnd>::Output

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Style) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAnd<Style> for Styles

Source§

type Output = Style

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Style) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAnd<Styles> for &Style

Source§

type Output = <Style as BitAnd>::Output

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Styles) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAnd<Styles> for Style

Source§

type Output = Style

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Styles) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAndAssign for Style

Source§

fn bitand_assign(&mut self, other: Style)

Performs the &= operation. Read more
Source§

impl BitAndAssign<&Style> for Style

Source§

fn bitand_assign(&mut self, other: &Style)

Performs the &= operation. Read more
Source§

impl BitAndAssign<&Styles> for Style

Source§

fn bitand_assign(&mut self, other: &Styles)

Performs the &= operation. Read more
Source§

impl BitAndAssign<Styles> for Style

Source§

fn bitand_assign(&mut self, other: Styles)

Performs the &= operation. Read more
Source§

impl BitOr for Style

Source§

type Output = Style

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Style) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr<&Style> for &Style

Source§

type Output = <Style as BitOr>::Output

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: &Style) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr<&Style> for &Styles

Source§

type Output = <Styles as BitOr>::Output

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: &Style) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr<&Style> for Style

Source§

type Output = <Style as BitOr>::Output

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: &Style) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr<&Style> for Styles

Source§

type Output = <Styles as BitOr>::Output

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: &Style) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr<&Styles> for &Style

Source§

type Output = <Style as BitOr>::Output

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: &Styles) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr<&Styles> for Style

Source§

type Output = <Style as BitOr>::Output

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: &Styles) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr<Style> for &Style

Source§

type Output = <Style as BitOr>::Output

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Style) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr<Style> for &Styles

Source§

type Output = <Styles as BitOr>::Output

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Style) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr<Style> for Styles

Source§

type Output = Style

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Style) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr<Styles> for &Style

Source§

type Output = <Style as BitOr>::Output

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Styles) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr<Styles> for Style

Source§

type Output = Style

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Styles) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOrAssign for Style

Source§

fn bitor_assign(&mut self, other: Style)

Performs the |= operation. Read more
Source§

impl BitOrAssign<&Style> for Style

Source§

fn bitor_assign(&mut self, other: &Style)

Performs the |= operation. Read more
Source§

impl BitOrAssign<&Styles> for Style

Source§

fn bitor_assign(&mut self, other: &Styles)

Performs the |= operation. Read more
Source§

impl BitOrAssign<Styles> for Style

Source§

fn bitor_assign(&mut self, other: Styles)

Performs the |= operation. Read more
Source§

impl BitXor for Style

Source§

type Output = Style

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Style) -> Self::Output

Performs the ^ operation. Read more
Source§

impl BitXor<&Style> for &Style

Source§

type Output = <Style as BitXor>::Output

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: &Style) -> Self::Output

Performs the ^ operation. Read more
Source§

impl BitXor<&Style> for &Styles

Source§

type Output = <Styles as BitXor>::Output

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: &Style) -> Self::Output

Performs the ^ operation. Read more
Source§

impl BitXor<&Style> for Style

Source§

type Output = <Style as BitXor>::Output

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: &Style) -> Self::Output

Performs the ^ operation. Read more
Source§

impl BitXor<&Style> for Styles

Source§

type Output = <Styles as BitXor>::Output

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: &Style) -> Self::Output

Performs the ^ operation. Read more
Source§

impl BitXor<&Styles> for &Style

Source§

type Output = <Style as BitXor>::Output

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: &Styles) -> Self::Output

Performs the ^ operation. Read more
Source§

impl BitXor<&Styles> for Style

Source§

type Output = <Style as BitXor>::Output

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: &Styles) -> Self::Output

Performs the ^ operation. Read more
Source§

impl BitXor<Style> for &Style

Source§

type Output = <Style as BitXor>::Output

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Style) -> Self::Output

Performs the ^ operation. Read more
Source§

impl BitXor<Style> for &Styles

Source§

type Output = <Styles as BitXor>::Output

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Style) -> Self::Output

Performs the ^ operation. Read more
Source§

impl BitXor<Style> for Styles

Source§

type Output = Style

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Style) -> Self::Output

Performs the ^ operation. Read more
Source§

impl BitXor<Styles> for &Style

Source§

type Output = <Style as BitXor>::Output

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Styles) -> Self::Output

Performs the ^ operation. Read more
Source§

impl BitXor<Styles> for Style

Source§

type Output = Style

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Styles) -> Self::Output

Performs the ^ operation. Read more
Source§

impl BitXorAssign for Style

Source§

fn bitxor_assign(&mut self, other: Style)

Performs the ^= operation. Read more
Source§

impl BitXorAssign<&Style> for Style

Source§

fn bitxor_assign(&mut self, other: &Style)

Performs the ^= operation. Read more
Source§

impl BitXorAssign<&Styles> for Style

Source§

fn bitxor_assign(&mut self, other: &Styles)

Performs the ^= operation. Read more
Source§

impl BitXorAssign<Styles> for Style

Source§

fn bitxor_assign(&mut self, other: Styles)

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

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

impl Default for Style

Source§

fn default() -> Self

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

impl Eq for Style

Source§

impl From<&Styles> for Style

Source§

fn from(value: &Styles) -> Self

Converts to this type from the input type.
Source§

impl From<Styles> for Style

Source§

fn from(value: Styles) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<Styles> for Style

Source§

fn from_iter<T: IntoIterator<Item = Styles>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl Not for &Style

Source§

type Output = Style

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl Not for Style

Source§

type Output = Style

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
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 (const: unstable) · 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 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 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, 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.