Style

Struct Style 

Source
pub struct Style {
    pub fg: Option<Color>,
    pub bg: Option<Color>,
    pub underline_color: Option<Color>,
    pub add_modifier: Modifier,
    pub sub_modifier: Modifier,
}
Expand description

Style lets you control the main characteristics of the displayed elements.

use ratatui_core::style::{Color, Modifier, Style};

Style::default()
    .fg(Color::Black)
    .bg(Color::Green)
    .add_modifier(Modifier::ITALIC | Modifier::BOLD);

Styles can also be created with a shorthand notation.

use ratatui_core::style::{Style, Stylize};

Style::new().black().on_green().italic().bold();

For more information about the style shorthands, see the Stylize trait.

We implement conversions from Color and Modifier to Style so you can use them anywhere that accepts Into<Style>.

use ratatui_core::style::{Color, Modifier, Style};
use ratatui_core::text::Line;

Line::styled("hello", Style::new().fg(Color::Red));
// simplifies to
Line::styled("hello", Color::Red);

Line::styled("hello", Style::new().add_modifier(Modifier::BOLD));
// simplifies to
Line::styled("hello", Modifier::BOLD);

Styles represents an incremental change. If you apply the styles S1, S2, S3 to a cell of the terminal buffer, the style of this cell will be the result of the merge of S1, S2 and S3, not just S3.

use ratatui_core::buffer::Buffer;
use ratatui_core::layout::Rect;
use ratatui_core::style::{Color, Modifier, Style};

let styles = [
    Style::default()
        .fg(Color::Blue)
        .add_modifier(Modifier::BOLD | Modifier::ITALIC),
    Style::default()
        .bg(Color::Red)
        .add_modifier(Modifier::UNDERLINED),
    #[cfg(feature = "underline-color")]
    Style::default().underline_color(Color::Green),
    Style::default()
        .fg(Color::Yellow)
        .remove_modifier(Modifier::ITALIC),
];
let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1));
for style in &styles {
    buffer[(0, 0)].set_style(*style);
}
assert_eq!(
    Style {
        fg: Some(Color::Yellow),
        bg: Some(Color::Red),
        #[cfg(feature = "underline-color")]
        underline_color: Some(Color::Green),
        add_modifier: Modifier::BOLD | Modifier::UNDERLINED,
        sub_modifier: Modifier::empty(),
    },
    buffer[(0, 0)].style(),
);

The default implementation returns a Style that does not modify anything. If you wish to reset all properties until that point use Style::reset.

use ratatui_core::buffer::Buffer;
use ratatui_core::layout::Rect;
use ratatui_core::style::{Color, Modifier, Style};

let styles = [
    Style::default()
        .fg(Color::Blue)
        .add_modifier(Modifier::BOLD | Modifier::ITALIC),
    Style::reset().fg(Color::Yellow),
];
let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1));
for style in &styles {
    buffer[(0, 0)].set_style(*style);
}
assert_eq!(
    Style {
        fg: Some(Color::Yellow),
        bg: Some(Color::Reset),
        #[cfg(feature = "underline-color")]
        underline_color: Some(Color::Reset),
        add_modifier: Modifier::empty(),
        sub_modifier: Modifier::empty(),
    },
    buffer[(0, 0)].style(),
);

Fields§

§fg: Option<Color>

The foreground color.

§bg: Option<Color>

The background color.

§underline_color: Option<Color>
Available on crate feature underline-color only.

The underline color.

§add_modifier: Modifier

The modifiers to add.

§sub_modifier: Modifier

The modifiers to remove.

Implementations§

Source§

impl Style

Source

pub const fn new() -> Self

Returns a Style with default properties.

Source

pub const fn reset() -> Self

Returns a Style resetting all properties.

Source

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

Changes the foreground color.

§Examples
use ratatui_core::style::{Color, Style};

let style = Style::default().fg(Color::Blue);
let diff = Style::default().fg(Color::Red);
assert_eq!(style.patch(diff), Style::default().fg(Color::Red));
Source

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

Changes the background color.

§Examples
use ratatui_core::style::{Color, Style};

let style = Style::default().bg(Color::Blue);
let diff = Style::default().bg(Color::Red);
assert_eq!(style.patch(diff), Style::default().bg(Color::Red));
Source

pub const fn underline_color(self, color: Color) -> Self

Available on crate feature underline-color only.

Changes the underline color. The text must be underlined with a modifier for this to work.

