r256/
lib.rs

1#[cfg(windows)]
2mod virtual_processing;
3#[cfg(windows)]
4pub use virtual_processing::*;
5
6#[derive(Debug, Eq, PartialEq)]
7/// Style enum that containes both color and text utils.
8pub enum Styles {
9    Bold,
10    Italic,
11    Faint,
12    Underline,
13    Strike,
14    Invisible,
15    Blink,
16    Reverse,
17    FgColor256(u8),
18    BgColor256(u8)
19}
20
21/// Initialize the r256 for cross-platform support.
22///
23/// # Examples
24///
25/// ```
26/// r256::init();
27/// ```
28pub fn init() {
29    #[cfg(windows)]
30    enable_virtual_terminal_processing();
31}
32
33fn convert_to_string(style: &Styles) -> String {
34    match style {
35        Styles::Bold => String::from("\x1b[1m"),
36        Styles::Italic => String::from("\x1b[3m"),
37        Styles::Faint => String::from("\x1b[2m"),
38        Styles::Underline => String::from("\x1b[4m"),
39        Styles::Strike => String::from("\x1b[9m"),
40        Styles::Invisible => String::from("\x1b[8m"),
41        Styles::Blink => String::from("\x1b[5m"),
42        Styles::Reverse => String::from("\x1b[7m"),
43        Styles::FgColor256(color) => format!("\x1b[38;5;{}m", color),
44        Styles::BgColor256(color) => format!("\x1b[48;5;{}m", color)
45    }
46}
47
48/// Generate a string with given styles. 
49///
50/// # Arguments
51/// 
52/// * `styles` (`&Vec<Styles>`) - The styles will be appended to the string.
53/// * `text` (`&str`) - The text.
54/// 
55/// # Examples
56///
57/// ```
58/// let mut color_combines: Vec<r256::Styles> = Vec::new();
59/// color_combines.push(r256::Styles::FgColor256(1));
60/// color_combines.push(r256::Styles::Bold);
61/// 
62/// let output_str = r256::generate_string(&color_combines, "hello!");
63/// println!("{}", output_str);
64/// ```
65pub fn generate_string(styles: &Vec<Styles>, text: &str) -> String {
66    let mut generated_str = String::new();
67    
68    for style in styles {
69        generated_str = format!("{}{}", generated_str, convert_to_string(style));
70    }
71
72    generated_str + text + "\x1b[0m"
73}
74
75/// Generate a string and print to the stdout. 
76///
77/// # Arguments
78/// 
79/// * `styles` (`&Vec<Styles>`) - The styles will be appended to the string.
80/// * `text` (`&str`) - The text.
81/// 
82/// # Examples
83///
84/// ```
85/// let mut color_combines: Vec<r256::Styles> = Vec::new();
86/// color_combines.push(r256::Styles::FgColor256(42));
87/// color_combines.push(r256::Styles::Bold);
88/// 
89/// r256::print(&color_combines, "hello!");
90/// ```
91pub fn print(styles: &Vec<Styles>, text: &str) {
92    print!("{}", generate_string(styles, text))
93}
94
95/// Generate a string and print with new line to the stdout. 
96///
97/// # Arguments
98/// 
99/// * `styles` (`&Vec<Styles>`) - The styles will be appended to the string.
100/// * `text` (`&str`) - The text.
101/// 
102/// # Examples
103///
104/// ```
105/// let mut color_combines: Vec<r256::Styles> = Vec::new();
106/// color_combines.push(r256::Styles::FgColor256(69));
107/// color_combines.push(r256::Styles::Bold);
108/// 
109/// r256::println(&color_combines, "hello!");
110/// ```
111pub fn println(styles: &Vec<Styles>, text: &str) {
112    println!("{}", generate_string(styles, text))
113}