1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//Copyright 2018 #UlinProject Денис Котляров

//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at

//       http://www.apache.org/licenses/LICENSE-2.0

//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
// limitations under the License.


//#Ulin Project 1718
//

/*!
Methods for formatted recording of color output.

# Easy methods of formatted recording

```
#[macro_use]
extern crate clucolor;

let str_colored = color!(blue, "test");
println!("{}", str_colored);
	
let str_colored = color!(blue, bold, "test");
println!("{}", str_colored);

	
let str_colored = color!(bright_red, bold, "test");
println!("{}", str_colored);
```

# Generating a string using color types

```
#[macro_use]
extern crate clucolor;

use clucolor::colors::cluColor;
use clucolor::colors::BrightRed;

let string = BrightRed::string_fmt( format_args!("[{:?}] {}", TEST, str) );
let string = BrightRed::stringn( "color str!" );

```


# Recording macros in `Write trait`

```
#[macro_use]
extern crate clucolor;

use clucolor::colors::Blue;
use clucolor::colors::BrightBlue;


writen_color!(&mut ::std::io::stdout(), BrightBlue, "OutValueTest {}", 123);
writen_color!(&mut ::std::io::stdout(), BrightBlue, "OutValueTest2 {}", 12345);
writen_color!(&mut File::open("color_file.txt"), BrightBlue, "Color Str:)", 12345);
```

# Recording using color types

```
#[macro_use]
extern crate clucolor;

use clucolor::colors::Blue;
use clucolor::colors::BrightBlue;


let mut vec: Vec<u8> = Vec::new(); // For Vec implemented Write!!


let _e = BrightBlue::write_str(&mut vec, "color str!" );

let _e = vec.write(b"TestValue"); // For Vec implemented Write!!
//Also this value will remain without color formatting.

let _e = BrightBlue::writen_str(&mut vec, "end str.." );

let _e = BrightRed::writen(&mut vec, b"end value.." );

```

# Use move color arguments
```
#[macro_use]
extern crate clucolor;

use clucolor::colors::BrightBlue;

#[derive(Debug, Default)]
pub struct Items(usize, usize);

impl Items {
     #[inline]
     pub fn count(&self) -> usize {
          self.0 + self.1
     }
}

let mut item = Items::default();

for a in 0..15 {
     BrightGreen::with_color_fmt(format_args!("NUM #{}", a), |fmt_num| {
          BrightBlue::with_color_fmt(format_args!("ITEM #{:?}", item), |fmt_item| {
               BrightRed::with_color_fmt(format_args!("ALL_COUNT {}", item.count()), |fmt_count| {
                    println!("{}, {}; {};", fmt_num, fmt_item, fmt_count);
               });
          });
     });

     item.0 += 1;
     item.1 += 2;
}

```


# Use ColorWriter
```
#[macro_use]
extern crate clucolor;

use clucolor::colors::Blue;

let writer = Blue::writer();
	
let stdout = ::std::io::stdout();
let mut lock_stdio = stdout.lock();
	
writer.writen(&mut lock_stdio, b"TestWriten").unwrap();
```

All other functions are implemented in color mod with the help of cluColor!

*/


#[macro_use]
///Manual methods for color formatting.
pub mod raw;

///Generalized types of colors.
pub mod colors;

///Additional methods of color recording.
pub mod writer;


macro_rules! build_colored {
	( $(  $color:tt | $name:ident | $name2:ident )+ ) => {
		
		///Concat macro for color generation.
		///```	
		///DATA | NAME_COLOR		| NAME2_COLOR
		///---------------------------------------------
		///"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
		///```
		///
		///```
		///let str_colored = color!(blue, bold, "test");
		///println!("{}", str_colored);
		///```
		
		#[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) };
				
			)+
		}
		
		///A concatenated macro for generating a colored static string.
		#[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) };
				
			)+
		}
		
		
		///A concatenated macro for creating a color dynamic string.
		///```	
		///DATA | NAME_COLOR		| NAME2_COLOR
		///---------------------------------------------
		///"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
		///```
		#[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;

///Common features implemented by the generalized type.
#[allow(non_camel_case_types)]
pub trait cluColor: Debug + Display + Eq + Hash + Ord + PartialEq + PartialOrd {
	///Color str type
	fn raw_color<'a>() -> &'a str;
	
	///Color array type
	fn raw_color_b<'a>() -> &'a [u8];
	
	///Name color
	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<'a>(str: &'a str) -> String;
	fn string_fmt<'a>(fmt: Arguments<'a>) -> String;
	fn stringn<'a>(str: &'a str) -> String;
	fn stringn_fmt<'a>(fmt: Arguments<'a>) -> String;
	
	
	#[inline]
	fn write_as<'a, A: AsRef<[u8]>>(w: &mut Write, asref: A) -> io::Result<()> {
		Self::write(w, asref.as_ref())
	}
	#[inline]
	fn writen_as<'a, A: AsRef<[u8]>>(w: &mut Write, asref: A) -> io::Result<()> {
		Self::writen(w, asref.as_ref())
	}
	

	fn write<'a>(w: &mut Write, buf: &'a [u8]) -> io::Result<()>;
	fn write_str<'a>(w: &mut Write, str: &'a str) -> io::Result<()>;
	fn write_fmt<'a>(w: &mut Write, fmt: Arguments<'a>) -> io::Result<()>;
	
	fn writen<'a>(w: &mut Write, buf: &'a [u8]) -> io::Result<()>;
	fn writen_str<'a>(w: &mut Write, str: &'a str) -> io::Result<()>;
	fn writen_fmt<'a>(w: &mut Write, fmt: Arguments<'a>) -> io::Result<()>;

	fn with_color_fmt<'a, F: Fn(&Arguments) -> T, T: 'a>(args: Arguments<'a>, function: F) -> T;
	fn once_with_color_fmt<'a, F: FnOnce(&Arguments) -> T, T: 'a>(args: Arguments<'a>, function: F) -> T;
	fn mut_with_color_fmt<'a, F: FnMut(&Arguments) -> T, T: 'a>(args: Arguments<'a>, function: F) -> T;
}




#[macro_export]
///Macro of the formatted entry in the trait.
macro_rules! write_color {
	( $write:expr, $color:tt, $( $arg:tt )* ) => {
		$color::write_fmt($write, format_args!( $( $arg )* ))
	};
}


#[macro_export]
///Macro of the formatted entry in the trait. Adds /n to end.
macro_rules! writen_color {
	( $write:expr, $color:tt, $( $arg:tt )* ) => {
		$color::writen_fmt($write, format_args!( $( $arg )* ))
	};
}



//Ulin Project 1817