formatting/
formatting.rs

1use dev_utils::{app_dt, format::*};
2
3
4fn main() {
5    app_dt!(file!());
6    // * Showcasing basic formatting capabilities
7    print_styles();
8    print_colors();
9    print_gradients();
10}
11
12fn print_styles() {
13    println!("\n--- Style Combinations ---\n");
14    let styles = [Style::Bold, Style::Italic, Style::Underline, Style::Dim];
15    let style_names = styles.iter().map(|style| format!("{:?}", style)).collect::<Vec<_>>();
16
17    let max_width = style_names.iter().map(|name| name.len()).max().unwrap();
18    
19    print!("{:width$}", "", width = max_width + 2);
20    for name in &style_names {
21        print!("{:width$}", name, width = max_width + 2);
22    }
23    println!();
24    
25    for (i, style1) in styles.iter().enumerate() {
26        print!("{:<width$}", style_names[i], width = max_width + 2);
27        for style2 in &styles {
28            let combined = "Sample".style(*style1).style(*style2);
29            let padding = max_width + 2 - visual_length(&combined);
30            print!("{}{}", combined, " ".repeat(padding));
31        }
32        println!();
33    }
34}
35
36fn print_colors() {
37    println!("\n--- Color Combinations (FG on BG) ---\n");
38    let colors = [BLUE, GREEN, CYAN, RED, MAGENTA, YELLOW, WHITE];
39    let color_names = ["Blue", "Green", "Cyan", "Red", "Magenta", "Yellow", "White"];
40
41    let max_width = color_names.iter().map(|name| name.len()).max().unwrap();
42    
43    print!("{:width$}", "", width = max_width + 2);
44    for name in &color_names {
45        print!("{:width$}", name, width = max_width + 2);
46    }
47    println!();
48    
49    for (i, fg_color) in colors.iter().enumerate() {
50        print!("{:<width$}", color_names[i], width = max_width + 2);
51        for bg_color in &colors {
52            let combined = " Sample ".color(*fg_color).on_color(*bg_color);
53            let padding = max_width + 2 - visual_length(&combined);
54            print!("{}{}", combined, " ".repeat(padding));
55        }
56        println!();
57    }
58}
59
60fn print_gradients() {
61    println!("\n--- Gradient Demonstrations ---\n");
62
63    fn create_gradient(start: Color, end: Color, steps: usize) -> String {
64        (0..steps).map(|i| {
65            let t = i as f32 / (steps - 1) as f32;
66            let r = (start.to_rgb().0 as f32 * (1.0 - t) + end.to_rgb().0 as f32 * t) as u8;
67            let g = (start.to_rgb().1 as f32 * (1.0 - t) + end.to_rgb().1 as f32 * t) as u8;
68            let b = (start.to_rgb().2 as f32 * (1.0 - t) + end.to_rgb().2 as f32 * t) as u8;
69            "■".color(Color::from((r, g, b)))
70        }).collect()
71    }
72
73    fn create_rectangular_gradient(width: usize, height: usize) -> String {
74        let mut result = String::new();
75        
76        for y in 0..height {
77            for x in 0..width {
78                result.push_str(&"██".color(Color::from((
79                    // * the output will look like:
80                    // .     LU: (Red)
81                    // *     RU: (Green)
82                    // ?     LD: (Blue)
83                    // *     RD: (R+G)
84                    (255.0 * (1.0 - (x as f32 / width as f32).max(y as f32 / height as f32))) as u8,                
85                    (255.0 * y as f32 / height as f32) as u8,
86                    (255.0 * x as f32 / width as f32) as u8,
87                ))));
88            }
89            result.push('\n');
90        }
91        result
92    }
93
94    println!("Linear Gradient (Red to Blue):");
95    println!("{}\n", create_gradient(RED, BLUE, 15));
96
97    println!("Rect Gradient:");
98    println!("{}", create_rectangular_gradient(32, 16));
99}