vtcode_ui/tui/core_tui/session/
config.rs1use hashbrown::HashMap;
6use serde::{Deserialize, Serialize};
7use std::path::Path;
8use vtcode_commons::fs::{read_file_with_context_sync, write_file_with_context_sync};
9
10#[derive(Debug, Clone, Serialize, Deserialize, Default)]
12pub struct SessionConfig {
13 appearance: AppearanceConfig,
15
16 key_bindings: KeyBindingConfig,
18
19 behavior: BehaviorConfig,
21
22 performance: PerformanceConfig,
24
25 customization: CustomizationConfig,
27}
28
29pub use vtcode_commons::ui_protocol::{
31 DiffPreviewMode, LayoutModeOverride, ReasoningDisplayMode, ThinkingBlockState, UiMode,
32};
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct AppearanceConfig {
37 pub theme: String,
39
40 pub ui_mode: UiMode,
42
43 pub show_sidebar: bool,
45
46 pub min_content_width: u16,
48
49 pub min_navigation_width: u16,
51
52 pub navigation_width_percent: u8,
54
55 pub transcript_bottom_padding: u16,
63
64 pub dim_completed_todos: bool,
66
67 #[serde(default)]
70 pub show_task_panel: bool,
71
72 pub message_block_spacing: u8,
74
75 #[serde(default)]
77 pub layout_mode: LayoutModeOverride,
78
79 #[serde(default)]
81 pub reasoning_display_mode: ReasoningDisplayMode,
82
83 #[serde(default)]
85 pub reasoning_visible_default: bool,
86
87 #[serde(default)]
89 pub thinking_display: ThinkingBlockState,
90
91 #[serde(default)]
93 pub vim_mode: bool,
94
95 #[serde(default)]
99 pub readline_mode: bool,
100
101 #[serde(default)]
103 pub screen_reader_mode: bool,
104
105 #[serde(default)]
107 pub reduce_motion_mode: bool,
108
109 #[serde(default)]
112 pub reduce_motion_keep_progress_animation: bool,
113
114 #[serde(default)]
116 pub hide_header: bool,
117
118 #[serde(default = "default_transcript_review_control")]
120 pub show_transcript_review_hints: bool,
121
122 #[serde(default = "default_transcript_review_control")]
124 pub show_transcript_review_shortcut_guide: bool,
125
126 #[serde(default = "default_transcript_review_control")]
128 pub show_transcript_review_close_button: bool,
129
130 #[serde(default)]
132 pub diff_preview_mode: DiffPreviewMode,
133
134 #[serde(default)]
139 pub syntax_highlighting: vtcode_config::loader::SyntaxHighlightingConfig,
140
141 pub customization: CustomizationConfig,
143}
144
145impl Default for AppearanceConfig {
146 fn default() -> Self {
147 Self {
148 theme: "default".to_owned(),
149 ui_mode: UiMode::Full,
150 show_sidebar: true,
151 min_content_width: 40,
152 min_navigation_width: 20,
153 navigation_width_percent: 25,
154 transcript_bottom_padding: 0,
155 dim_completed_todos: true,
156 show_task_panel: false,
157 message_block_spacing: 1,
161 layout_mode: LayoutModeOverride::Auto,
162 reasoning_display_mode: ReasoningDisplayMode::Toggle,
163 reasoning_visible_default: crate::tui::config::constants::ui::DEFAULT_REASONING_VISIBLE,
164 thinking_display: ThinkingBlockState::Collapsed,
165 vim_mode: false,
166 readline_mode: false,
167 screen_reader_mode: false,
168 reduce_motion_mode: false,
169 reduce_motion_keep_progress_animation: false,
170 hide_header: true,
171 show_transcript_review_hints: default_transcript_review_control(),
172 show_transcript_review_shortcut_guide: default_transcript_review_control(),
173 show_transcript_review_close_button: default_transcript_review_control(),
174 diff_preview_mode: DiffPreviewMode::Inline,
175 syntax_highlighting: vtcode_config::loader::SyntaxHighlightingConfig::default(),
176 customization: CustomizationConfig::default(),
177 }
178 }
179}
180
181fn default_transcript_review_control() -> bool {
182 true
183}
184
185impl AppearanceConfig {
186 pub(crate) fn should_show_sidebar(&self) -> bool {
188 match self.ui_mode {
189 UiMode::Full => self.show_sidebar,
190 UiMode::Minimal | UiMode::Focused => false,
191 }
192 }
193
194 pub(crate) fn should_show_task_panel(&self) -> bool {
196 self.show_task_panel
197 }
198
199 fn reasoning_visible(&self) -> bool {
200 match self.reasoning_display_mode {
201 ReasoningDisplayMode::Always => true,
202 ReasoningDisplayMode::Hidden => false,
203 ReasoningDisplayMode::Toggle => self.reasoning_visible_default,
204 }
205 }
206
207 pub(crate) fn thinking_collapsed_by_default(&self) -> bool {
209 self.thinking_display == ThinkingBlockState::Collapsed
210 }
211
212 pub(crate) fn should_animate_progress_status(&self) -> bool {
214 !self.screen_reader_mode && (!self.reduce_motion_mode || self.reduce_motion_keep_progress_animation)
215 }
216
217 pub fn should_show_footer(&self) -> bool {
219 match self.ui_mode {
220 UiMode::Full => true,
221 UiMode::Minimal => false,
222 UiMode::Focused => false,
223 }
224 }
225}
226
227#[derive(Debug, Clone, Serialize, Deserialize)]
229pub struct KeyBindingConfig {
230 bindings: HashMap<String, Vec<String>>,
232}
233
234impl Default for KeyBindingConfig {
235 fn default() -> Self {
236 let mut bindings = HashMap::new();
237
238 bindings.insert("scroll_up".to_owned(), vec!["up".to_owned()]);
240 bindings.insert("scroll_down".to_owned(), vec!["down".to_owned()]);
241 bindings.insert("page_up".to_owned(), vec!["pageup".to_owned()]);
242 bindings.insert("page_down".to_owned(), vec!["pagedown".to_owned()]);
243
244 bindings.insert("submit".to_owned(), vec!["enter".to_owned()]);
246 bindings.insert("submit_queue".to_owned(), vec!["tab".to_owned()]);
247 bindings.insert("cancel".to_owned(), vec!["esc".to_owned()]);
248 bindings.insert("interrupt".to_owned(), vec!["ctrl+c".to_owned()]);
249
250 Self { bindings }
251 }
252}
253
254#[derive(Debug, Clone, Serialize, Deserialize)]
256pub struct BehaviorConfig {
257 max_input_lines: usize,
259
260 enable_history: bool,
262
263 history_size: usize,
265
266 show_queued_inputs: bool,
268}
269
270impl Default for BehaviorConfig {
271 fn default() -> Self {
272 Self {
273 max_input_lines: 10,
274 enable_history: true,
275 history_size: 100,
276 show_queued_inputs: true,
277 }
278 }
279}
280
281#[derive(Debug, Clone, Serialize, Deserialize)]
283pub struct PerformanceConfig {
284 render_cache_size: usize,
286
287 transcript_cache_size: usize,
289
290 enable_transcript_caching: bool,
292
293 lru_cache_size: usize,
295
296 enable_smooth_scrolling: bool,
298}
299
300impl Default for PerformanceConfig {
301 fn default() -> Self {
302 Self {
303 render_cache_size: 1000,
304 transcript_cache_size: 500,
305 enable_transcript_caching: true,
306 lru_cache_size: 128,
307 enable_smooth_scrolling: false,
308 }
309 }
310}
311
312#[derive(Debug, Clone, Serialize, Deserialize)]
314pub struct CustomizationConfig {
315 ui_labels: HashMap<String, String>,
317
318 custom_styles: HashMap<String, String>,
320
321 enabled_features: Vec<String>,
323}
324
325impl Default for CustomizationConfig {
326 fn default() -> Self {
327 Self {
328 ui_labels: HashMap::new(),
329 custom_styles: HashMap::new(),
330 enabled_features: vec![
331 "slash_commands".to_owned(),
332 "file_palette".to_owned(),
333 "modal_dialogs".to_owned(),
334 ],
335 }
336 }
337}
338
339impl SessionConfig {
340 fn new() -> Self {
342 Self::default()
343 }
344
345 pub fn load_from_file(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
347 let content = read_file_with_context_sync(Path::new(path), "session config file")
348 .map_err(|err| -> Box<dyn std::error::Error> { Box::new(std::io::Error::other(err)) })?;
349 let config: SessionConfig = toml::from_str(&content)?;
350 Ok(config)
351 }
352
353 pub fn save_to_file(&self, path: &str) -> Result<(), Box<dyn std::error::Error>> {
355 let content = toml::to_string_pretty(self)?;
356 write_file_with_context_sync(Path::new(path), &content, "session config file")
357 .map_err(|err| -> Box<dyn std::error::Error> { Box::new(std::io::Error::other(err)) })?;
358 Ok(())
359 }
360
361 fn set_value(&mut self, key: &str, value: &str) -> Result<(), String> {
363 match key {
366 "behavior.max_input_lines" => {
367 self.behavior.max_input_lines =
368 value.parse().map_err(|_| format!("Cannot parse '{value}' as number"))?;
369 }
370 "performance.lru_cache_size" => {
371 self.performance.lru_cache_size =
372 value.parse().map_err(|_| format!("Cannot parse '{value}' as number"))?;
373 }
374 _ => return Err(format!("Unknown configuration key: {key}")),
375 }
376 Ok(())
377 }
378
379 fn get_value(&self, key: &str) -> Option<String> {
381 match key {
382 "behavior.max_input_lines" => Some(self.behavior.max_input_lines.to_string()),
383 "performance.lru_cache_size" => Some(self.performance.lru_cache_size.to_string()),
384 _ => None,
385 }
386 }
387
388 fn validate(&self) -> Result<(), Vec<String>> {
390 let mut errors = Vec::new();
391
392 if self.behavior.history_size == 0 {
393 errors.push("history_size must be greater than 0".to_owned());
394 }
395
396 if self.performance.lru_cache_size == 0 {
397 errors.push("lru_cache_size must be greater than 0".to_owned());
398 }
399
400 if self.appearance.navigation_width_percent > 100 {
401 errors.push("navigation_width_percent must be between 0 and 100".to_owned());
402 }
403
404 if errors.is_empty() { Ok(()) } else { Err(errors) }
405 }
406}
407
408#[cfg(test)]
409mod tests {
410 use super::*;
411
412 #[test]
413 fn test_default_config() {
414 let config = SessionConfig::new();
415 assert_eq!(config.behavior.history_size, 100);
416 assert_eq!(config.appearance.reasoning_display_mode, ReasoningDisplayMode::Toggle);
417 assert!(config.appearance.reasoning_visible_default);
418 assert!(config.appearance.reasoning_visible());
419 assert_eq!(config.appearance.thinking_display, ThinkingBlockState::Collapsed);
420 assert!(config.appearance.thinking_collapsed_by_default());
421 }
422
423 #[test]
424 fn test_task_panel_default_hidden() {
425 let mut config = SessionConfig::new();
426 assert!(!config.appearance.show_task_panel);
427 assert!(!config.appearance.should_show_task_panel());
428
429 config.appearance.show_task_panel = true;
430 assert!(config.appearance.should_show_task_panel());
431 }
432
433 #[test]
434 fn test_thinking_display_default_collapsed() {
435 let config = SessionConfig::new();
436 assert_eq!(config.appearance.thinking_display, ThinkingBlockState::Collapsed);
437 assert!(config.appearance.thinking_collapsed_by_default());
438 }
439
440 #[test]
441 fn test_thinking_display_override_extended() {
442 let mut config = SessionConfig::new();
443 config.appearance.thinking_display = ThinkingBlockState::Extended;
444 assert!(!config.appearance.thinking_collapsed_by_default());
445 }
446
447 #[test]
448 fn test_config_serialization() {
449 let config = SessionConfig::new();
450 let serialized = toml::to_string_pretty(&config).unwrap();
451 assert!(serialized.contains("theme"));
452 }
453
454 #[test]
455 fn test_config_value_setting() {
456 let mut config = SessionConfig::new();
457
458 config.set_value("behavior.max_input_lines", "15").unwrap();
459 assert_eq!(config.behavior.max_input_lines, 15);
460
461 assert!(config.set_value("behavior.max_input_lines", "not_a_number").is_err());
462 }
463
464 #[test]
465 fn test_config_value_getting() {
466 let config = SessionConfig::new();
467 assert_eq!(config.get_value("behavior.max_input_lines"), Some("10".to_owned()));
468 }
469
470 #[test]
471 fn test_config_validation() {
472 let config = SessionConfig::new();
473 config.validate().unwrap();
474
475 let mut invalid_config = config.clone();
477 invalid_config.behavior.history_size = 0;
478 assert!(invalid_config.validate().is_err());
479
480 let mut invalid_config2 = config.clone();
482 invalid_config2.performance.lru_cache_size = 0;
483 assert!(invalid_config2.validate().is_err());
484 }
485
486 #[test]
487 fn test_config_with_custom_values() {
488 let mut config = SessionConfig::new();
489
490 config.behavior.max_input_lines = 20;
492 config.performance.lru_cache_size = 256;
493
494 assert_eq!(config.behavior.max_input_lines, 20);
495 assert_eq!(config.performance.lru_cache_size, 256);
496 }
497}