tempera/
lib.rs

1//! Template based terminal coloring made really easy.
2
3extern crate lazy_static;
4
5mod codes;
6mod custom;
7mod styling;
8mod templating;
9
10pub use codes::CODES;
11pub use custom::{add_style, delete_styles, resolve_styles, CustomStyleError};
12pub use styling::ColorError;
13pub use templating::{clean_template, colorize_template};
14
15/// Colorize a string using given styles.
16pub fn colorize(content: &str, styles: &[&str]) -> String {
17  let mut header = String::new();
18  let mut footer = String::new();
19
20  // For each requested style
21  for style in styles {
22    // Split styles by space
23    let tokens: Vec<&str> = style.split(" ").collect();
24
25    // Resolve custom styles
26    for resolved in custom::resolve_styles(&tokens) {
27      // Translate style to ANSI escape codes
28      let (open, close) = styling::style_to_ansi(&resolved);
29
30      // If at least one of the codes are valid, prepend and append to the string
31      if open != "" && close != "" {
32        header.push_str(open.as_str());
33        footer.insert_str(0, close.as_str());
34      }
35    }
36  }
37
38  format!("{}{}{}", header, content, footer)
39}