main/
main.rs

1extern crate term_painter;
2
3use term_painter::{ToStyle, Color};
4use term_painter::Color::*;
5use term_painter::Attr::*;
6
7fn main() {
8    struct_sizes();
9
10    simple_examples();
11    with_example();
12    doc_examples();
13
14    all_styles(
15        &[NotSet, Black, Red, Green, Yellow, Blue, Magenta, Cyan, White]);
16    all_styles(
17        &[BrightBlack, BrightRed, BrightGreen, BrightYellow, BrightBlue,
18         BrightMagenta, BrightCyan, BrightWhite]);
19}
20
21fn struct_sizes() {
22    use std::mem::size_of;
23
24    println!("size_of(Style): {}", size_of::<term_painter::Style>());
25    println!("size_of(Color): {}", size_of::<Color>());
26    println!("size_of(Attr):  {}", size_of::<term_painter::Attr>());
27}
28
29fn simple_examples() {
30    println!("{} | {} | {} | {} | {}",
31        Red.bg(Green).bold().paint("Red-Green-Bold"),
32        Blue.paint("Blue"),
33        Blue.bold().paint("BlueBold"),
34        Blue.bg(Magenta).paint("BlueMagentaBG"),
35        Plain.underline().paint("Underline"));
36}
37
38fn with_example() {
39    Red.with(|| {
40         print!("JustRed");
41         Bold.with(|| {
42             print!(" BoldRed {} BoldRed ", Underline.paint("Underline"));
43         });
44         print!("JustRed ");
45
46         print!("{}", Blue.paint("Blue (overwrite) "));
47         Green.with(|| {
48             println!("Green (overwrite)");
49         });
50     });
51}
52
53fn doc_examples() {
54    // --- Doc example 1
55    println!("{} or {} or {}",
56        Red.paint("Red"),
57        Bold.paint("Bold"),
58        Red.bold().paint("Both!"));
59
60    // --- Doc example 2
61    let x = 5;
62
63    // These two are equivalent
64    println!("{} | {}", x, Plain.paint(x));
65
66    // These two are equivalent, too
67    println!("{} | {}", Red.paint(x), Plain.fg(Red).paint(x));
68
69    // --- Doc example 3
70    let non_copy = "cake".to_string();  // String is *not* Copy
71    let copy = 27;  // usize/isize *is* Copy
72
73    println!("{}", Plain.paint(&non_copy));
74    println!("{}", Plain.paint(&copy));
75    // non_copy is still usable here...
76    // copy is still usable here...
77
78    println!("{}", Plain.paint(non_copy));
79    println!("{}", Plain.paint(copy));
80    // non_copy was moved into paint, so it not usable anymore...
81    // copy is still usable here...
82}
83
84fn all_styles(colors: &[Color]) {
85    // Normal test
86    for c in colors { print!("{:?} ", c.paint(c)); }
87    println!("    (fg)");
88    for c in colors { print!("{:?} ", Plain.bg(*c).paint(c)); }
89    println!("    (bg)");
90
91    // Bold text
92    for c in colors { print!("{:?} ", c.bold().paint(c)); }
93    println!("    (bold fg)");
94    for c in colors { print!("{:?} ", Bold.bg(*c).paint(c)); }
95    println!("    (bold bg)");
96
97    // Dim text
98    for c in colors { print!("{:?} ", c.dim().paint(c)); }
99    println!("    (dim fg)");
100    for c in colors { print!("{:?} ", Dim.bg(*c).paint(c)); }
101    println!("    (dim bg)");
102
103    // Underlined text
104    for c in colors { print!("{:?} ", c.underline().paint(c)); }
105    println!("    (underline fg)");
106    for c in colors { print!("{:?} ", Underline.bg(*c).paint(c)); }
107    println!("    (underline bg)");
108
109    // Blinking text
110    for c in colors { print!("{:?} ", c.blink().paint(c)); }
111    println!("    (blink fg)");
112    for c in colors { print!("{:?} ", Blink.bg(*c).paint(c)); }
113    println!("    (blink bg)");
114
115    // Reverse text
116    for c in colors { print!("{:?} ", c.reverse().paint(c)); }
117    println!("    (reverse fg)");
118    for c in colors { print!("{:?} ", Reverse.bg(*c).paint(c)); }
119    println!("    (reverse bg)");
120
121    // Secure text
122    for c in colors { print!("{:?} ", c.secure().paint(c)); }
123    println!("    (secure fg)");
124    for c in colors { print!("{:?} ", Secure.bg(*c).paint(c)); }
125    println!("    (secure bg)");
126
127}