format_string

Function format_string 

Source
pub fn format_string(str_to_format: &str, style: &str) -> String
Expand description

Function providing a way to format you cli output

ยงExamples

use termcol::*;

println!("{}", format_string("im bold", "bold"));
Examples found in repository?
examples/usage.rs (line 75)
3fn main() {
4    println!("===== COLORS =====\n");
5
6    println!("{}black", color("black"));
7    println!("{}red", color("red"));
8    println!("{}green", color("green"));
9    println!("{}gold", color("gold"));
10    println!("{}blue", color("blue"));
11    println!("{}pink", color("pink"));
12    println!("{}cyan", color("cyan"));
13    println!("{}gray", color("gray"));
14    println!("{}light red", color("light red"));
15    println!("{}light green", color("light green"));
16    println!("{}yellow", color("yellow"));
17    println!("{}purple", color("purple"));
18    println!("{}light pink", color("light pink"));
19    println!("{}light blue", color("light blue"));
20    println!("{}white", color("white"));
21    println!("{}reset\n", color("reset"));
22
23    let mut colorful_string: String = "".to_string();
24    colorful_string += &(color("black") + "! " + &color("red") + "c");
25    colorful_string += &(color("green") + "o" + &color("gold") + "l");
26    colorful_string += &(color("blue") + "o" + &color("pink") + "r");
27    colorful_string += &(color("cyan") + "f" + &color("gray") + "u");
28    colorful_string += &(color("light red") + "l" + &color("light green") + " s");
29    colorful_string += &(color("yellow") + "t" + &color("purple") + "r");
30    colorful_string += &(color("light pink") + "i" + &color("light blue") + "n");
31    colorful_string += &(color("white") + "g" + &color("reset") + " !");
32    println!("{}\n", colorful_string);
33
34    let mut string_to_color: String = "Hello, World!".to_string();
35    string_to_color = color_string(&string_to_color, "gold");
36    println!("{}", string_to_color);
37
38    let mut multi_color: String = "".to_string();
39    multi_color += &(color_string("easy ", "red") + &color_string("multi ", "green"));
40    multi_color += &(color_string("color ", "blue") + &color_string("text!", "white"));
41    println!("{}\n", multi_color);
42
43    println!("===== TRUE COLOR =====\n");
44
45    println!(
46        "{}{}True Color{} {}{}Support!{}",
47        true_color(255, 240, 32, true),
48        true_color(20, 20, 200, false),
49        color("reset"),
50        true_color_hex("0x1414C8", true),
51        true_color_hex("0xfff020", false),
52        color("reset")
53    );
54
55    println!(
56        "{} {}",
57        true_color_string("True Color", 255, 240, 32, 20, 20, 200),
58        true_color_string_hex("Support!", "0x1414C8", "0xfff020")
59    );
60
61    println!();
62
63    println!("===== FORMATING =====\n");
64
65    println!("{}bold{}", format("bold"), format("reset"));
66    println!("{}faint{}", format("faint"), format("reset"));
67    println!("{}italic{}", format("italic"), format("reset"));
68    println!("{}underline{}", format("underline"), format("reset"));
69    println!("{}blink{}", format("blink"), format("reset"));
70    println!("{}invert{}", format("invert"), format("reset"));
71    println!("{}strike{}\n", format("strike"), format("reset"));
72
73    let mut multi_format: String = "".to_string();
74    multi_format +=
75        &(format_string("easy", "italic") + " " + &format_string("multi", "underline") + " ");
76    multi_format += &(format_string("format", "invert") + " " + &format_string("text!", "strike"));
77    println!("{}\n", multi_format);
78
79    println!("===== OPTIMIZING DISPLAYING TEXT =====\n");
80
81    let mut string_to_optimize: String = "".to_string();
82    string_to_optimize +=
83        &(color_string("this text ", "reset") + &color_string("contains a lot", "reset"));
84    string_to_optimize +=
85        &(color_string(" of invisible ", "reset") + &color_string("characters.", "reset"));
86    println!(
87        "before: {}     length: {}",
88        string_to_optimize,
89        string_to_optimize.chars().count()
90    );
91    string_to_optimize = optimize(&string_to_optimize);
92    println!(
93        " after: {}     length: {}",
94        string_to_optimize,
95        string_to_optimize.chars().count()
96    );
97
98    // This is how to disable coloring/formating all together:
99    clean_output(true);
100}