use crate::color::Color;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Style {
pub(crate) fg: Color,
pub(crate) bg: Color,
}
impl Style {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub const fn fg(mut self, color: Color) -> Self {
self.fg = color;
self
}
#[must_use]
pub const fn bg(mut self, color: Color) -> Self {
self.bg = color;
self
}
#[must_use]
pub const fn foreground(&self) -> Color {
self.fg
}
#[must_use]
pub const fn background(&self) -> Color {
self.bg
}
#[must_use]
pub fn patch(mut self, other: Self) -> Self {
if other.fg != Color::Default {
self.fg = other.fg;
}
if other.bg != Color::Default {
self.bg = other.bg;
}
self
}
#[must_use]
pub const fn reset_fg(mut self) -> Self {
self.fg = Color::Default;
self
}
#[must_use]
pub const fn reset_bg(mut self) -> Self {
self.bg = Color::Default;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_style_builder() {
let s = Style::new().fg(Color::RED).bg(Color::BLUE);
assert_eq!(s.foreground(), Color::RED);
assert_eq!(s.background(), Color::BLUE);
}
#[test]
fn test_patch_keeps_non_default_fields() {
let base = Style::new().fg(Color::RED).bg(Color::BLUE);
let patched = base.patch(Style::new().fg(Color::GREEN));
assert_eq!(patched.foreground(), Color::GREEN);
assert_eq!(patched.background(), Color::BLUE);
}
#[test]
fn test_patch_cannot_reset_a_field_to_default() {
let base = Style::new().fg(Color::RED).bg(Color::BLUE);
let patched = base.patch(Style::new());
assert_eq!(patched.foreground(), Color::RED);
assert_eq!(patched.background(), Color::BLUE);
}
#[test]
fn test_reset_fg_and_reset_bg_clear_to_default() {
let s = Style::new().fg(Color::RED).bg(Color::BLUE);
assert_eq!(s.reset_fg().foreground(), Color::Default);
assert_eq!(s.reset_bg().background(), Color::Default);
}
}