#[macro_use]
pub mod raw;
pub mod colors;
pub mod writer;
macro_rules! build_colored {
( $( $color:tt | $name:ident | $name2:ident )+ ) => {
#[macro_export]
macro_rules! color_args {
$(
($name, $s:expr) => {
format_args!(
"{}{}{}",
concat!(
raw_color!(start),
$color,
raw_color!(end_color),
),
$s,
raw_color!(end),
)
};
($name, bold, $s:expr) => {
format_args!(
"{}{}{}{}",
concat!(
raw_color!(start),
$color,
raw_color!(end_color),
),
raw_color!(bold),
$s,
raw_color!(end),
)
};
($name2, $s:expr) => { color_args!($name, $s) };
($name2, bold, $s:expr) => { color_args!($name, bold, $s) };
)+
}
#[macro_export]
macro_rules! color {
$(
($name, $s:expr) => {
concat!(
concat!(
raw_color!(start),
$color,
raw_color!(end_color),
),
$s,
raw_color!(end),
)
};
($name, bold, $s:expr) => {
concat!(
concat!(
raw_color!(start),
$color,
raw_color!(end_color),
),
raw_color!(bold),
$s,
raw_color!(end),
)
};
($name2, $s:expr) => { color_args!($name, $s) };
($name2, bold, $s:expr) => { color_args!($name, bold, $s) };
)+
}
#[macro_export]
macro_rules! color_format {
$(
($name, $s:expr) => {
format!("{}", color_args!($name, $s))
};
($name2, $s:expr) => { color!($name, $s) };
($name, bold, $s:expr) => {
format!("{}", color_args!($name, bold, $s))
};
($name2, bold, $s:expr) => { color!($name, bold, $s) };
)+
}
}
}
build_colored! (
"30" | black | BLACK
"31" | red | RED
"32" | green | GREEN
"33" | yellow | YELLOW
"34" | blue | BLUE
"35" | magenta | MAGENTA
"36" | cyan | CYAN
"37" | white | WHITE
"90" | bright_black | BRIGHT_BLACK
"91" | bright_red | BRIGHT_RED
"92" | bright_green | BRIGHT_GREEN
"93" | bright_yellow | BRIGHT_YELLOW
"94" | bright_blue | BRIGHT_BLUE
"95" | bright_magenta | BRIGHT_MAGENTA
"96" | bright_cyan | BRIGHT_CYAN
"97" | bright_white | BRIGHT_WHITE
);
use std::fmt::Arguments;
use std::io::Write;
use writer::cluColorWriter;
use std::hash::Hash;
use std::fmt::Display;
use std::fmt::Debug;
use std::io;
#[allow(non_camel_case_types)]
pub trait cluColor: Debug + Display + Eq + Hash + Ord + PartialEq + PartialOrd {
fn raw_color<'a>() -> &'a str;
fn raw_color_b<'a>() -> &'a [u8];
fn name<'a>() -> &'a str;
#[inline]
fn writer() -> cluColorWriter<Self> where Self: Sized {
cluColorWriter::<Self>::new()
}
#[inline]
fn string_as<'a, A: AsRef<str>>(asref: A) -> String {
Self::string(asref.as_ref())
}
#[inline]
fn stringn_as<'a, A: AsRef<str>>(asref: A) -> String {
Self::stringn(asref.as_ref())
}
fn string_fmt<'a>(fmt: Arguments<'a>) -> String {
format!("{}{}{}{}{}",
raw_color!(start),
Self::raw_color(),
raw_color!(end_color),
fmt,
raw_color!(end)
)
}
fn string<'a>(str: &'a str) -> String {
format!("{}{}{}{}{}",
raw_color!(start),
Self::raw_color(),
raw_color!(end_color),
str,
raw_color!(end)
)
}
fn stringn<'a>(str: &'a str) -> String {
format!("{}{}{}{}{}\n",
raw_color!(start),
Self::raw_color(),
raw_color!(end_color),
str,
raw_color!(end)
)
}
fn stringn_fmt<'a>(fmt: Arguments<'a>) -> String {
format!("{}{}{}{}{}\n",
raw_color!(start),
Self::raw_color(),
raw_color!(end_color),
fmt,
raw_color!(end)
)
}
#[inline]
fn write_as<'a, W: Write, A: AsRef<[u8]>>(w: W, asref: A) -> io::Result<()> {
Self::write(w, asref.as_ref())
}
#[inline]
fn writen_as<'a, W: Write, A: AsRef<[u8]>>(w: W, asref: A) -> io::Result<()> {
Self::writen(w, asref.as_ref())
}
fn write<'a, W: Write>(mut w: W, array: &'a [u8]) -> io::Result<()> {
write!(w, "{}{}{}{}{}",
raw_color!(start),
Self::raw_color(),
raw_color!(end_color),
unsafe { ::std::str::from_utf8_unchecked(array) },
raw_color!(end)
)
}
fn write_str<'a, W: Write>(mut w: W, str: &'a str) -> io::Result<()> {
write!(w, "{}{}{}{}{}",
raw_color!(start),
Self::raw_color(),
raw_color!(end_color),
str,
raw_color!(end)
)
}
fn write_fmt<'a, W: Write>(mut w: W, fmt: Arguments<'a>) -> io::Result<()> {
write!(w, "{}{}{}{}{}",
raw_color!(start),
Self::raw_color(),
raw_color!(end_color),
fmt,
raw_color!(end)
)
}
fn writen<'a, W: Write>(mut w: W, array: &'a [u8]) -> io::Result<()> {
write!(w, "{}{}{}{}{}\n",
raw_color!(start),
Self::raw_color(),
raw_color!(end_color),
unsafe { ::std::str::from_utf8_unchecked(array) },
raw_color!(end)
)
}
fn writen_str<'a, W: Write>(mut w: W, str: &'a str) -> io::Result<()> {
write!(w, "{}{}{}{}{}\n",
raw_color!(start),
Self::raw_color(),
raw_color!(end_color),
str,
raw_color!(end)
)
}
fn writen_fmt<'a, W: Write>(mut w: W, fmt: Arguments<'a>) -> io::Result<()> {
write!(w, "{}{}{}{}{}\n",
raw_color!(start),
Self::raw_color(),
raw_color!(end_color),
fmt,
raw_color!(end)
)
}
fn with_color_fmt<'a, F: Fn(&Arguments) -> T, T: 'a>(args: Arguments<'a>, function: F) -> T {
function(
&format_args!("{}{}{}{}{}",
raw_color!(start),
Self::raw_color(),
raw_color!(end_color),
args,
raw_color!(end)
)
)
}
fn once_with_color_fmt<'a, F: FnOnce(&Arguments) -> T, T: 'a>(args: Arguments<'a>, function: F) -> T {
function(
&format_args!("{}{}{}{}{}",
raw_color!(start),
Self::raw_color(),
raw_color!(end_color),
args,
raw_color!(end)
)
)
}
fn mut_with_color_fmt<'a, F: FnMut(&Arguments) -> T, T: 'a>(args: Arguments<'a>, mut function: F) -> T {
function(
&format_args!("{}{}{}{}{}",
raw_color!(start),
Self::raw_color(),
raw_color!(end_color),
args,
raw_color!(end)
)
)
}
}
impl<'l, T: cluColor> cluColor for &'l T {
#[inline(always)]
fn raw_color<'a>() -> &'a str { T::raw_color() }
#[inline(always)]
fn raw_color_b<'a>() -> &'a [u8] { T::raw_color_b() }
#[inline(always)]
fn name<'a>() -> &'a str { T::name() }
#[inline(always)]
fn writer() -> cluColorWriter<Self> where Self: Sized { cluColorWriter::<Self>::new() }
#[inline(always)]
fn string_as<'a, A: AsRef<str>>(asref: A) -> String { T::string_as(asref) }
#[inline(always)]
fn stringn_as<'a, A: AsRef<str>>(asref: A) -> String { T::stringn_as(asref) }
#[inline(always)]
fn string_fmt<'a>(fmt: Arguments<'a>) -> String { T::string_fmt(fmt) }
#[inline(always)]
fn string<'a>(str: &'a str) -> String { T::string(str) }
#[inline(always)]
fn stringn<'a>(str: &'a str) -> String { T::stringn(str) }
#[inline(always)]
fn stringn_fmt<'a>(fmt: Arguments<'a>) -> String { T::stringn_fmt(fmt) }
#[inline(always)]
fn write_as<'a, W: Write, A: AsRef<[u8]>>(w: W, asref: A) -> io::Result<()> { T::write_as(w, asref) }
#[inline(always)]
fn writen_as<'a, W: Write, A: AsRef<[u8]>>(w: W, asref: A) -> io::Result<()> { T::writen_as(w, asref) }
#[inline(always)]
fn write<'a, W: Write>(w: W, array: &'a [u8]) -> io::Result<()> { T::write(w, array) }
#[inline(always)]
fn write_str<'a, W: Write>(w: W, str: &'a str) -> io::Result<()> { T::write_str(w, str) }
#[inline(always)]
fn write_fmt<'a, W: Write>(w: W, fmt: Arguments<'a>) -> io::Result<()> { T::write_fmt(w, fmt) }
#[inline(always)]
fn writen<'a, W: Write>(w: W, array: &'a [u8]) -> io::Result<()> { T::writen(w, array) }
#[inline(always)]
fn writen_str<'a, W: Write>(w: W, str: &'a str) -> io::Result<()> { T::writen_str(w, str) }
#[inline(always)]
fn writen_fmt<'a, W: Write>(w: W, fmt: Arguments<'a>) -> io::Result<()> { T::writen_fmt(w, fmt) }
#[inline(always)]
fn with_color_fmt<'a, F: Fn(&Arguments) -> C, C: 'a>(args: Arguments<'a>, function: F) -> C { T::with_color_fmt(args, function) }
#[inline(always)]
fn once_with_color_fmt<'a, F: FnOnce(&Arguments) -> C, C: 'a>(args: Arguments<'a>, function: F) -> C { T::once_with_color_fmt(args, function) }
#[inline(always)]
fn mut_with_color_fmt<'a, F: FnMut(&Arguments) -> C, C: 'a>(args: Arguments<'a>, function: F) -> C { T::mut_with_color_fmt(args, function) }
}
impl<'l, T: cluColor> cluColor for &'l mut T {
#[inline(always)]
fn raw_color<'a>() -> &'a str { T::raw_color() }
#[inline(always)]
fn raw_color_b<'a>() -> &'a [u8] { T::raw_color_b() }
#[inline(always)]
fn name<'a>() -> &'a str { T::name() }
#[inline(always)]
fn writer() -> cluColorWriter<Self> where Self: Sized { cluColorWriter::<Self>::new() }
#[inline(always)]
fn string_as<'a, A: AsRef<str>>(asref: A) -> String { T::string_as(asref) }
#[inline(always)]
fn stringn_as<'a, A: AsRef<str>>(asref: A) -> String { T::stringn_as(asref) }
#[inline(always)]
fn string_fmt<'a>(fmt: Arguments<'a>) -> String { T::string_fmt(fmt) }
#[inline(always)]
fn string<'a>(str: &'a str) -> String { T::string(str) }
#[inline(always)]
fn stringn<'a>(str: &'a str) -> String { T::stringn(str) }
#[inline(always)]
fn stringn_fmt<'a>(fmt: Arguments<'a>) -> String { T::stringn_fmt(fmt) }
#[inline(always)]
fn write_as<'a, W: Write, A: AsRef<[u8]>>(w: W, asref: A) -> io::Result<()> { T::write_as(w, asref) }
#[inline(always)]
fn writen_as<'a, W: Write, A: AsRef<[u8]>>(w: W, asref: A) -> io::Result<()> { T::writen_as(w, asref) }
#[inline(always)]
fn write<'a, W: Write>(w: W, array: &'a [u8]) -> io::Result<()> { T::write(w, array) }
#[inline(always)]
fn write_str<'a, W: Write>(w: W, str: &'a str) -> io::Result<()> { T::write_str(w, str) }
#[inline(always)]
fn write_fmt<'a, W: Write>(w: W, fmt: Arguments<'a>) -> io::Result<()> { T::write_fmt(w, fmt) }
#[inline(always)]
fn writen<'a, W: Write>(w: W, array: &'a [u8]) -> io::Result<()> { T::writen(w, array) }
#[inline(always)]
fn writen_str<'a, W: Write>(w: W, str: &'a str) -> io::Result<()> { T::writen_str(w, str) }
#[inline(always)]
fn writen_fmt<'a, W: Write>(w: W, fmt: Arguments<'a>) -> io::Result<()> { T::writen_fmt(w, fmt) }
#[inline(always)]
fn with_color_fmt<'a, F: Fn(&Arguments) -> C, C: 'a>(args: Arguments<'a>, function: F) -> C { T::with_color_fmt(args, function) }
#[inline(always)]
fn once_with_color_fmt<'a, F: FnOnce(&Arguments) -> C, C: 'a>(args: Arguments<'a>, function: F) -> C { T::once_with_color_fmt(args, function) }
#[inline(always)]
fn mut_with_color_fmt<'a, F: FnMut(&Arguments) -> C, C: 'a>(args: Arguments<'a>, function: F) -> C { T::mut_with_color_fmt(args, function) }
}
#[macro_export]
macro_rules! write_color {
( $write:expr, $color:tt, $( $arg:tt )* ) => {
$color::write_fmt($write, format_args!( $( $arg )* ))
};
}
#[macro_export]
macro_rules! writen_color {
( $write:expr, $color:tt, $( $arg:tt )* ) => {
$color::writen_fmt($write, format_args!( $( $arg )* ))
};
}