clucolor/
lib.rs

1//Copyright 2018 #UlinProject Денис Котляров
2
3//Licensed under the Apache License, Version 2.0 (the "License");
4//you may not use this file except in compliance with the License.
5//You may obtain a copy of the License at
6
7//       http://www.apache.org/licenses/LICENSE-2.0
8
9//Unless required by applicable law or agreed to in writing, software
10//distributed under the License is distributed on an "AS IS" BASIS,
11//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//See the License for the specific language governing permissions and
13// limitations under the License.
14
15
16//#Ulin Project 1718
17//
18
19/*!
20Methods for formatted recording of color output.
21
22# Easy methods of formatted recording
23
24```
25#[macro_use]
26extern crate clucolor;
27
28let str_colored = color!(blue, "test");
29println!("{}", str_colored);
30	
31let str_colored = color!(blue, bold, "test");
32println!("{}", str_colored);
33
34	
35let str_colored = color!(bright_red, bold, "test");
36println!("{}", str_colored);
37```
38
39# Generating a string using color types
40
41```
42#[macro_use]
43extern crate clucolor;
44
45use clucolor::colors::cluColor;
46use clucolor::colors::BrightRed;
47
48let string = BrightRed::string_fmt( format_args!("[{:?}] {}", TEST, str) );
49let string = BrightRed::stringn( "color str!" );
50
51```
52
53
54# Recording macros in `Write trait`
55
56```
57#[macro_use]
58extern crate clucolor;
59
60use clucolor::colors::Blue;
61use clucolor::colors::BrightBlue;
62
63
64writen_color!(&mut ::std::io::stdout(), BrightBlue, "OutValueTest {}", 123);
65writen_color!(&mut ::std::io::stdout(), BrightBlue, "OutValueTest2 {}", 12345);
66writen_color!(&mut File::open("color_file.txt"), BrightBlue, "Color Str:)", 12345);
67```
68
69# Recording using color types
70
71```
72#[macro_use]
73extern crate clucolor;
74
75use clucolor::colors::Blue;
76use clucolor::colors::BrightBlue;
77
78
79let mut vec: Vec<u8> = Vec::new(); // For Vec implemented Write!!
80
81
82let _e = BrightBlue::write_str(&mut vec, "color str!" );
83
84let _e = vec.write(b"TestValue"); // For Vec implemented Write!!
85//Also this value will remain without color formatting.
86
87let _e = BrightBlue::writen_str(&mut vec, "end str.." );
88
89let _e = BrightRed::writen(&mut vec, b"end value.." );
90
91```
92
93# Use move color arguments
94```
95#[macro_use]
96extern crate clucolor;
97
98use clucolor::colors::BrightBlue;
99
100#[derive(Debug, Default)]
101pub struct Items(usize, usize);
102
103impl Items {
104     #[inline]
105     pub fn count(&self) -> usize {
106          self.0 + self.1
107     }
108}
109
110let mut item = Items::default();
111
112for a in 0..15 {
113     BrightGreen::with_color_fmt(format_args!("NUM #{}", a), |fmt_num| {
114          BrightBlue::with_color_fmt(format_args!("ITEM #{:?}", item), |fmt_item| {
115               BrightRed::with_color_fmt(format_args!("ALL_COUNT {}", item.count()), |fmt_count| {
116                    println!("{}, {}; {};", fmt_num, fmt_item, fmt_count);
117               });
118          });
119     });
120
121     item.0 += 1;
122     item.1 += 2;
123}
124
125```
126
127
128# Use ColorWriter
129```
130#[macro_use]
131extern crate clucolor;
132
133use clucolor::colors::Blue;
134
135let writer = Blue::writer();
136	
137let stdout = ::std::io::stdout();
138let mut lock_stdio = stdout.lock();
139	
140writer.writen(&mut lock_stdio, b"TestWriten").unwrap();
141```
142
143All other functions are implemented in color mod with the help of cluColor!
144
145*/
146
147
148#[macro_use]
149///Manual methods for color formatting.
150pub mod raw;
151
152///Generalized types of colors.
153pub mod colors;
154
155///Additional methods of color recording.
156pub mod writer;
157
158
159macro_rules! build_colored {
160	( $(  $color:tt | $name:ident | $name2:ident )+ ) => {
161		
162		///Concat macro for color generation.
163		///```	
164		///DATA | NAME_COLOR		| NAME2_COLOR
165		///---------------------------------------------
166		///"30" | black			| BLACK
167		///"31" | red			| RED
168		///"32" | green			| GREEN
169		///"33" | yellow			| YELLOW
170		///"34" | blue			| BLUE
171		///"35" | magenta			| MAGENTA
172		///"36" | cyan			| CYAN
173		///"37" | white			| WHITE
174		///
175		///"90" | bright_black		| BRIGHT_BLACK
176		///"91" | bright_red		| BRIGHT_RED
177		///"92" | bright_green		| BRIGHT_GREEN
178		///"93" | bright_yellow		| BRIGHT_YELLOW
179		///"94" | bright_blue		| BRIGHT_BLUE
180		///"95" | bright_magenta		| BRIGHT_MAGENTA
181		///"96" | bright_cyan		| BRIGHT_CYAN
182		///"97" | bright_white		| BRIGHT_WHITE
183		///```
184		///
185		///```
186		///let str_colored = color!(blue, bold, "test");
187		///println!("{}", str_colored);
188		///```
189		
190		#[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		///A concatenated macro for generating a colored static string.
230		#[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		///A concatenated macro for creating a color dynamic string.
267		///```	
268		///DATA | NAME_COLOR		| NAME2_COLOR
269		///---------------------------------------------
270		///"30" | black			| BLACK
271		///"31" | red			| RED
272		///"32" | green			| GREEN
273		///"33" | yellow			| YELLOW
274		///"34" | blue			| BLUE
275		///"35" | magenta			| MAGENTA
276		///"36" | cyan			| CYAN
277		///"37" | white			| WHITE
278		///
279		///"90" | bright_black		| BRIGHT_BLACK
280		///"91" | bright_red		| BRIGHT_RED
281		///"92" | bright_green		| BRIGHT_GREEN
282		///"93" | bright_yellow		| BRIGHT_YELLOW
283		///"94" | bright_blue		| BRIGHT_BLUE
284		///"95" | bright_magenta		| BRIGHT_MAGENTA
285		///"96" | bright_cyan		| BRIGHT_CYAN
286		///"97" | bright_white		| BRIGHT_WHITE
287		///```
288		#[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///Common features implemented by the generalized type.
336#[allow(non_camel_case_types)]
337pub trait cluColor: Debug + Display + Eq + Hash + Ord + PartialEq + PartialOrd {
338	///Color str type
339	fn raw_color<'a>() -> &'a str;
340	
341	///Color array type
342	fn raw_color_b<'a>() -> &'a [u8];
343	
344	///Name color
345	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]
653///Macro of the formatted entry in the trait.
654macro_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]
662///Macro of the formatted entry in the trait. Adds /n to end.
663macro_rules! writen_color {
664	( $write:expr, $color:tt, $( $arg:tt )* ) => {
665		$color::writen_fmt($write, format_args!( $( $arg )* ))
666	};
667}
668
669
670
671//Ulin Project 1817