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>The underline color.
add_modifier: ModifierThe modifiers to add.
sub_modifier: ModifierThe modifiers to remove.
Implementations§
Source§impl Style
impl Style
Sourcepub const fn fg(self, color: Color) -> Style
pub const fn fg(self, color: Color) -> Style
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));Sourcepub const fn bg(self, color: Color) -> Style
pub const fn bg(self, color: Color) -> Style
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));Sourcepub const fn underline_color(self, color: Color) -> Style
pub const fn underline_color(self, color: Color) -> Style
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)
);Sourcepub const fn add_modifier(self, modifier: Modifier) -> Style
pub const fn add_modifier(self, modifier: Modifier) -> Style
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());Sourcepub const fn remove_modifier(self, modifier: Modifier) -> Style
pub const fn remove_modifier(self, modifier: Modifier) -> Style
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);Sourcepub fn patch<S>(self, other: S) -> Style
pub fn patch<S>(self, other: S) -> Style
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)
);Trait Implementations§
Source§impl Debug for Style
A custom debug implementation that prints only the fields that are not the default, and unwraps
the Options.
impl Debug for Style
A custom debug implementation that prints only the fields that are not the default, and unwraps
the Options.
Source§impl<'de> Deserialize<'de> for Style
impl<'de> Deserialize<'de> for Style
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Style, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Style, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl From<(Color, Color)> for Style
impl From<(Color, Color)> for Style
Source§fn from(_: (Color, Color)) -> Style
fn from(_: (Color, Color)) -> Style
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
impl From<(Color, Color, Modifier)> for Style
Source§fn from(_: (Color, Color, Modifier)) -> Style
fn from(_: (Color, Color, Modifier)) -> Style
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
impl From<(Color, Color, Modifier, Modifier)> for Style
Source§fn from(_: (Color, Color, Modifier, Modifier)) -> Style
fn from(_: (Color, Color, Modifier, Modifier)) -> Style
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
impl From<(Color, Modifier)> for Style
Source§fn from(_: (Color, Modifier)) -> Style
fn from(_: (Color, Modifier)) -> Style
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> for Style
impl From<Modifier> for Style
Source§fn from(modifier: Modifier) -> Style
fn from(modifier: Modifier) -> Style
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 FromCrossterm<ContentStyle> for Style
impl FromCrossterm<ContentStyle> for Style
Source§fn from_crossterm(value: ContentStyle) -> Style
fn from_crossterm(value: ContentStyle) -> Style
Source§impl FromTermion<Bg<LightBlack>> for Style
impl FromTermion<Bg<LightBlack>> for Style
Source§fn from_termion(_: Bg<LightBlack>) -> Style
fn from_termion(_: Bg<LightBlack>) -> Style
Source§impl FromTermion<Bg<LightGreen>> for Style
impl FromTermion<Bg<LightGreen>> for Style
Source§fn from_termion(_: Bg<LightGreen>) -> Style
fn from_termion(_: Bg<LightGreen>) -> Style
Source§impl FromTermion<Bg<LightMagenta>> for Style
impl FromTermion<Bg<LightMagenta>> for Style
Source§fn from_termion(_: Bg<LightMagenta>) -> Style
fn from_termion(_: Bg<LightMagenta>) -> Style
Source§impl FromTermion<Bg<LightWhite>> for Style
impl FromTermion<Bg<LightWhite>> for Style
Source§fn from_termion(_: Bg<LightWhite>) -> Style
fn from_termion(_: Bg<LightWhite>) -> Style
Source§impl FromTermion<Bg<LightYellow>> for Style
impl FromTermion<Bg<LightYellow>> for Style
Source§fn from_termion(_: Bg<LightYellow>) -> Style
fn from_termion(_: Bg<LightYellow>) -> Style
Source§impl FromTermion<Fg<LightBlack>> for Style
impl FromTermion<Fg<LightBlack>> for Style
Source§fn from_termion(_: Fg<LightBlack>) -> Style
fn from_termion(_: Fg<LightBlack>) -> Style
Source§impl FromTermion<Fg<LightGreen>> for Style
impl FromTermion<Fg<LightGreen>> for Style
Source§fn from_termion(_: Fg<LightGreen>) -> Style
fn from_termion(_: Fg<LightGreen>) -> Style
Source§impl FromTermion<Fg<LightMagenta>> for Style
impl FromTermion<Fg<LightMagenta>> for Style
Source§fn from_termion(_: Fg<LightMagenta>) -> Style
fn from_termion(_: Fg<LightMagenta>) -> Style
Source§impl FromTermion<Fg<LightWhite>> for Style
impl FromTermion<Fg<LightWhite>> for Style
Source§fn from_termion(_: Fg<LightWhite>) -> Style
fn from_termion(_: Fg<LightWhite>) -> Style
Source§impl FromTermion<Fg<LightYellow>> for Style
impl FromTermion<Fg<LightYellow>> for Style
Source§fn from_termion(_: Fg<LightYellow>) -> Style
fn from_termion(_: Fg<LightYellow>) -> Style
Source§impl FromTermwiz<CellAttributes> for Style
impl FromTermwiz<CellAttributes> for Style
Source§fn from_termwiz(value: CellAttributes) -> Style
fn from_termwiz(value: CellAttributes) -> Style
Source§impl Serialize for Style
impl Serialize for Style
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl Copy for Style
impl Eq for Style
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 Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
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) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
Source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other into Self, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
T.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self into T, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, U> Stylize<'_, T> for Uwhere
U: Styled<Item = T>,
impl<T, U> Stylize<'_, T> for Uwhere
U: Styled<Item = T>,
fn bg<C>(self, color: C) -> T
fn fg<C>(self, color: C) -> T
fn add_modifier(self, modifier: Modifier) -> T
fn remove_modifier(self, modifier: Modifier) -> T
fn reset(self) -> T
Source§fn on_magenta(self) -> T
fn on_magenta(self) -> T
magenta.Source§fn on_dark_gray(self) -> T
fn on_dark_gray(self) -> T
dark_gray.Source§fn on_light_red(self) -> T
fn on_light_red(self) -> T
light_red.Source§fn light_green(self) -> T
fn light_green(self) -> T
light_green.Source§fn on_light_green(self) -> T
fn on_light_green(self) -> T
light_green.Source§fn light_yellow(self) -> T
fn light_yellow(self) -> T
light_yellow.Source§fn on_light_yellow(self) -> T
fn on_light_yellow(self) -> T
light_yellow.Source§fn light_blue(self) -> T
fn light_blue(self) -> T
light_blue.Source§fn on_light_blue(self) -> T
fn on_light_blue(self) -> T
light_blue.Source§fn light_magenta(self) -> T
fn light_magenta(self) -> T
light_magenta.Source§fn on_light_magenta(self) -> T
fn on_light_magenta(self) -> T
light_magenta.Source§fn light_cyan(self) -> T
fn light_cyan(self) -> T
light_cyan.Source§fn on_light_cyan(self) -> T
fn on_light_cyan(self) -> T
light_cyan.Source§fn not_italic(self) -> T
fn not_italic(self) -> T
italic modifier.Source§fn underlined(self) -> T
fn underlined(self) -> T
underlined modifier.Source§fn not_underlined(self) -> T
fn not_underlined(self) -> T
underlined modifier.Source§fn slow_blink(self) -> T
fn slow_blink(self) -> T
slow_blink modifier.Source§fn not_slow_blink(self) -> T
fn not_slow_blink(self) -> T
slow_blink modifier.Source§fn rapid_blink(self) -> T
fn rapid_blink(self) -> T
rapid_blink modifier.Source§fn not_rapid_blink(self) -> T
fn not_rapid_blink(self) -> T
rapid_blink modifier.Source§fn not_reversed(self) -> T
fn not_reversed(self) -> T
reversed modifier.hidden modifier.hidden modifier.Source§fn crossed_out(self) -> T
fn crossed_out(self) -> T
crossed_out modifier.Source§fn not_crossed_out(self) -> T
fn not_crossed_out(self) -> T
crossed_out modifier.Source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds error is returned which contains
the unclamped color. Read more