use crate::font::Font;
use std::fmt::Display;
#[macro_export]
macro_rules! cs {
($($arg:tt)*) => {{
let mut s = String::new();
$crate::wcs!(&mut s, $($arg)*);
s
}};
}
#[macro_export]
macro_rules! pcs {
($($arg:tt)*) => {{
let s = $crate::cs!($($arg)*);
println!("{s}")
}};
}
#[macro_export]
macro_rules! epcs {
($($arg:tt)*) => {{
let s = $crate::cs!($($arg)*);
eprintln!("{s}")
}};
}
#[macro_export]
macro_rules! wcs {
($buf:expr, $($font:expr),* ; $($arg:tt)*) => {{
use std::fmt::Write;
$crate::wf!($buf, $($font),*);
write!($buf, $($arg)*).unwrap();
$buf.push_str("\x1b[0m");
}};
($buf:expr, $($($font:expr),* => $($s:expr),* );* $(;)?) => {{
use std::fmt::Write;
$(
$crate::wf!($buf, $($font),*);
$(write!($buf, "{}", $s).unwrap();)*
)*
$buf.push_str("\x1b[0m");
}};
}
macro_rules! colored_trait {
($($method:ident => $font:expr),*) => {
pub trait Colored:Display {
$(
fn $method(&self) -> String {
$crate::cs!($font => self)
}
)*
fn color(&self, r:u8, g:u8, b:u8) -> String {
$crate::cs!($crate::color::Font::Color(r,g,b) => self)
}
fn bg_color(&self, r:u8, g:u8, b:u8) -> String {
$crate::cs!($crate::color::Font::BgColor(r,g,b) => self)
}
#[allow(clippy::ptr_arg)]
fn fonts(&self, mut fonts:String) -> String {
use std::fmt::Write;
write!(&mut fonts, "{}\x1b[0m", self).unwrap();
fonts
}
}
};
}
impl<T> Colored for T where T: Display + ?Sized {}
colored_trait! {
bold => Font::Bold,
underline => Font::Underline,
italic => Font::Italic,
reverse => Font::Reverse,
delete => Font::Delete,
black => Font::Black,
red => Font::Red,
green => Font::Green,
yellow => Font::Yellow,
blue => Font::Blue,
purple => Font::Purple,
cyan => Font::Cyan,
grey => Font::Grey,
bg_black => Font::BgBlack,
bg_red => Font::BgRed,
bg_green => Font::BgGreen,
bg_yellow => Font::BgYellow,
bg_blue => Font::BgBlue,
bg_purple => Font::BgPurple,
bg_cyan => Font::BgCyan,
bg_grey => Font::BgGrey
}