multiple_sources/
multiple_sources.rs

1use std::fs::File;
2use std::io::Read;
3use std::path::Path;
4use termio::{prelude::*, style};
5
6fn main() {
7    // Create a new Termio instance
8    let mut tcss = Termio::new();
9
10    // Load basic styles from string
11    let base_styles = r#"
12        @element "base" {
13            color: white;
14            background: black;
15            decoration: bold;
16            padding: 1;
17        }
18
19        @element "subtle" {
20            color: i-black;
21            background: black;
22            decoration: italic;
23        }
24    "#;
25
26    // Parse basic styles
27    match tcss.parse(base_styles) {
28        Ok(_) => println!("Basic styles successfully loaded from string!"),
29        Err(e) => {
30            eprintln!("Error parsing basic styles: {}", e);
31            return;
32        }
33    }
34
35    // Load additional styles from file
36    let additional_styles_path = Path::new("examples/styles.tcss");
37    let mut additional_styles = String::new();
38
39    // Read the file
40    let mut file = match File::open(&additional_styles_path) {
41        Ok(file) => file,
42        Err(e) => {
43            eprintln!("Failed to open style file: {}", e);
44            return;
45        }
46    };
47
48    if let Err(e) = file.read_to_string(&mut additional_styles) {
49        eprintln!("Failed to read style file: {}", e);
50        return;
51    }
52
53    // Parse additional styles
54    match tcss.parse(&additional_styles) {
55        Ok(_) => println!("Additional styles successfully loaded from file!"),
56        Err(e) => {
57            eprintln!("Error parsing additional styles: {}", e);
58            return;
59        }
60    }
61
62    // Add another custom style via API
63    let mut custom_style = Style::new();
64    custom_style.fg = Some(Color::Magenta);
65    custom_style.decoration = Some(vec![Decoration::Bold, Decoration::Underline]);
66    custom_style.padding = Some(1);
67    custom_style.padding_left = Some(6);
68    custom_style.padding_right = Some(6);
69    custom_style.border_style = Some(BorderStyle::Rounded);
70
71    // Add style to the parser
72    tcss.add_style("custom", custom_style);
73    println!("Custom style successfully added via API!");
74
75    let style_from_macros = style! {
76        fg: Color::IntenseBlack,
77        bg: Color::Black,
78        decoration: vec![Decoration::Strikethrough],  
79        padding: 1,
80        border_style: BorderStyle::Dashed,
81    };
82    
83    tcss.add_style("macros", style_from_macros);
84    println!("Macros style successfully added via macros!");
85
86    // Display all styles
87    println!("Examples of using styles from different sources:");
88    println!("{}", "Base style from string".style("base", &tcss));
89    println!("{}", "Subtle style from string".style("subtle", &tcss));
90    println!("{}", "Header from file".style("header", &tcss));
91    println!("{}", "Warning from file".style("warning", &tcss));
92    println!("{}", "Custom style via API".style("custom", &tcss));
93    println!("{}", "Macros style".style("macros", &tcss));
94}