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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
//! Library for working with ansi codes to create beutiful terminal outputs.
//!
//! The main focus of this library are the macros [`formatc`], [`printc`],
//! [`printcln`], [`eprintc`], [`eprintcln`], [`writecln`] adn [`writecln`].
//! They can be used in the same way as you would use the standard rust macros
//! [`format`], [`print`], [`println`], [`eprint`], [`eprintln`], [`write`] and
//! [`writeln`]. In addition the macros in this crate have special syntax for
//! encoding terminal commands.
//!
//! ## The macros
//! For all these macros the following applies:
//!
//! If content braces `{}` starts with `'` (e.g. `"{'command}hello"`) than the
//! content is interpreted by this crate, otherwised it is interpreted by the
//! [`format`] macro.
//!
//! The content can contain one or more commands that will expand directly to a
//! string literal.
//!
//! For most of the color commands the folowing is true:
//! - The commands have short aliases (e.g. `w` is alias for `white`)
//! - Some of the commands can be reset, the resetting command has the same
//! name but it is prepended with `_` (e.g. use `italic` to set font style to
//! italic and than use `_italic` to unset the italic font style)
//! - Some commands take arguments and some of the arguments are optional. The
//! arguments are passed to the command by typing them directly after the
//! command and separate them by `,` (e.g. `move_to0,0` moves the cursor to
//! 0, 0. This is also the default value so you can just use `move_to,`).
//!
//! ### RGB color commands
//! The colors can be set either by the [color commands](#color-commands) or
//! by hex color:
//!
//! The hex color starts with `#` and can contain either 1, 2, 3 or 6 hex
//! digits. They are interpreted as follows:
//! - **6 digits:** normal 6 digit RGB color (e.g `#FF0000` is pure red)
//! - **3 digits:** 3 digit RGB color, each digit is repeated twice (e.g.
//! `#ABC` has the same result as `#AABBCC`)
//! - **2 digits:** the two digits are repeated 3 times to form one of 256
//! shades of gray (e.g. `#AB` has the same result as `#ABABAB`)
//! - **1 digit:** the digit is repeated 6 times to form one of 16 shades of
//! gray (e.g. `#A` has the same result as `#AAAAAA`)
//!
//! If you want to set the foregorund color, you just type the hex (e.g.
//! `#FF0000` will set the foreground color to pure red). In order to set the
//! background color, you can append `_` to the color (e.g. `#FF0000_` will set
//! the background color to pure red).
//!
//! If you want to set the underline color, just type the same as background
//! color, but use `u` instead of the `_`.
//!
//! ### Ascii commands
//! - `bell`: console bell (create sound)
//! - `backspace`: move left by one
//! - `htab`, `tab`: horizontal tabulator
//! - `move_down_scrl`, `mds`: move down by one line scrolling if needed
//! - `newline`, `nl`: move to the start of the next line
//! - `vtab`: vertical tabulator
//! - `carriage_return` | `cr`: move to the start of the current line
//!
//! ### Commands for moving the cursor
//! - `move_to`, `mt`: moves the cursor to the given position, has two
//! arguments, default values are `0`.
//! - `move_up`, `mu`: moves the cursor up by the given amount, has one
//! argument, default value is `1`
//! - `move_down`, `md`: moves the cursor down by the given amount, has one
//! argument, default value is `1`
//! - `move_right`, `mr`: moves the cursor right by the given amount, has one
//! argument, default value is `1`
//! - `move_left`, `ml`: moves the cursor left by the given amount, has one
//! argument, default value is `1`
//! - `set_down`, `sd`: moves the cursor to the start of line n lines down, has
//! one argument, default value is `1`
//! - `set_up`, `su`: moves the cursor to the start of line n lines up, has one
//! argument, default value is `1`
//! - `move_to_column`, `mc`: moves the cursor to the given x coordinate, has
//! one argument, default value is `0`
//! + `move_up_scrl`, `mus`: moves the cursor up by one line, scrolling if
//! needed
//! + `save_cur`, `save`, `s`: saves the current cursor position (single slot,
//! not stack)
//! + `load_cur`, `load`, `l`: loads the last saved cursor position
//!
//! ### Erase commands
//! - `erase_to_end`, `e_`: erases from the cursor to the end of the screen
//! - `erase_from_start`, `_e`: erases from the start of the screen to the cursor
//! - `erase_screen`, `_e_`: erases the whole screen
//! - `erase_all`, `e`: erases the whole screen and the scroll buffer
//! - `erase_ln_end`, `el_`: erases from the cursor to the end of the line
//! - `erase_ln_start`, `_el`: erases from the start of the line to the cursor
//! - `erase_line`, `erase_ln`, `_el_`, `el`: erases the current line
//!
//! ### Font style and color command
//! + `reset`, `_`: resets all colors and styles
//!
//! ### Font style commands
//! - `bold`: sets style to bold
//! - `faint`, `f`: sets style to faint
//! - `italic`, `i`: sets style to italic
//! - `underline`, `u`: sets style to underline
//! - `blinking`, `blink`: sets style to blinking
//! - `inverse`: sets style to inverse (swap background and foreground)
//! - `invisible`, `invis`: sets the style to invisible (foreground and
//! background are same)
//! - `striketrough`, `strike`: sets the style to striketrough
//! - `double_underline`, `dunderline`, `dun`: sets the style to double
//! underline
//! - `overline`, `ol`: sets the style to overline
//! + `_bold`: resets bold and faint
//! + `_italic`, `_i`: resets italic
//! + `_underline`, `_u`: resets underline and double underline
//! + `_blinking`, `_blink`: resets blinking
//! + `_inverse`: resets inverse
//! + `_invisible`, `_invis`: resets invisible
//! + `_striketrough`, `_strike`: resets striketrough
//! + `_overline`, `_ol`: resets overline
//!
//! ### Color commands
//! - `black_fg`, `black`, `bl`: sets the foreground to black
//! - `white_fg`, `white`, `w`: sets the foreground to white
//! - `gray_fg`, `gray`, `gr`: sets the foreground to green
//! - `bright_gray_fg`, `bgray`, `bgr`: sets the foreground to bright gray
//! + `red_fg`, `red`, `r`: sets the foreground to red
//! + `green_fg`, `green`, `g`: sets the foreground to green
//! + `yellow_fg`, `yellow`, `y`: sets the foreground to yellow
//! + `magenta_fg`, `magenta`, `m`: sets the foreground to magenta
//! + `cyan_fg`, `cyan`, `c`: sets the foreground to cyan
//! - `dark_red_fg`, `dred`, `dr`: sets the foreground to dark red
//! - `dark_green_fg`, `dgreen`, `dg`: sets the foreground to dark green
//! - `dark_yellow_fg`, `dyellow`, `dy`: sets the foreground to dark yellow
//! - `dark_magenta_fg`, `dmagenta`, `dm`: sets the foreground to dark magenta
//! - `dark_cyan_fg`, `dcyan`, `dc`: sets the foreground to dark cyan
//! + `_fg`: resets the foreground color
//! - `black_bg`, `blackb`, `blb`: sets the background to black
//! - `white_bg`, `whiteb`, `wb`: sets the background to white
//! - `gray_bg`, `grayb`, `grb`: sets the background to green
//! - `bright_gray_bg`, `bgrayb`, `bgrb`: sets the background to bright gray
//! + `red_bg`, `redb`, `rb`: sets the background to red
//! + `green_bg`, `greenb`, `gb`: sets the background to green
//! + `yellow_bg`, `yellowb`, `yb`: sets the background to yellow
//! + `magenta_bg`, `magentab`, `mb`: sets the background to magenta
//! + `cyan_bg`, `cyanb`, `cb`: sets the background to cyan
//! - `dark_red_bg`, `dredb`, `drb`: sets the background to dark red
//! - `dark_green_bg`, `dgreenb`, `dgb`: sets the background to dark green
//! - `dark_yellow_bg`, `dyellowb`, `dyb`: sets the background to dark yellow
//! - `dark_magenta_bg`, `dmagentab`, `dmb`: sets the background to dark
//! magenta
//! - `dark_cyan_bg`, `dcyanb`, `dcb`: sets the background to dark cyan
//! + `_bg`: resets the background
//! + `_ucolor`, `_uc`: resets the underline color
//! - `fg`: sets the foreground color to one of the 256 colors, has one
//! argument
//! - `bg`: sets the background color to one of the 256 colors, has one
//! argument
//! - `ucolor`, `uc`: sets the underline color to one of the 256 colors, has
//! one argument.
//!
//! ### Other
//! - `line_wrap`, `wrap`: enable line wrapping
//! - `_line_wrap`, `_wrap`: disable line wrapping
//! + `hide_cursor`, `nocur`: hide the cursor
//! + `show_cursor`, `_nocur`: show the cursor
//! + `save_screen`, `sscr`: saves the screen view (single slot, not stack)
//! + `load_screen`, `lscr`: restores the last saved screen view
//! + `alt_buf`, `abuf`: enable alternative buffer
//! + `_alt_buf`, `_abuf`: disable alternative buffer
//!
//! ### Compound
//! - `clear`, `cls`: erases the screen and the buffer and moves the cursor to the
//! topleft position (equivalent to `e mt,`)
//!
//! ## The uncoloring macros
//! There are also macros that will skip the terminal commands. These can be
//! useful when you need to conditionaly print with colors or without colors.
//!
//! The macros have the same names but they have `n` before the `c` to signify
//! *no color*: [`formatnc`], [`printnc`], [`printncln`], [`eprintnc`],
//! [`eprintncln`], [`writenc`] and [`writencln`].
//!
//! ## The conditionally coloring macros
//! Theese are same as the normal coloring macros except they take additional
//! first argument that tells whether the output should be colored or not.
//!
//! They have the same names as the uncoloring macros but they have `m` instead
//! of the `n` to signify *maybe color*:[`formatmc`], [`printmc`],
//! [`printmcln`], [`eprintmc`], [`eprintmcln`], [`writemc`] and [`writemcln`].
//!
//! ## Automatically coloring macros.
//! Theese are same as the normal coloring macros except the will not color the
//! output if it detects that the output stream is not terminal.
//!
//! They have the same name as the normal macros, but they have `a` before `c`
//! to signify *automatic coloring*: [`printac`], [`printacln`], [`eprintac`]
//! and [`eprintacln`].
//!
//! ## Examples
//! ### With macro
//! ```rust
//! use termal::*;
//!
//! // you can use a special macro to inline the color codes, this will write
//! // italic text with yellow foreground and reset at the end.
//! printcln!("{'yellow italic}hello{'reset}");
//!
//! // the macro also supports standard formatting
//! printcln!("{'yellow italic}{}{'reset}", "hello");
//!
//! // you can also use short versions of the codes
//! printcln!("{'y i}{}{'_}", "hello");
//!
//! // you can also use true colors with their hex codes
//! printcln!("{'#dd0 i}{}{'_}", "hello");
//! ```
//!
//! ### Without macro
//! ```rust
//! // Move cursor to position column 5 on line 7 and write 'hello' in italic
//! // yellow
//!
//! use termal::codes::*;
//! use termal::*;
//!
//! println!("{}{YELLOW_FG}{ITALIC}hello{RESET}", move_to!(5, 7));
//! ```
//!
//! ## Other macros
//!
//! The macros such as `move_to!` can accept either literals or dynamic values.
//! Its main feature is that if you supply literals, it expands to a string
//! literal with the ansi code.
//! If you however supply dynamic values it expands to a `format!` macro:
//! ```rust
//! use termal::*;
//!
//! let a = move_to!(5, 7);
//! // expands to:
//! let a = "\x1b[5;7H";
//!
//! let b = move_to!(2 + 3, 7);
//! // expands to:
//! let b = format!("\x1b[{};{}H", 2 + 3, 7);
//! ```
//!
//! If you know the values for the arguments you can also use the `*c` macros:
//! ```rust
//! use termal::formatc;
//!
//! // the spaces, or the lack of them is important
//! let a = formatc!("{'move_to5,7}");
//! ```
//!
//! ### Gradients
//! Youn can create gradients with the function `termal::gradient`:
//! ```rust
//! use termal::*;
//!
//! // This will create foreground gradient from the rgb color `(250, 50, 170)`
//! // to the rgb color `(180, 50, 240)`
//! printcln!("{}{'_}",gradient("BonnyAD9", (250, 50, 170), (180, 50, 240)));
//! ```
pub use *;
pub use termal_proc as proc;
/// Works as [`println!`], in addition can generate ansi escape codes.
/// To generate the ansi codes use `"{'...}"`.
///
/// # Examples
/// ```
/// use termal::*;
/// // Print 'hello' in yellow:
/// printcln!("{'yellow}hello{'reset}");
/// ```
/// Works as [`print!`], in addition can generate ansi escape codes.
/// To generate the ansi codes use `"{'...}"`.
///
/// # Examples
/// ```
/// use termal::*;
/// // Print 'hello' in yellow:
/// printc!("{'yellow}hello{'reset}");
/// ```
/// Works as [`eprintln!`], in addition can generate ansi escape codes.
/// To generate the ansi codes use `"{'...}"`.
///
/// # Examples
/// ```
/// use termal::*;
/// // Print 'hello' in yellow:
/// eprintcln!("{'yellow}hello{'reset}");
/// ```
/// Works as [`eprint!`], in addition can generate ansi escape codes.
/// To generate the ansi codes use `"{'...}"`.
///
/// # Examples
/// ```
/// use termal::*;
/// // Print 'hello' in yellow:
/// eprintc!("{'yellow}hello{'reset}");
/// ```
/// Works as [`format!`], in addition can generate ansi escape codes.
/// To generate the ansi codes use `"{'...}"`.
///
/// # Examples
/// ```
/// use termal::*;
/// // Generate 'hello' in yellow:
/// formatc!("{'yellow}hello{'reset}");
/// ```
/// Works as [`writeln!`], in addition can generate ansi escape codes.
/// To generate the ansi codes use `"{'...}"`.
/// Works as [`write!`], in addition can generate ansi escape codes.
/// To generate the ansi codes use `"{'...}"`.
/// Works as [`println!`], skips terminal commands in `"{'...}"`.
///
/// # Examples
/// ```
/// use termal::*;
/// // Print 'hello' (not in yellow, the terminal commands are skipped):
/// printncln!("{'yellow}hello{'reset}");
/// ```
/// Works as [`print!`], skips terminal commands in `"{'...}"`.
///
/// # Examples
/// ```
/// use termal::*;
/// // Print 'hello' (not in yellow, the terminal commands are skipped):
/// printnc!("{'yellow}hello{'reset}");
/// ```
/// Works as [`eprintln!`], skips terminal commands in `"{'...}"`.
///
/// # Examples
/// ```
/// use termal::*;
/// // Print 'hello' (not in yellow, the terminal commands are skipped):
/// eprintncln!("{'yellow}hello{'reset}");
/// ```
/// Works as [`eprint!`], skips terminal commands in `"{'...}"`.
///
/// # Examples
/// ```
/// use termal::*;
/// // Print 'hello' (not in yellow, the terminal commands are skipped):
/// printnc!("{'yellow}hello{'reset}");
/// ```
/// Works as [`format!`], skips terminal commands in `"{'...}"`.
///
/// # Examples
/// ```
/// use termal::*;
/// // Generate 'hello' (not in yellow, the terminal commands are skipped):
/// printcln!("{'yellow}hello{'reset}");
/// ```
/// Works as [`writeln!`], skips terminal commands in `"{'...}"`.
/// Works as [`write!`], skips terminal commands in `"{'...}"`.
/// Works as [`println!`], conditionally skips terminal commands in `"{'...}"`.
///
/// # Examples
/// ```
/// use termal::*;
/// // Print 'hello' (not in yellow, the terminal commands are skipped):
/// printmcln!(false, "{'yellow}hello{'reset}");
/// ```
/// Works as [`print!`], conditionally skips terminal commands in `"{'...}"`.
///
/// # Examples
/// ```
/// use termal::*;
/// // Print 'hello' (not in yellow, the terminal commands are skipped):
/// printmc!(false, "{'yellow}hello{'reset}");
/// ```
/// Works as [`eprintln!`], conditionally skips terminal commands in
/// `"{'...}"`.
///
/// # Examples
/// ```
/// use termal::*;
/// // Print 'hello' (not in yellow, the terminal commands are skipped):
/// eprintmcln!(false, "{'yellow}hello{'reset}");
/// ```
/// Works as [`eprint!`], conditionally skips terminal commands in `"{'...}"`.
///
/// # Examples
/// ```
/// use termal::*;
/// // Print 'hello' (not in yellow, the terminal commands are skipped):
/// printmc!(false, "{'yellow}hello{'reset}");
/// ```
/// Works as [`format!`], conditionally skips terminal commands in `"{'...}"`.
///
/// # Examples
/// ```
/// use termal::*;
/// // Generate 'hello' (not in yellow, the terminal commands are skipped):
/// printmcln!(false, "{'yellow}hello{'reset}");
/// ```
/// Works as [`writeln!`], conditionally skips terminal commands in `"{'...}"`.
/// Works as [`write!`], conditionally skips terminal commands in `"{'...}"`.
/// Works as [`println!`], in addition can generate ansi escape codes.
/// To generate the ansi codes use `"{'...}"`. This will not use the ansi codes
/// if stdout is not terminal.
///
/// # Examples
/// ```
/// use termal::*;
/// // Print 'hello' in yellow:
/// printcln!("{'yellow}hello{'reset}");
/// ```
/// Works as [`print!`], in addition can generate ansi escape codes.
/// To generate the ansi codes use `"{'...}"`. This will not use the ansi codes
/// if stdout is not terminal.
///
/// # Examples
/// ```
/// use termal::*;
/// // Print 'hello' in yellow:
/// printc!("{'yellow}hello{'reset}");
/// ```
/// Works as [`eprintln!`], in addition can generate ansi escape codes.
/// To generate the ansi codes use `"{'...}"`. This will not use the ansi codes
/// if stdout is not terminal.
///
/// # Examples
/// ```
/// use termal::*;
/// // Print 'hello' in yellow:
/// eprintcln!("{'yellow}hello{'reset}");
/// ```
/// Works as [`eprint!`], in addition can generate ansi escape codes.
/// To generate the ansi codes use `"{'...}"`. This will not use the ansi codes
/// if stdout is not terminal.
///
/// # Examples
/// ```
/// use termal::*;
/// // Print 'hello' in yellow:
/// eprintc!("{'yellow}hello{'reset}");
/// ```