Skip to main content

promkit_widgets/text_editor/
config.rs

1use std::collections::HashSet;
2
3use promkit_core::crossterm::style::ContentStyle;
4
5use super::Mode;
6
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[cfg_attr(feature = "serde", serde(default))]
9#[derive(Clone, Default)]
10pub struct Config {
11    pub prefix: String,
12    pub mask: Option<char>,
13    #[cfg_attr(
14        feature = "serde",
15        serde(with = "termcfg::crossterm_config::content_style_serde")
16    )]
17    pub prefix_style: ContentStyle,
18    #[cfg_attr(
19        feature = "serde",
20        serde(with = "termcfg::crossterm_config::content_style_serde")
21    )]
22    pub active_char_style: ContentStyle,
23    #[cfg_attr(
24        feature = "serde",
25        serde(with = "termcfg::crossterm_config::content_style_serde")
26    )]
27    pub inactive_char_style: ContentStyle,
28    pub edit_mode: Mode,
29    pub word_break_chars: HashSet<char>,
30    pub lines: Option<usize>,
31}
32
33#[cfg(test)]
34mod tests {
35    #[cfg(feature = "serde")]
36    mod serde_compatibility {
37        use std::collections::HashSet;
38
39        use promkit_core::crossterm::style::{Attribute, Color};
40
41        use super::super::{Config, Mode};
42
43        #[test]
44        fn config_fields_are_fully_loaded_from_toml() {
45            let input = r#"
46prefix = ">> "
47mask = "*"
48prefix_style = "fg=green,attr=bold"
49active_char_style = "bg=darkcyan,attr=underlined"
50inactive_char_style = "fg=grey"
51edit_mode = "Overwrite"
52word_break_chars = [" ", ".", "/"]
53lines = 3
54"#;
55            let formatter: Config = toml::from_str(input).unwrap();
56
57            assert_eq!(formatter.prefix, ">> ");
58            assert_eq!(formatter.mask, Some('*'));
59            assert_eq!(formatter.prefix_style.foreground_color, Some(Color::Green));
60            assert!(formatter.prefix_style.attributes.has(Attribute::Bold));
61            assert_eq!(
62                formatter.active_char_style.background_color,
63                Some(Color::DarkCyan),
64            );
65            assert!(
66                formatter
67                    .active_char_style
68                    .attributes
69                    .has(Attribute::Underlined)
70            );
71            assert_eq!(
72                formatter.inactive_char_style.foreground_color,
73                Some(Color::Grey),
74            );
75            assert!(matches!(formatter.edit_mode, Mode::Overwrite));
76            assert_eq!(formatter.word_break_chars, HashSet::from([' ', '.', '/']));
77            assert_eq!(formatter.lines, Some(3));
78        }
79    }
80}