1use crate::colors::ColorScheme;
6use anyhow::Error;
7use carnelian::drawing::DisplayRotation;
8use std::convert::TryFrom;
9use std::str::FromStr;
10use virtcon_config::Config;
11
12pub const MIN_FONT_SIZE: f32 = 16.0;
13pub const MAX_FONT_SIZE: f32 = 160.0;
14
15#[derive(Debug, Default)]
16pub struct VirtualConsoleArgs {
17 pub disable: bool,
18 pub keep_log_visible: bool,
19 pub show_logo: bool,
20 pub keyrepeat: bool,
21 pub rounded_corners: bool,
22 pub boot_animation: bool,
23 pub color_scheme: ColorScheme,
24 pub keymap: String,
25 pub display_rotation: DisplayRotation,
26 pub font_size: f32,
27 pub dpi: Vec<u32>,
28 pub scrollback_rows: u32,
29 pub buffer_count: usize,
30}
31
32impl TryFrom<Config> for VirtualConsoleArgs {
33 type Error = Error;
34
35 fn try_from(config: Config) -> Result<Self, Self::Error> {
36 let keymap = match config.key_map.as_str() {
37 "qwerty" => "US_QWERTY",
38 "dvorak" => "US_DVORAK",
39 _ => &config.key_map,
40 }
41 .to_string();
42 let font_size = config.font_size.parse::<f32>()?.clamp(MIN_FONT_SIZE, MAX_FONT_SIZE);
43
44 Ok(VirtualConsoleArgs {
45 disable: config.disable,
46 keep_log_visible: config.keep_log_visible,
47 show_logo: config.show_logo,
48 keyrepeat: config.keyrepeat,
49 rounded_corners: config.rounded_corners,
50 boot_animation: config.boot_animation,
51 color_scheme: ColorScheme::from_str(&config.color_scheme)?,
52 keymap,
53 display_rotation: DisplayRotation::try_from(config.display_rotation)?,
54 font_size,
55 dpi: config.dpi,
56 scrollback_rows: config.scrollback_rows,
57 buffer_count: config.buffer_count as usize,
58 })
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65 use crate::colors::LIGHT_COLOR_SCHEME;
66
67 fn new_config() -> Config {
68 Config {
70 boot_animation: false,
71 buffer_count: 1,
72 color_scheme: "default".into(),
73 disable: false,
74 display_rotation: 0,
75 dpi: vec![],
76 font_size: "16.0".into(),
77 keep_log_visible: false,
78 show_logo: false,
79 keyrepeat: false,
80 key_map: "qwerty".into(),
81 rounded_corners: false,
82 scrollback_rows: 1024,
83 }
84 }
85
86 #[test]
87 fn check_disable() -> Result<(), Error> {
88 let config = Config { disable: true, ..new_config() };
89 let args = VirtualConsoleArgs::try_from(config)?;
90 assert_eq!(args.disable, true);
91
92 let config = Config { disable: false, ..new_config() };
93 let args = VirtualConsoleArgs::try_from(config)?;
94 assert_eq!(args.disable, false);
95
96 Ok(())
97 }
98
99 #[test]
100 fn check_keep_log_visible() -> Result<(), Error> {
101 let config = Config { keep_log_visible: true, ..new_config() };
102 let args = VirtualConsoleArgs::try_from(config)?;
103 assert_eq!(args.keep_log_visible, true);
104
105 let config = Config { keep_log_visible: false, ..new_config() };
106 let args = VirtualConsoleArgs::try_from(config)?;
107 assert_eq!(args.keep_log_visible, false);
108
109 Ok(())
110 }
111
112 #[test]
113 fn check_show_logo() -> Result<(), Error> {
114 let config = Config { show_logo: true, ..new_config() };
115 let args = VirtualConsoleArgs::try_from(config)?;
116 assert_eq!(args.show_logo, true);
117
118 let config = Config { show_logo: false, ..new_config() };
119 let args = VirtualConsoleArgs::try_from(config)?;
120 assert_eq!(args.show_logo, false);
121
122 Ok(())
123 }
124
125 #[test]
126 fn check_keyrepeat() -> Result<(), Error> {
127 let config = Config { keyrepeat: true, ..new_config() };
128 let args = VirtualConsoleArgs::try_from(config)?;
129 assert_eq!(args.keyrepeat, true);
130
131 let config = Config { keyrepeat: false, ..new_config() };
132 let args = VirtualConsoleArgs::try_from(config)?;
133 assert_eq!(args.keyrepeat, false);
134
135 Ok(())
136 }
137
138 #[test]
139 fn check_rounded_corners() -> Result<(), Error> {
140 let config = Config { rounded_corners: true, ..new_config() };
141 let args = VirtualConsoleArgs::try_from(config)?;
142 assert_eq!(args.rounded_corners, true);
143
144 let config = Config { rounded_corners: false, ..new_config() };
145 let args = VirtualConsoleArgs::try_from(config)?;
146 assert_eq!(args.rounded_corners, false);
147
148 Ok(())
149 }
150
151 #[test]
152 fn check_boot_animation() -> Result<(), Error> {
153 let config = Config { boot_animation: true, ..new_config() };
154 let args = VirtualConsoleArgs::try_from(config)?;
155 assert_eq!(args.boot_animation, true);
156
157 let config = Config { boot_animation: false, ..new_config() };
158 let args = VirtualConsoleArgs::try_from(config)?;
159 assert_eq!(args.boot_animation, false);
160
161 Ok(())
162 }
163
164 #[test]
165 fn check_color_scheme() -> Result<(), Error> {
166 let config = Config { color_scheme: "light".into(), ..new_config() };
167 let args = VirtualConsoleArgs::try_from(config)?;
168 assert_eq!(args.color_scheme, LIGHT_COLOR_SCHEME);
169
170 let config = Config { ..new_config() };
171 let args = VirtualConsoleArgs::try_from(config)?;
172 assert_eq!(args.color_scheme, ColorScheme::default());
173
174 Ok(())
175 }
176
177 #[test]
178 fn check_keymap() -> Result<(), Error> {
179 let config = Config { key_map: "US_DVORAK".into(), ..new_config() };
180 let args = VirtualConsoleArgs::try_from(config)?;
181 assert_eq!(args.keymap, "US_DVORAK");
182
183 let config = Config { key_map: "dvorak".into(), ..new_config() };
184 let args = VirtualConsoleArgs::try_from(config)?;
185 assert_eq!(args.keymap, "US_DVORAK");
186
187 let config = Config { ..new_config() };
188 let args = VirtualConsoleArgs::try_from(config)?;
189 assert_eq!(args.keymap, "US_QWERTY");
190
191 Ok(())
192 }
193
194 #[test]
195 fn check_display_rotation() -> Result<(), Error> {
196 let config = Config { display_rotation: 90, ..new_config() };
197 let args = VirtualConsoleArgs::try_from(config)?;
198 assert_eq!(args.display_rotation, DisplayRotation::Deg90);
199
200 let config = Config { display_rotation: 0, ..new_config() };
201 let args = VirtualConsoleArgs::try_from(config)?;
202 assert_eq!(args.display_rotation, DisplayRotation::Deg0);
203
204 Ok(())
205 }
206
207 #[test]
208 fn check_font_size() -> Result<(), Error> {
209 let config = Config { font_size: "32.0".into(), ..new_config() };
210 let args = VirtualConsoleArgs::try_from(config)?;
211 assert_eq!(args.font_size, 32.0);
212
213 let config = Config { font_size: "1000000.0".into(), ..new_config() };
214 let args = VirtualConsoleArgs::try_from(config)?;
215 assert_eq!(args.font_size, MAX_FONT_SIZE);
216
217 Ok(())
218 }
219
220 #[test]
221 fn check_dpi() -> Result<(), Error> {
222 let config = Config { dpi: vec![160, 320, 480, 640], ..new_config() };
223 let args = VirtualConsoleArgs::try_from(config)?;
224 assert_eq!(args.dpi, vec![160, 320, 480, 640]);
225
226 Ok(())
227 }
228
229 #[test]
230 fn check_scrollback_rows() -> Result<(), Error> {
231 let config = Config { scrollback_rows: 10_000, ..new_config() };
232 let args = VirtualConsoleArgs::try_from(config)?;
233 assert_eq!(args.scrollback_rows, 10_000);
234
235 Ok(())
236 }
237
238 #[test]
239 fn check_buffer_count() -> Result<(), Error> {
240 let config = Config { buffer_count: 2, ..new_config() };
241 let args = VirtualConsoleArgs::try_from(config)?;
242 assert_eq!(args.buffer_count, 2);
243
244 Ok(())
245 }
246}