tui_markup_renderer/
styles.rs1#![allow(unused_imports)]
2use std::collections::HashMap;
3use std::fmt;
4use std::io::Stdout;
5
6use tui::style::Style;
7
8pub trait IStylesStorage {
9 fn has_rule(&self, name: String) -> bool;
10 fn add_rule(&mut self, name: String, styles: Style) -> &mut Self;
11 fn get_rule(&self, name: String) -> Style;
12}
13
14#[derive(Default)]
15pub struct StylesStorage {
16 storage: HashMap<String, Style>,
17}
18
19impl StylesStorage {
20 pub fn new() -> Self {
21 StylesStorage {
22 storage: HashMap::new(),
23 }
24 }
25}
26
27impl IStylesStorage for StylesStorage {
28 fn add_rule(&mut self, name: String, styles: Style) -> &mut Self {
29 self.storage.entry(name).or_insert(styles);
30 self
31 }
32
33 fn has_rule(&self, name: String) -> bool {
34 self.storage.contains_key(&name)
35 }
36
37 fn get_rule(&self, name: String) -> Style {
38 let opt = self.storage.get(&name);
39 if let Some(value) = opt {
40 *value
41 } else {
42 Style::default()
43 }
44 }
45}
46
47impl fmt::Debug for StylesStorage {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 let mut r = f.debug_struct("RenderStorage");
50 r.field("Components", &self.storage.keys());
51 r.finish()
52 }
53}
54