use core::fmt;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Canvas {
Background,
Foreground,
}
pub trait FormatColor {
fn prelude(&self, f: &mut fmt::Formatter, canvas: Canvas) -> fmt::Result;
fn epilogue(&self, f: &mut fmt::Formatter, canvas: Canvas) -> fmt::Result {
f.write_str(match canvas {
Canvas::Foreground => "\x1B[39m",
Canvas::Background => "\x1B[49m"
})
}
}
pub struct WithForeground<Item, Formatter: FormatColor> {
item: Item,
formatter: Formatter
}
pub struct WithBackground<Item, Formatter: FormatColor> {
item: Item,
formatter: Formatter
}
pub trait Colorable: Sized {
fn bg<TFormatColor: FormatColor>(self, formatter: TFormatColor) -> WithBackground<Self, TFormatColor> {
WithBackground {
item: self,
formatter
}
}
fn fg<TFormatColor: FormatColor>(self, formatter: TFormatColor) -> WithForeground<Self, TFormatColor> {
WithForeground {
item: self,
formatter
}
}
}
impl<T> Colorable for T {}
macro_rules! impl_me {
($bound:path) => {
impl<Item: $bound, TFormatColor: FormatColor> $bound for WithForeground<Item, TFormatColor> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.formatter.prelude(f, Canvas::Foreground)
.and_then(|_| self.item.fmt(f))
.and_then(|_| self.formatter.epilogue(f, Canvas::Foreground))
}
}
impl<Item: $bound, TFormatColor: FormatColor> $bound for WithBackground<Item, TFormatColor> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.formatter.prelude(f, Canvas::Background)
.and_then(|_| self.item.fmt(f))
.and_then(|_| self.formatter.epilogue(f, Canvas::Background))
}
}
};
}
impl_me!(fmt::Binary);
impl_me!(fmt::Debug);
impl_me!(fmt::Display);
impl_me!(fmt::LowerExp);
impl_me!(fmt::LowerHex);
impl_me!(fmt::Octal);
impl_me!(fmt::Pointer);
impl_me!(fmt::UpperExp);
impl_me!(fmt::UpperHex);