This uses a non-standard ANSI escape sequence. It is supported by most terminal emulators, but is only implemented in the crossterm backend and enabled by the underline-color feature flag.

See Wikipedia code 58 and 59 for more information.

§Examples
use ratatui_core::style::{Color, Modifier, Style};

let style = Style::default()
    .underline_color(Color::Blue)
    .add_modifier(Modifier::UNDERLINED);
let diff = Style::default()
    .underline_color(Color::Red)
    .add_modifier(Modifier::UNDERLINED);
assert_eq!(
    style.patch(diff),
    Style::default()
        .underline_color(Color::Red)
        .add_modifier(Modifier::UNDERLINED)
);
Source

pub const fn add_modifier(self, modifier: Modifier) -> Self

Changes the text emphasis.

When applied, it adds the given modifier to the Style modifiers.

§Examples
use ratatui_core::style::{Modifier, Style};

let style = Style::default().add_modifier(Modifier::BOLD);
let diff = Style::default().add_modifier(Modifier::ITALIC);
let patched = style.patch(diff);
assert_eq!(patched.add_modifier, Modifier::BOLD | Modifier::ITALIC);
assert_eq!(patched.sub_modifier, Modifier::empty());
Source

pub const fn remove_modifier(self, modifier: Modifier) -> Self

Changes the text emphasis.

When applied, it removes the given modifier from the Style modifiers.

§Examples
use ratatui_core::style::{Modifier, Style};

let style = Style::default().add_modifier(Modifier::BOLD | Modifier::ITALIC);
let diff = Style::default().remove_modifier(Modifier::ITALIC);
let patched = style.patch(diff);
assert_eq!(patched.add_modifier, Modifier::BOLD);
assert_eq!(patched.sub_modifier, Modifier::ITALIC);
Source

pub const fn has_modifier(self, modifier: Modifier) -> bool

Returns true if the style has the given modifier set.

§Examples
use ratatui_core::style::{Modifier, Style};

let style = Style::default().add_modifier(Modifier::BOLD | Modifier::ITALIC);
assert!(style.has_modifier(Modifier::BOLD));
assert!(style.has_modifier(Modifier::ITALIC));
assert!(!style.has_modifier(Modifier::UNDERLINED));
Source

pub fn patch<S: Into<Self>>(self, other: S) -> Self

Results in a combined style that is equivalent to applying the two individual styles to a style one after the other.

style accepts any type that is convertible to Style (e.g. Style, Color, or your own type that implements Into<Style>).

§Examples
use ratatui_core::style::{Color, Modifier, Style};

let style_1 = Style::default().fg(Color::Yellow);
let style_2 = Style::default().bg(Color::Red);
let combined = style_1.patch(style_2);
assert_eq!(
    Style::default().patch(style_1).patch(style_2),
    Style::default().patch(combined)
);
Source

pub const fn black(self) -> Self

Sets the foreground color to black.

Source

pub const fn on_black(self) -> Self

Sets the background color to black.

Source

pub const fn red(self) -> Self

Sets the foreground color to red.

Source

pub const fn on_red(self) -> Self

Sets the background color to red.

Source

pub const fn green(self) -> Self

Sets the foreground color to green.

Source

pub const fn on_green(self) -> Self

Sets the background color to green.

Source

pub const fn yellow(self) -> Self

Sets the foreground color to yellow.

Source

pub const fn on_yellow(self) -> Self

Sets the background color to yellow.

Source

pub const fn blue(self) -> Self

Sets the foreground color to blue.

Source

pub const fn on_blue(self) -> Self

Sets the background color to blue.

Source

pub const fn magenta(self) -> Self

Sets the foreground color to magenta.

Source

pub const fn on_magenta(self) -> Self

Sets the background color to magenta.

Source

pub const fn cyan(self) -> Self

Sets the foreground color to cyan.

Source

pub const fn on_cyan(self) -> Self

Sets the background color to cyan.

Source

pub const fn gray(self) -> Self

Sets the foreground color to gray.

Source

pub const fn on_gray(self) -> Self

Sets the background color to gray.

Source

pub const fn dark_gray(self) -> Self

Sets the foreground color to dark_gray.

Source

pub const fn on_dark_gray(self) -> Self

Sets the background color to dark_gray.

Source

pub const fn light_red(self) -> Self

Sets the foreground color to light_red.

Source

pub const fn on_light_red(self) -> Self

Sets the background color to light_red.

Source

pub const fn light_green(self) -> Self

