1#[macro_use]
149pub mod raw;
151
152pub mod colors;
154
155pub mod writer;
157
158
159macro_rules! build_colored {
160 ( $( $color:tt | $name:ident | $name2:ident )+ ) => {
161
162 #[macro_export]
191 macro_rules! color_args {
192 $(
193
194 ($name, $s:expr) => {
195 format_args!(
196 "{}{}{}",
197
198 concat!(
199 raw_color!(start),
200 $color,
201 raw_color!(end_color),
202 ),
203 $s,
204 raw_color!(end),
205 )
206 };
207
208 ($name, bold, $s:expr) => {
209 format_args!(
210 "{}{}{}{}",
211
212 concat!(
213 raw_color!(start),
214 $color,
215 raw_color!(end_color),
216 ),
217 raw_color!(bold),
218 $s,
219 raw_color!(end),
220 )
221 };
222
223 ($name2, $s:expr) => { color_args!($name, $s) };
224 ($name2, bold, $s:expr) => { color_args!($name, bold, $s) };
225
226 )+
227 }
228
229 #[macro_export]
231 macro_rules! color {
232 $(
233
234 ($name, $s:expr) => {
235 concat!(
236 concat!(
237 raw_color!(start),
238 $color,
239 raw_color!(end_color),
240 ),
241 $s,
242 raw_color!(end),
243 )
244 };
245
246 ($name, bold, $s:expr) => {
247 concat!(
248 concat!(
249 raw_color!(start),
250 $color,
251 raw_color!(end_color),
252 ),
253 raw_color!(bold),
254 $s,
255 raw_color!(end),
256 )
257 };
258
259 ($name2, $s:expr) => { color_args!($name, $s) };
260 ($name2, bold, $s:expr) => { color_args!($name, bold, $s) };
261
262 )+
263 }
264
265
266 #[macro_export]
289 macro_rules! color_format {
290 $(
291 ($name, $s:expr) => {
292 format!("{}", color_args!($name, $s))
293 };
294 ($name2, $s:expr) => { color!($name, $s) };
295
296 ($name, bold, $s:expr) => {
297 format!("{}", color_args!($name, bold, $s))
298 };
299 ($name2, bold, $s:expr) => { color!($name, bold, $s) };
300 )+
301 }
302
303 }
304}
305
306build_colored! (
307 "30" | black | BLACK
308 "31" | red | RED
309 "32" | green | GREEN
310 "33" | yellow | YELLOW
311 "34" | blue | BLUE
312 "35" | magenta | MAGENTA
313 "36" | cyan | CYAN
314 "37" | white | WHITE
315
316 "90" | bright_black | BRIGHT_BLACK
317 "91" | bright_red | BRIGHT_RED
318 "92" | bright_green | BRIGHT_GREEN
319 "93" | bright_yellow | BRIGHT_YELLOW
320 "94" | bright_blue | BRIGHT_BLUE
321 "95" | bright_magenta | BRIGHT_MAGENTA
322 "96" | bright_cyan | BRIGHT_CYAN
323 "97" | bright_white | BRIGHT_WHITE
324);
325
326
327use std::fmt::Arguments;
328use std::io::Write;
329use writer::cluColorWriter;
330use std::hash::Hash;
331use std::fmt::Display;
332use std::fmt::Debug;
333use std::io;
334
335#[allow(non_camel_case_types)]
337pub trait cluColor: Debug + Display + Eq + Hash + Ord + PartialEq + PartialOrd {
338 fn raw_color<'a>() -> &'a str;
340
341 fn raw_color_b<'a>() -> &'a [u8];
343
344 fn name<'a>() -> &'a str;
346
347
348 #[inline]
349 fn writer() -> cluColorWriter<Self> where Self: Sized {
350 cluColorWriter::<Self>::new()
351 }
352
353
354 #[inline]
355 fn string_as<'a, A: AsRef<str>>(asref: A) -> String {
356 Self::string(asref.as_ref())
357 }
358 #[inline]
359 fn stringn_as<'a, A: AsRef<str>>(asref: A) -> String {
360 Self::stringn(asref.as_ref())
361 }
362
363
364 fn string_fmt<'a>(fmt: Arguments<'a>) -> String {
365 format!("{}{}{}{}{}",
366 raw_color!(start),
367 Self::raw_color(),
368 raw_color!(end_color),
369 fmt,
370 raw_color!(end)
371 )
372 }
373 fn string<'a>(str: &'a str) -> String {
374 format!("{}{}{}{}{}",
375 raw_color!(start),
376 Self::raw_color(),
377 raw_color!(end_color),
378 str,
379 raw_color!(end)
380 )
381 }
382
383 fn stringn<'a>(str: &'a str) -> String {
384 format!("{}{}{}{}{}\n",
385 raw_color!(start),
386 Self::raw_color(),
387 raw_color!(end_color),
388 str,
389 raw_color!(end)
390 )
391 }
392 fn stringn_fmt<'a>(fmt: Arguments<'a>) -> String {
393 format!("{}{}{}{}{}\n",
394 raw_color!(start),
395 Self::raw_color(),
396 raw_color!(end_color),
397 fmt,
398 raw_color!(end)
399 )
400 }
401
402
403 #[inline]
404 fn write_as<'a, W: Write, A: AsRef<[u8]>>(w: W, asref: A) -> io::Result<()> {
405 Self::write(w, asref.as_ref())
406 }
407 #[inline]
408 fn writen_as<'a, W: Write, A: AsRef<[u8]>>(w: W, asref: A) -> io::Result<()> {
409 Self::writen(w, asref.as_ref())
410 }
411
412
413 fn write<'a, W: Write>(mut w: W, array: &'a [u8]) -> io::Result<()> {
414 write!(w, "{}{}{}{}{}",
415 raw_color!(start),
416 Self::raw_color(),
417 raw_color!(end_color),
418 unsafe { ::std::str::from_utf8_unchecked(array) },
419 raw_color!(end)
420 )
421 }
422
423 fn write_str<'a, W: Write>(mut w: W, str: &'a str) -> io::Result<()> {
424 write!(w, "{}{}{}{}{}",
425
426 raw_color!(start),
427 Self::raw_color(),
428 raw_color!(end_color),
429 str,
430 raw_color!(end)
431 )
432 }
433 fn write_fmt<'a, W: Write>(mut w: W, fmt: Arguments<'a>) -> io::Result<()> {
434 write!(w, "{}{}{}{}{}",
435 raw_color!(start),
436 Self::raw_color(),
437 raw_color!(end_color),
438 fmt,
439 raw_color!(end)
440 )
441 }
442
443 fn writen<'a, W: Write>(mut w: W, array: &'a [u8]) -> io::Result<()> {
444 write!(w, "{}{}{}{}{}\n",
445
446 raw_color!(start),
447 Self::raw_color(),
448 raw_color!(end_color),
449 unsafe { ::std::str::from_utf8_unchecked(array) },
450 raw_color!(end)
451 )
452 }
453
454 fn writen_str<'a, W: Write>(mut w: W, str: &'a str) -> io::Result<()> {
455 write!(w, "{}{}{}{}{}\n",
456
457 raw_color!(start),
458 Self::raw_color(),
459 raw_color!(end_color),
460 str,
461 raw_color!(end)
462 )
463 }
464
465 fn writen_fmt<'a, W: Write>(mut w: W, fmt: Arguments<'a>) -> io::Result<()> {
466 write!(w, "{}{}{}{}{}\n",
467 raw_color!(start),
468 Self::raw_color(),
469 raw_color!(end_color),
470 fmt,
471 raw_color!(end)
472 )
473 }
474
475 fn with_color_fmt<'a, F: Fn(&Arguments) -> T, T: 'a>(args: Arguments<'a>, function: F) -> T {
476 function(
477 &format_args!("{}{}{}{}{}",
478
479 raw_color!(start),
480 Self::raw_color(),
481 raw_color!(end_color),
482 args,
483 raw_color!(end)
484 )
485 )
486 }
487 fn once_with_color_fmt<'a, F: FnOnce(&Arguments) -> T, T: 'a>(args: Arguments<'a>, function: F) -> T {
488 function(
489 &format_args!("{}{}{}{}{}",
490
491 raw_color!(start),
492 Self::raw_color(),
493 raw_color!(end_color),
494 args,
495 raw_color!(end)
496 )
497 )
498 }
499
500 fn mut_with_color_fmt<'a, F: FnMut(&Arguments) -> T, T: 'a>(args: Arguments<'a>, mut function: F) -> T {
501 function(
502 &format_args!("{}{}{}{}{}",
503
504 raw_color!(start),
505 Self::raw_color(),
506 raw_color!(end_color),
507 args,
508 raw_color!(end)
509 )
510 )
511 }
512}
513
514
515impl<'l, T: cluColor> cluColor for &'l T {
516 #[inline(always)]
517 fn raw_color<'a>() -> &'a str { T::raw_color() }
518
519 #[inline(always)]
520 fn raw_color_b<'a>() -> &'a [u8] { T::raw_color_b() }
521
522 #[inline(always)]
523 fn name<'a>() -> &'a str { T::name() }
524
525
526 #[inline(always)]
527 fn writer() -> cluColorWriter<Self> where Self: Sized { cluColorWriter::<Self>::new() }
528
529
530 #[inline(always)]
531 fn string_as<'a, A: AsRef<str>>(asref: A) -> String { T::string_as(asref) }
532
533 #[inline(always)]
534 fn stringn_as<'a, A: AsRef<str>>(asref: A) -> String { T::stringn_as(asref) }
535
536 #[inline(always)]
537 fn string_fmt<'a>(fmt: Arguments<'a>) -> String { T::string_fmt(fmt) }
538
539 #[inline(always)]
540 fn string<'a>(str: &'a str) -> String { T::string(str) }
541
542 #[inline(always)]
543 fn stringn<'a>(str: &'a str) -> String { T::stringn(str) }
544
545 #[inline(always)]
546 fn stringn_fmt<'a>(fmt: Arguments<'a>) -> String { T::stringn_fmt(fmt) }
547
548 #[inline(always)]
549 fn write_as<'a, W: Write, A: AsRef<[u8]>>(w: W, asref: A) -> io::Result<()> { T::write_as(w, asref) }
550
551 #[inline(always)]
552 fn writen_as<'a, W: Write, A: AsRef<[u8]>>(w: W, asref: A) -> io::Result<()> { T::writen_as(w, asref) }
553
554 #[inline(always)]
555 fn write<'a, W: Write>(w: W, array: &'a [u8]) -> io::Result<()> { T::write(w, array) }
556
557 #[inline(always)]
558 fn write_str<'a, W: Write>(w: W, str: &'a str) -> io::Result<()> { T::write_str(w, str) }
559
560 #[inline(always)]
561 fn write_fmt<'a, W: Write>(w: W, fmt: Arguments<'a>) -> io::Result<()> { T::write_fmt(w, fmt) }
562
563 #[inline(always)]
564 fn writen<'a, W: Write>(w: W, array: &'a [u8]) -> io::Result<()> { T::writen(w, array) }
565
566 #[inline(always)]
567 fn writen_str<'a, W: Write>(w: W, str: &'a str) -> io::Result<()> { T::writen_str(w, str) }
568
569 #[inline(always)]
570 fn writen_fmt<'a, W: Write>(w: W, fmt: Arguments<'a>) -> io::Result<()> { T::writen_fmt(w, fmt) }
571
572 #[inline(always)]
573 fn with_color_fmt<'a, F: Fn(&Arguments) -> C, C: 'a>(args: Arguments<'a>, function: F) -> C { T::with_color_fmt(args, function) }
574
575 #[inline(always)]
576 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) }
577
578 #[inline(always)]
579 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) }
580}
581
582
583
584impl<'l, T: cluColor> cluColor for &'l mut T {
585 #[inline(always)]
586 fn raw_color<'a>() -> &'a str { T::raw_color() }
587
588 #[inline(always)]
589 fn raw_color_b<'a>() -> &'a [u8] { T::raw_color_b() }
590
591 #[inline(always)]
592 fn name<'a>() -> &'a str { T::name() }
593
594
595 #[inline(always)]
596 fn writer() -> cluColorWriter<Self> where Self: Sized { cluColorWriter::<Self>::new() }
597
598
599 #[inline(always)]
600 fn string_as<'a, A: AsRef<str>>(asref: A) -> String { T::string_as(asref) }
601
602 #[inline(always)]
603 fn stringn_as<'a, A: AsRef<str>>(asref: A) -> String { T::stringn_as(asref) }
604
605 #[inline(always)]
606 fn string_fmt<'a>(fmt: Arguments<'a>) -> String { T::string_fmt(fmt) }
607
608 #[inline(always)]
609 fn string<'a>(str: &'a str) -> String { T::string(str) }
610
611 #[inline(always)]
612 fn stringn<'a>(str: &'a str) -> String { T::stringn(str) }
613
614 #[inline(always)]
615 fn stringn_fmt<'a>(fmt: Arguments<'a>) -> String { T::stringn_fmt(fmt) }
616
617 #[inline(always)]
618 fn write_as<'a, W: Write, A: AsRef<[u8]>>(w: W, asref: A) -> io::Result<()> { T::write_as(w, asref) }
619
620 #[inline(always)]
621 fn writen_as<'a, W: Write, A: AsRef<[u8]>>(w: W, asref: A) -> io::Result<()> { T::writen_as(w, asref) }
622
623 #[inline(always)]
624 fn write<'a, W: Write>(w: W, array: &'a [u8]) -> io::Result<()> { T::write(w, array) }
625
626 #[inline(always)]
627 fn write_str<'a, W: Write>(w: W, str: &'a str) -> io::Result<()> { T::write_str(w, str) }
628
629 #[inline(always)]
630 fn write_fmt<'a, W: Write>(w: W, fmt: Arguments<'a>) -> io::Result<()> { T::write_fmt(w, fmt) }
631
632 #[inline(always)]
633 fn writen<'a, W: Write>(w: W, array: &'a [u8]) -> io::Result<()> { T::writen(w, array) }
634
635 #[inline(always)]
636 fn writen_str<'a, W: Write>(w: W, str: &'a str) -> io::Result<()> { T::writen_str(w, str) }
637
638 #[inline(always)]
639 fn writen_fmt<'a, W: Write>(w: W, fmt: Arguments<'a>) -> io::Result<()> { T::writen_fmt(w, fmt) }
640
641 #[inline(always)]
642 fn with_color_fmt<'a, F: Fn(&Arguments) -> C, C: 'a>(args: Arguments<'a>, function: F) -> C { T::with_color_fmt(args, function) }
643
644 #[inline(always)]
645 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) }
646
647 #[inline(always)]
648 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) }
649}
650
651
652#[macro_export]
653macro_rules! write_color {
655 ( $write:expr, $color:tt, $( $arg:tt )* ) => {
656 $color::write_fmt($write, format_args!( $( $arg )* ))
657 };
658}
659
660
661#[macro_export]
662macro_rules! writen_color {
664 ( $write:expr, $color:tt, $( $arg:tt )* ) => {
665 $color::writen_fmt($write, format_args!( $( $arg )* ))
666 };
667}
668
669
670
671