xref: /Held/src/config/appconfig.rs (revision 984a200e159c143eb730a2df362731fe1a62fe01)
1 use crossterm::style::Color;
2 
3 #[derive(Debug, serde::Deserialize, Default)]
4 pub struct DeserializeAppOption {
5     pub line: Option<DeserializeLineOption>,
6 }
7 
8 #[derive(Debug)]
9 pub struct AppSetting {
10     pub line: LineSetting,
11 }
12 
13 #[derive(Debug, serde::Deserialize, Clone, Copy)]
14 pub struct DeserializeLineOption {
15     pub number: Option<DeserializeLineNumber>,
16     pub highlight: Option<DeserializeHighLightSetting>,
17 }
18 
19 #[derive(Debug, Clone, Copy)]
20 pub struct LineSetting {
21     pub line_num: LineNumberSetting,
22     pub highlight: HighLightSetting,
23 
24     pub prefix_width: usize,
25 }
26 
27 #[derive(Debug, serde::Deserialize, Clone, Copy)]
28 pub struct DeserializeLineNumber {
29     pub enable: bool,
30     pub background: Option<u32>,
31     pub frontground: Option<u32>,
32 }
33 
34 impl DeserializeLineNumber {
35     pub fn to_line_number_setting(setting: Option<Self>) -> LineNumberSetting {
36         let mut ret = LineNumberSetting::default();
37         if setting.is_none() {
38             return ret;
39         } else {
40             let setting = setting.unwrap();
41             if setting.background.is_some() {
42                 let color = setting.background.unwrap();
43                 let r = (color & 0xff0000) >> 16;
44                 let g = (color & 0x00ff00) >> 8;
45                 let b = color & 0x0000ff;
46 
47                 ret.background = Color::Rgb {
48                     r: r as u8,
49                     g: g as u8,
50                     b: b as u8,
51                 }
52             }
53 
54             if setting.frontground.is_some() {
55                 let color = setting.frontground.unwrap();
56                 let r = (color & 0xff0000) >> 16;
57                 let g = (color & 0x00ff00) >> 8;
58                 let b = color & 0x0000ff;
59 
60                 ret.frontground = Color::Rgb {
61                     r: r as u8,
62                     g: g as u8,
63                     b: b as u8,
64                 }
65             }
66 
67             return ret;
68         }
69     }
70 }
71 
72 #[derive(Debug, Clone, Copy)]
73 pub struct LineNumberSetting {
74     pub enable: bool,
75     pub background: Color,
76     pub frontground: Color,
77 }
78 
79 impl Default for LineNumberSetting {
80     fn default() -> Self {
81         Self {
82             enable: true,
83             background: Color::DarkGreen,
84             frontground: Color::White,
85         }
86     }
87 }
88 
89 impl Default for LineSetting {
90     fn default() -> Self {
91         Self {
92             line_num: LineNumberSetting::default(),
93             highlight: Default::default(),
94             prefix_width: 0,
95         }
96     }
97 }
98 
99 // 高亮选项
100 #[derive(Debug, serde::Deserialize, Clone, Copy)]
101 pub struct DeserializeHighLightSetting {
102     pub enable: bool,
103     pub color: u32,
104 }
105 
106 impl DeserializeHighLightSetting {
107     pub fn to_highlight_setting(&self) -> HighLightSetting {
108         let r = (self.color & 0xff0000) >> 16;
109         let g = (self.color & 0x00ff00) >> 8;
110         let b = self.color & 0x0000ff;
111 
112         HighLightSetting {
113             enable: self.enable,
114             color: Color::Rgb {
115                 r: r as u8,
116                 g: g as u8,
117                 b: b as u8,
118             },
119         }
120     }
121 }
122 
123 // 高亮选项
124 #[derive(Debug, Clone, Copy)]
125 pub struct HighLightSetting {
126     pub enable: bool,
127     pub color: Color,
128 }
129 
130 impl Default for HighLightSetting {
131     fn default() -> Self {
132         Self {
133             enable: true,
134             color: Color::DarkYellow,
135         }
136     }
137 }
138 
139 impl DeserializeAppOption {
140     pub fn to_app_setting(&self) -> AppSetting {
141         let line_setting = match self.line {
142             Some(setting) => setting.to_line_setting(),
143             None => LineSetting::default(),
144         };
145 
146         AppSetting { line: line_setting }
147     }
148 }
149 
150 impl DeserializeLineOption {
151     pub fn to_line_setting(&self) -> LineSetting {
152         let mut highlight = HighLightSetting::default();
153         if self.highlight.is_some() {
154             let h = self.highlight.unwrap();
155             highlight = h.to_highlight_setting();
156         }
157         LineSetting {
158             line_num: DeserializeLineNumber::to_line_number_setting(self.number),
159             highlight,
160             prefix_width: 0,
161         }
162     }
163 }
164