Sets the foreground color to light_green.

Source

pub const fn on_light_green(self) -> Self

Sets the background color to light_green.

Source

pub const fn light_yellow(self) -> Self

Sets the foreground color to light_yellow.

Source

pub const fn on_light_yellow(self) -> Self

Sets the background color to light_yellow.

Source

pub const fn light_blue(self) -> Self

Sets the foreground color to light_blue.

Source

pub const fn on_light_blue(self) -> Self

Sets the background color to light_blue.

Source

pub const fn light_magenta(self) -> Self

Sets the foreground color to light_magenta.

Source

pub const fn on_light_magenta(self) -> Self

Sets the background color to light_magenta.

Source

pub const fn light_cyan(self) -> Self

Sets the foreground color to light_cyan.

Source

pub const fn on_light_cyan(self) -> Self

Sets the background color to light_cyan.

Source

pub const fn white(self) -> Self

Sets the foreground color to white.

Source

pub const fn on_white(self) -> Self

Sets the background color to white.

Source

pub const fn bold(self) -> Self

Adds the bold modifier.

Source

pub const fn not_bold(self) -> Self

Removes the bold modifier.

Source

pub const fn dim(self) -> Self

Adds the dim modifier.

Source

pub const fn not_dim(self) -> Self

Removes the dim modifier.

Source

pub const fn italic(self) -> Self

Adds the italic modifier.

Source

pub const fn not_italic(self) -> Self

Removes the italic modifier.

Source

pub const fn underlined(self) -> Self

Adds the underlined modifier.

Source

pub const fn not_underlined(self) -> Self

Removes the underlined modifier.

Adds the slow_blink modifier.

Removes the slow_blink modifier.

Adds the rapid_blink modifier.

Removes the rapid_blink modifier.

Source

pub const fn reversed(self) -> Self

Adds the reversed modifier.

Source

pub const fn not_reversed(self) -> Self

Removes the reversed modifier.

Source

pub const fn hidden(self) -> Self

Adds the hidden modifier.

Source

pub const fn not_hidden(self) -> Self

Removes the hidden modifier.

Source

pub const fn crossed_out(self) -> Self

Adds the crossed_out modifier.

Source

pub const fn not_crossed_out(self) -> Self

Removes the crossed_out modifier.

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

A custom debug implementation that prints only the fields that are not the default, and unwraps the Options.

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<'de> Deserialize<'de> for Style

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<(Color, Color)> for Style

Source§

fn from((fg, bg): (Color, Color)) -> Self

Creates a new Style with the given foreground and background colors.

§Example
use ratatui_core::style::{Color, Style};

// red foreground, blue background
let style = Style::from((Color::Red, Color::Blue));
// default foreground, blue background
let style = Style::from((Color::Reset, Color::Blue));
Source§

impl From<(Color, Color, Modifier)> for Style

Source§

fn from((fg, bg, modifier): (Color, Color, Modifier)) -> Self

Creates a new Style with the given foreground and background colors and modifier added.

To specify multiple modifiers, use the | operator.

§Example
use ratatui_core::style::{Color, Modifier, Style};

// red foreground, blue background, add bold and italic
let style = Style::from((Color::Red, Color::Blue, Modifier::BOLD | Modifier::ITALIC));
Source§

impl From<(Color, Color, Modifier, Modifier)> for Style

Source§

fn from( (fg, bg, add_modifier, sub_modifier): (Color, Color, Modifier, Modifier), ) -> Self

Creates a new Style with the given foreground and background colors and modifiers added and removed.

§Example
use ratatui_core::style::{Color, Modifier, Style};

// red foreground, blue background, add bold and italic, remove dim
let style = Style::from((
    Color::Red,
    Color::Blue,
    Modifier::BOLD | Modifier::ITALIC,
    Modifier::DIM,
));
Source§

impl From<(Color, Modifier)> for Style

Source§

fn from((fg, modifier): (Color, Modifier)) -> Self

Creates a new Style with the given foreground color and modifier added.

To specify multiple modifiers, use the | operator.

§Example
use ratatui_core::style::{Color, Modifier, Style};

// red foreground, add bold and italic
let style = Style::from((Color::Red, Modifier::BOLD | Modifier::ITALIC));
Source§

impl From<(Modifier, Modifier)> for Style

Source§

fn from((add_modifier, sub_modifier): (Modifier, Modifier)) -> Self

Creates a new Style with the given modifiers added and removed.

