use crate::color::Color;
use owo_colors::OwoColorize;
use std::fmt::{self, Display};
#[derive(Debug)]
pub struct Colorized<'a, D: Display> {
display: D,
foreground: &'a Option<Color>,
background: &'a Option<Color>,
}
impl<'a, D> Colorized<'a, D>
where
D: Display,
{
pub fn new(display: D, foreground: &'a Option<Color>, background: &'a Option<Color>) -> Self {
Self {
display,
foreground,
background,
}
}
}
impl<D> Display for Colorized<'_, D>
where
D: Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let fg = self.foreground.map(|c| c.into_owo());
let bg = self.background.map(|c| c.into_owo());
match (fg, bg) {
(Some(fg), Some(bg)) => write!(f, "{}", self.display.color(fg).on_color(bg)),
(Some(fg), None) => write!(f, "{}", self.display.color(fg)),
(None, Some(bg)) => write!(f, "{}", self.display.on_color(bg)),
(None, None) => write!(f, "{}", self.display),
}
}
}