use super::{Color, Modifier};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
pub struct Style {
pub fg: Color,
pub bg: Color,
pub modifier: Modifier,
}
impl Style {
pub const fn new() -> Self {
Self {
fg: Color::Reset,
bg: Color::Reset,
modifier: Modifier::NONE,
}
}
pub const fn reset() -> Self {
Self::new()
}
pub const fn fg(mut self, color: Color) -> Self {
self.fg = color;
self
}
pub const fn bg(mut self, color: Color) -> Self {
self.bg = color;
self
}
pub const fn modifier(mut self, modifier: Modifier) -> Self {
self.modifier = modifier;
self
}
pub const fn bold(mut self) -> Self {
self.modifier = self.modifier.union(Modifier::BOLD);
self
}
pub const fn dim(mut self) -> Self {
self.modifier = self.modifier.union(Modifier::DIM);
self
}
pub const fn italic(mut self) -> Self {
self.modifier = self.modifier.union(Modifier::ITALIC);
self
}
pub const fn underlined(mut self) -> Self {
self.modifier = self.modifier.union(Modifier::UNDERLINED);
self
}
pub const fn crossed_out(mut self) -> Self {
self.modifier = self.modifier.union(Modifier::CROSSED_OUT);
self
}
pub const fn reversed(mut self) -> Self {
self.modifier = self.modifier.union(Modifier::REVERSED);
self
}
pub fn patch(self, other: Style) -> Self {
Self {
fg: if other.fg == Color::Reset {
self.fg
} else {
other.fg
},
bg: if other.bg == Color::Reset {
self.bg
} else {
other.bg
},
modifier: self.modifier | other.modifier,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_style_default() {
let s = Style::new();
assert_eq!(s.fg, Color::Reset);
assert_eq!(s.bg, Color::Reset);
assert!(s.modifier.is_empty());
}
#[test]
fn test_style_builder() {
let s = Style::new().fg(Color::Red).bg(Color::Blue).bold().italic();
assert_eq!(s.fg, Color::Red);
assert_eq!(s.bg, Color::Blue);
assert!(s.modifier.contains(Modifier::BOLD));
assert!(s.modifier.contains(Modifier::ITALIC));
}
#[test]
fn test_style_patch() {
let base = Style::new().fg(Color::Red).bold();
let patch = Style::new().fg(Color::Blue);
let combined = base.patch(patch);
assert_eq!(combined.fg, Color::Blue); assert!(combined.modifier.contains(Modifier::BOLD)); }
#[test]
fn test_style_patch_reset_preserved() {
let base = Style::new().fg(Color::Red);
let patch = Style::new();
let combined = base.patch(patch);
assert_eq!(combined.fg, Color::Red); }
}