§Example
use ratatui_core::style::{Modifier, Style};

// add bold and italic, remove dim
let style = Style::from((Modifier::BOLD | Modifier::ITALIC, Modifier::DIM));
Source§

impl From<Color> for Style

Source§

fn from(color: Color) -> Self

Creates a new Style with the given foreground color.

To specify a foreground and background color, use the from((fg, bg)) constructor.

§Example
use ratatui_core::style::{Color, Style};

let style = Style::from(Color::Red);
Source§

impl From<Modifier> for Style

Source§

fn from(modifier: Modifier) -> Self

Creates a new Style with the given modifier added.

To specify multiple modifiers, use the | operator.

To specify modifiers to add and remove, use the from((add_modifier, sub_modifier)) constructor.

§Example
use ratatui_core::style::{Style, Modifier};

// add bold and italic
let style = Style::from(Modifier::BOLD|Modifier::ITALIC);
Source§

impl From<Style> for Style

Available on crate feature anstyle only.
Source§

fn from(style: Style) -> Self

Converts to this type from the input type.
Source§

impl From<Style> for Style

Available on crate feature anstyle only.
Source§

fn from(style: Style) -> Self

Converts to this type from the input type.
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 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 Serialize for Style

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
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<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
where T: Real + Zero + Arithmetics + Clone, Swp: WhitePoint<T>, Dwp: WhitePoint<T>, D: AdaptFrom<S, Swp, Dwp, T>,

Source§

fn adapt_into_using<M>(self, method: M) -> D
where M: TransformMatrix<T>,

Convert the source color to the destination color using the specified method.
Source§

fn adapt_into(self) -> D

Convert the source color to the destination color using the bradford method by default.
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, C> ArraysFrom<C> for T
where C: IntoArrays<T>,

Source§

fn arrays_from(colors: C) -> T

Cast a collection of colors into a collection of arrays.
Source§

impl<T, C> ArraysInto<C> for T
where C: FromArrays<T>,

Source§

fn arrays_into(self) -> C

Cast this collection of arrays into a collection of colors.
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<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for U
where T: FromCam16Unclamped<WpParam, U>,

Source§

type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
Source§

fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
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, C> ComponentsFrom<C> for T
where C: IntoComponents<T>,

Source§

fn components_from(colors: C) -> T

Cast a collection of colors into a collection of color components.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromAngle<T> for T

Source§

fn from_angle(angle: T) -> T

Performs a conversion from angle.
Source§

impl<T, U> FromStimulus<U> for T
where U: IntoStimulus<T>,

Source§

fn from_stimulus(other: U) -> T

Converts other into Self, while performing the appropriate scaling, rounding and clamping.
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, U> IntoAngle<U> for T
where U: FromAngle<T>,

Source§

fn into_angle(self) -> U

Performs a conversion into T.
Source§

impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for U
where T: Cam16FromUnclamped<WpParam, U>,

Source§

type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
Source§

fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
Source§

impl<T, U> IntoColor<U> for T
where U: FromColor<T>,

Source§

fn into_color(self) -> U

Convert into T with values clamped to the color defined bounds Read more
Source§

impl<T, U> IntoColorUnclamped<U> for T
where U: FromColorUnclamped<T>,

Source§

fn into_color_unclamped(self) -> U

Convert into T. The resulting color might be invalid in its color space Read more
Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoStimulus<T> for T

Source§

fn into_stimulus(self) -> T

Converts self into T, while performing the appropriate scaling, rounding and clamping.
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, C> TryComponentsInto<C> for T
where C: TryFromComponents<T>,

Source§

type Error = <C as TryFromComponents<T>>::Error

The error for when try_into_colors fails to cast.
Source§

fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>

Try to cast this collection of color components into a collection of colors. 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.
Source§

impl<T, U> TryIntoColor<U> for T
where U: TryFromColor<T>,

Source§

fn try_into_color(self) -> Result<U, OutOfBounds<U>>

Convert into T, returning ok if the color is inside of its defined range, otherwise an OutOfBounds error is returned which contains the unclamped color. Read more
Source§

impl<C, U> UintsFrom<C> for U
where C: IntoUints<U>,

Source§

fn uints_from(colors: C) -> U

Cast a collection of colors into a collection of unsigned integers.
Source§

impl<C, U> UintsInto<C> for U
where C: FromUints<U>,

Source§

fn uints_into(self) -> C

Cast this collection of unsigned integers into a collection of colors.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,