Struct colored::style::Style

source ·
pub struct Style(u8);
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));

Tuple Fields§

§0: u8

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(crate) fn to_str(self) -> String

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<&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 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 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 BitAndAssign for Style

source§

fn bitand_assign(&mut self, other: Style)

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

source§

fn bitor_assign(&mut self, other: Style)

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

source§

fn bitxor_assign(&mut self, other: Style)

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

Returns the “default value” for a type. Read more
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 · 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 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> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.