tui_markup_renderer/
utils.rs

1use std::collections::HashMap;
2use tui::{
3    style::{Color, Modifier, Style},
4    widgets::Borders,
5};
6
7pub fn extract_attribute(data: HashMap<String, String>, attribute_name: &str) -> String {
8    let default_value = "".to_string();
9    let value = data.get(attribute_name).unwrap_or(&default_value);
10    String::from(value)
11}
12
13pub fn modifier_from_str(input: &str) -> Modifier {
14    let input = input.to_lowercase();
15    let input = input.as_str();
16    match input {
17        "dim" => Modifier::DIM,
18        "bold" => Modifier::BOLD,
19        "italic" => Modifier::ITALIC,
20        "underlined" => Modifier::UNDERLINED,
21        "crossed_out" => Modifier::CROSSED_OUT,
22        "rapid_blink" => Modifier::RAPID_BLINK,
23        "slow_blink" => Modifier::SLOW_BLINK,
24        "reversed" => Modifier::REVERSED,
25        "hidden" => Modifier::HIDDEN,
26        _ => Modifier::DIM,
27    }
28}
29
30pub fn modifiers_from_str(input: &str) -> Style {
31    let values = input
32        .to_lowercase()
33        .split('|')
34        .fold(Style::default(), |old, value| {
35            let current = modifier_from_str(value);
36            old.add_modifier(current)
37        });
38    values
39}
40
41pub fn color_from_str(input: &str) -> Color {
42    let input = input.to_lowercase();
43    let input = input.as_str();
44    match input {
45        "reset" => Color::Reset,
46        "black" => Color::Black,
47        "red" => Color::Red,
48        "green" => Color::Green,
49        "yellow" => Color::Yellow,
50        "blue" => Color::Blue,
51        "magenta" => Color::Magenta,
52        "cyan" => Color::Cyan,
53        "gray" => Color::Gray,
54        "darkgray" => Color::DarkGray,
55        "lightred" => Color::LightRed,
56        "lightgreen" => Color::LightGreen,
57        "lightyellow" => Color::LightYellow,
58        "lightblue" => Color::LightBlue,
59        "lightmagenta" => Color::LightMagenta,
60        "lightcyan" => Color::LightCyan,
61        "white" => Color::White,
62        _ => Color::Reset,
63    }
64}
65
66pub fn contrast_color(input: &str) -> &str {
67    let input = input.to_lowercase();
68    let input = input.as_str();
69    match input {
70        "black" => "white",
71        "red" => "lightRed",
72        "green" => "lightgreen",
73        "yellow" => "lightyellow",
74        "blue" => "lightmagenta",
75        "magenta" => "lightmagenta",
76        "cyan" => "lightcyan",
77        "gray" => "darkgray",
78        "darkgray" => "gray",
79        "lightred" => "red",
80        "lightgreen" => "green",
81        "lightyellow" => "yellow",
82        "lightblue" => "blue",
83        "lightmagenta" => "magenta",
84        "lightcyan" => "cyan",
85        "white" => "black",
86        _ => "",
87    }
88}
89
90pub fn border_from_str(input: &str) -> Borders {
91    match input {
92        "all" => Borders::ALL,
93        "top" => Borders::TOP,
94        "bottom" => Borders::BOTTOM,
95        "left" => Borders::LEFT,
96        "right" => Borders::RIGHT,
97        _ => Borders::NONE,
98    }
99}
100
101pub fn borders_from_str(input: &str) -> Borders {
102    let values = input
103        .to_lowercase()
104        .split('|')
105        .fold(Borders::NONE, |old, value| {
106            let current = border_from_str(value);
107            current | old
108        });
109    values
110}