promkit_widgets/tree/
config.rs1use promkit_core::crossterm::style::{Color, ContentStyle};
2
3#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
4#[cfg_attr(feature = "serde", serde(default))]
5#[derive(Clone)]
6pub struct Config {
7 pub folded_symbol: String,
8 pub unfolded_symbol: String,
9 #[cfg_attr(
10 feature = "serde",
11 serde(with = "termcfg::crossterm_config::content_style_serde")
12 )]
13 pub active_item_style: ContentStyle,
14 #[cfg_attr(
15 feature = "serde",
16 serde(with = "termcfg::crossterm_config::content_style_serde")
17 )]
18 pub inactive_item_style: ContentStyle,
19 pub indent: usize,
20 pub lines: Option<usize>,
21}
22
23impl Default for Config {
24 fn default() -> Self {
25 Self {
26 folded_symbol: String::from("▶︎ "),
27 unfolded_symbol: String::from("▼ "),
28 active_item_style: ContentStyle {
29 foreground_color: Some(Color::DarkCyan),
30 ..Default::default()
31 },
32 inactive_item_style: ContentStyle::default(),
33 indent: 2,
34 lines: None,
35 }
36 }
37}
38
39#[cfg(test)]
40mod tests {
41 #[cfg(feature = "serde")]
42 mod serde_compatibility {
43 use promkit_core::crossterm::style::{Attribute, Color};
44
45 use super::super::Config;
46
47 #[test]
48 fn config_fields_are_fully_loaded_from_toml() {
49 let input = r#"
50folded_symbol = "> "
51unfolded_symbol = "v "
52active_item_style = "fg=cyan,attr=bold"
53inactive_item_style = "fg=grey"
54indent = 4
55lines = 9
56"#;
57 let formatter: Config = toml::from_str(input).unwrap();
58
59 assert_eq!(formatter.folded_symbol, "> ");
60 assert_eq!(formatter.unfolded_symbol, "v ");
61 assert_eq!(
62 formatter.active_item_style.foreground_color,
63 Some(Color::Cyan)
64 );
65 assert!(formatter.active_item_style.attributes.has(Attribute::Bold));
66 assert_eq!(
67 formatter.inactive_item_style.foreground_color,
68 Some(Color::Grey),
69 );
70 assert_eq!(formatter.indent, 4);
71 assert_eq!(formatter.lines, Some(9));
72 }
73 }
74}