1use anyhow::{Result, anyhow, bail};
2use serde::{Deserialize, Serialize};
3
4use crate::status_line::StatusLineConfig;
5use crate::terminal_title::TerminalTitleConfig;
6
7#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
8#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
9#[serde(rename_all = "snake_case")]
10#[derive(Default)]
11pub enum ToolOutputMode {
12 #[default]
13 Compact,
14 Full,
15}
16
17#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
18#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
19#[serde(rename_all = "snake_case")]
20#[derive(Default)]
21pub enum ReasoningDisplayMode {
22 Always,
23 #[default]
24 Toggle,
25 Hidden,
26}
27
28#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
30#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Default)]
31#[serde(rename_all = "snake_case")]
32pub enum LayoutModeOverride {
33 #[default]
35 Auto,
36 Compact,
38 Standard,
40 Wide,
42}
43
44#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
46#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Default)]
47#[serde(rename_all = "snake_case")]
48pub enum UiDisplayMode {
49 Full,
51 #[default]
53 Minimal,
54 Focused,
56}
57
58#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
60#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Default)]
61#[serde(rename_all = "snake_case")]
62pub enum NotificationDeliveryMode {
63 Terminal,
65 Hybrid,
67 #[default]
69 Desktop,
70}
71
72#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
74#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Default)]
75#[serde(rename_all = "snake_case")]
76pub enum NotificationBackend {
77 #[default]
79 Auto,
80 Osascript,
82 NotifyRust,
84 Terminal,
86}
87
88#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
90#[derive(Debug, Clone, Deserialize, Serialize)]
91pub struct UiNotificationsConfig {
92 #[serde(default = "default_notifications_enabled")]
94 pub enabled: bool,
95
96 #[serde(default)]
98 pub delivery_mode: NotificationDeliveryMode,
99
100 #[serde(default)]
102 pub backend: NotificationBackend,
103
104 #[serde(default = "default_notifications_suppress_when_focused")]
106 pub suppress_when_focused: bool,
107
108 #[serde(default)]
111 pub command_failure: Option<bool>,
112
113 #[serde(default = "default_notifications_tool_failure")]
115 pub tool_failure: bool,
116
117 #[serde(default = "default_notifications_error")]
119 pub error: bool,
120
121 #[serde(default = "default_notifications_completion")]
124 pub completion: bool,
125
126 #[serde(default)]
129 pub completion_success: Option<bool>,
130
131 #[serde(default)]
134 pub completion_failure: Option<bool>,
135
136 #[serde(default = "default_notifications_hitl")]
138 pub hitl: bool,
139
140 #[serde(default)]
143 pub policy_approval: Option<bool>,
144
145 #[serde(default)]
148 pub request: Option<bool>,
149
150 #[serde(default = "default_notifications_tool_success")]
152 pub tool_success: bool,
153
154 #[serde(default = "default_notifications_repeat_window_seconds")]
156 pub repeat_window_seconds: u64,
157
158 #[serde(default = "default_notifications_max_identical_in_window")]
160 pub max_identical_in_window: u32,
161}
162
163impl Default for UiNotificationsConfig {
164 fn default() -> Self {
165 Self {
166 enabled: default_notifications_enabled(),
167 delivery_mode: NotificationDeliveryMode::default(),
168 backend: NotificationBackend::default(),
169 suppress_when_focused: default_notifications_suppress_when_focused(),
170 command_failure: Some(default_notifications_command_failure()),
171 tool_failure: default_notifications_tool_failure(),
172 error: default_notifications_error(),
173 completion: default_notifications_completion(),
174 completion_success: Some(default_notifications_completion_success()),
175 completion_failure: Some(default_notifications_completion_failure()),
176 hitl: default_notifications_hitl(),
177 policy_approval: Some(default_notifications_policy_approval()),
178 request: Some(default_notifications_request()),
179 tool_success: default_notifications_tool_success(),
180 repeat_window_seconds: default_notifications_repeat_window_seconds(),
181 max_identical_in_window: default_notifications_max_identical_in_window(),
182 }
183 }
184}
185
186#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
187#[derive(Debug, Clone, Deserialize, Serialize)]
188pub struct UiFullscreenConfig {
189 #[serde(default = "default_fullscreen_mouse_capture")]
192 pub mouse_capture: bool,
193
194 #[serde(default = "default_fullscreen_copy_on_select")]
197 pub copy_on_select: bool,
198
199 #[serde(default = "default_fullscreen_scroll_speed")]
203 pub scroll_speed: u8,
204}
205
206impl Default for UiFullscreenConfig {
207 fn default() -> Self {
208 Self {
209 mouse_capture: default_fullscreen_mouse_capture(),
210 copy_on_select: default_fullscreen_copy_on_select(),
211 scroll_speed: default_fullscreen_scroll_speed(),
212 }
213 }
214}
215
216#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
217#[derive(Debug, Clone, Deserialize, Serialize)]
218pub struct UiConfig {
219 #[serde(default = "default_tool_output_mode")]
221 pub tool_output_mode: ToolOutputMode,
222
223 #[serde(default = "default_tool_output_max_lines")]
225 pub tool_output_max_lines: usize,
226
227 #[serde(default = "default_tool_output_spool_bytes")]
229 pub tool_output_spool_bytes: usize,
230
231 #[serde(default)]
233 pub tool_output_spool_dir: Option<String>,
234
235 #[serde(default = "default_allow_tool_ansi")]
237 pub allow_tool_ansi: bool,
238
239 #[serde(default = "default_inline_viewport_rows")]
241 pub inline_viewport_rows: u16,
242
243 #[serde(default = "default_reasoning_display_mode")]
245 pub reasoning_display_mode: ReasoningDisplayMode,
246
247 #[serde(default = "default_reasoning_visible_default")]
249 pub reasoning_visible_default: bool,
250
251 #[serde(default = "default_vim_mode")]
253 pub vim_mode: bool,
254
255 #[serde(default)]
257 pub status_line: StatusLineConfig,
258
259 #[serde(default)]
261 pub terminal_title: TerminalTitleConfig,
262
263 #[serde(default)]
265 pub keyboard_protocol: KeyboardProtocolConfig,
266
267 #[serde(default)]
269 pub layout_mode: LayoutModeOverride,
270
271 #[serde(default)]
273 pub display_mode: UiDisplayMode,
274
275 #[serde(default = "default_show_sidebar")]
277 pub show_sidebar: bool,
278
279 #[serde(default = "default_dim_completed_todos")]
281 pub dim_completed_todos: bool,
282
283 #[serde(default = "default_message_block_spacing")]
285 pub message_block_spacing: bool,
286
287 #[serde(default = "default_show_turn_timer")]
289 pub show_turn_timer: bool,
290
291 #[serde(default = "default_show_diagnostics_in_transcript")]
295 pub show_diagnostics_in_transcript: bool,
296
297 #[serde(default = "default_minimum_contrast")]
305 pub minimum_contrast: f64,
306
307 #[serde(default = "default_bold_is_bright")]
311 pub bold_is_bright: bool,
312
313 #[serde(default = "default_safe_colors_only")]
319 pub safe_colors_only: bool,
320
321 #[serde(default = "default_color_scheme_mode")]
326 pub color_scheme_mode: ColorSchemeMode,
327
328 #[serde(default)]
330 pub notifications: UiNotificationsConfig,
331
332 #[serde(default)]
334 pub fullscreen: UiFullscreenConfig,
335
336 #[serde(default = "default_screen_reader_mode")]
340 pub screen_reader_mode: bool,
341
342 #[serde(default = "default_reduce_motion_mode")]
345 pub reduce_motion_mode: bool,
346
347 #[serde(default = "default_reduce_motion_keep_progress_animation")]
349 pub reduce_motion_keep_progress_animation: bool,
350}
351
352#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
354#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Default)]
355#[serde(rename_all = "snake_case")]
356pub enum ColorSchemeMode {
357 #[default]
359 Auto,
360 Light,
362 Dark,
364}
365
366fn default_minimum_contrast() -> f64 {
367 crate::constants::ui::THEME_MIN_CONTRAST_RATIO
368}
369
370fn default_bold_is_bright() -> bool {
371 false
372}
373
374fn default_safe_colors_only() -> bool {
375 false
376}
377
378fn default_color_scheme_mode() -> ColorSchemeMode {
379 ColorSchemeMode::Auto
380}
381
382fn default_show_sidebar() -> bool {
383 true
384}
385
386fn default_dim_completed_todos() -> bool {
387 true
388}
389
390fn default_message_block_spacing() -> bool {
391 true
392}
393
394fn default_show_turn_timer() -> bool {
395 false
396}
397
398fn default_show_diagnostics_in_transcript() -> bool {
399 false
400}
401
402fn default_vim_mode() -> bool {
403 false
404}
405
406fn default_notifications_enabled() -> bool {
407 true
408}
409
410fn default_notifications_suppress_when_focused() -> bool {
411 true
412}
413
414fn default_notifications_command_failure() -> bool {
415 false
416}
417
418fn default_notifications_tool_failure() -> bool {
419 false
420}
421
422fn default_notifications_error() -> bool {
423 true
424}
425
426fn default_notifications_completion() -> bool {
427 true
428}
429
430fn default_notifications_completion_success() -> bool {
431 false
432}
433
434fn default_notifications_completion_failure() -> bool {
435 true
436}
437
438fn default_notifications_hitl() -> bool {
439 true
440}
441
442fn default_notifications_policy_approval() -> bool {
443 true
444}
445
446fn default_notifications_request() -> bool {
447 false
448}
449
450fn default_notifications_tool_success() -> bool {
451 false
452}
453
454fn default_notifications_repeat_window_seconds() -> u64 {
455 30
456}
457
458fn default_notifications_max_identical_in_window() -> u32 {
459 1
460}
461
462fn env_bool_var(name: &str) -> Option<bool> {
463 read_env_var(name).and_then(|v| {
464 let normalized = v.trim().to_ascii_lowercase();
465 match normalized.as_str() {
466 "1" | "true" | "yes" | "on" => Some(true),
467 "0" | "false" | "no" | "off" => Some(false),
468 _ => None,
469 }
470 })
471}
472
473fn env_u8_var(name: &str) -> Option<u8> {
474 read_env_var(name)
475 .and_then(|value| value.trim().parse::<u8>().ok())
476 .map(clamp_fullscreen_scroll_speed)
477}
478
479fn clamp_fullscreen_scroll_speed(value: u8) -> u8 {
480 value.clamp(1, 20)
481}
482
483fn default_fullscreen_mouse_capture() -> bool {
484 env_bool_var("VTCODE_FULLSCREEN_MOUSE_CAPTURE").unwrap_or(true)
485}
486
487fn default_fullscreen_copy_on_select() -> bool {
488 env_bool_var("VTCODE_FULLSCREEN_COPY_ON_SELECT").unwrap_or(true)
489}
490
491fn default_fullscreen_scroll_speed() -> u8 {
492 env_u8_var("VTCODE_FULLSCREEN_SCROLL_SPEED").unwrap_or(3)
493}
494
495fn default_screen_reader_mode() -> bool {
496 env_bool_var("VTCODE_SCREEN_READER").unwrap_or(false)
497}
498
499fn default_reduce_motion_mode() -> bool {
500 env_bool_var("VTCODE_REDUCE_MOTION").unwrap_or(false)
501}
502
503fn default_reduce_motion_keep_progress_animation() -> bool {
504 false
505}
506
507fn default_ask_questions_enabled() -> bool {
508 true
509}
510
511impl Default for UiConfig {
512 fn default() -> Self {
513 Self {
514 tool_output_mode: default_tool_output_mode(),
515 tool_output_max_lines: default_tool_output_max_lines(),
516 tool_output_spool_bytes: default_tool_output_spool_bytes(),
517 tool_output_spool_dir: None,
518 allow_tool_ansi: default_allow_tool_ansi(),
519 inline_viewport_rows: default_inline_viewport_rows(),
520 reasoning_display_mode: default_reasoning_display_mode(),
521 reasoning_visible_default: default_reasoning_visible_default(),
522 vim_mode: default_vim_mode(),
523 status_line: StatusLineConfig::default(),
524 terminal_title: TerminalTitleConfig::default(),
525 keyboard_protocol: KeyboardProtocolConfig::default(),
526 layout_mode: LayoutModeOverride::default(),
527 display_mode: UiDisplayMode::default(),
528 show_sidebar: default_show_sidebar(),
529 dim_completed_todos: default_dim_completed_todos(),
530 message_block_spacing: default_message_block_spacing(),
531 show_turn_timer: default_show_turn_timer(),
532 show_diagnostics_in_transcript: default_show_diagnostics_in_transcript(),
533 minimum_contrast: default_minimum_contrast(),
535 bold_is_bright: default_bold_is_bright(),
536 safe_colors_only: default_safe_colors_only(),
537 color_scheme_mode: default_color_scheme_mode(),
538 notifications: UiNotificationsConfig::default(),
539 fullscreen: UiFullscreenConfig::default(),
540 screen_reader_mode: default_screen_reader_mode(),
541 reduce_motion_mode: default_reduce_motion_mode(),
542 reduce_motion_keep_progress_animation: default_reduce_motion_keep_progress_animation(),
543 }
544 }
545}
546
547fn read_env_var(name: &str) -> Option<String> {
548 #[cfg(test)]
549 if let Some(override_value) = test_env_overrides::get(name) {
550 return override_value;
551 }
552
553 std::env::var(name).ok()
554}
555
556#[cfg(test)]
557mod test_env_overrides {
558 use std::collections::HashMap;
559 use std::sync::{Mutex, OnceLock};
560
561 static ENV_OVERRIDES: OnceLock<Mutex<HashMap<String, Option<String>>>> = OnceLock::new();
562
563 fn overrides() -> &'static Mutex<HashMap<String, Option<String>>> {
564 ENV_OVERRIDES.get_or_init(|| Mutex::new(HashMap::new()))
565 }
566
567 pub(super) fn get(name: &str) -> Option<Option<String>> {
568 overrides()
569 .lock()
570 .expect("env overrides lock poisoned")
571 .get(name)
572 .cloned()
573 }
574
575 pub(super) fn set(name: &str, value: Option<&str>) {
576 overrides()
577 .lock()
578 .expect("env overrides lock poisoned")
579 .insert(name.to_string(), value.map(ToOwned::to_owned));
580 }
581
582 pub(super) fn restore(name: &str, previous: Option<Option<String>>) {
583 let mut guard = overrides().lock().expect("env overrides lock poisoned");
584 match previous {
585 Some(value) => {
586 guard.insert(name.to_string(), value);
587 }
588 None => {
589 guard.remove(name);
590 }
591 }
592 }
593}
594
595#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
597#[derive(Debug, Clone, Deserialize, Serialize, Default)]
598pub struct ChatConfig {
599 #[serde(default, rename = "askQuestions", alias = "ask_questions")]
601 pub ask_questions: AskQuestionsConfig,
602}
603
604#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
606#[derive(Debug, Clone, Deserialize, Serialize)]
607pub struct AskQuestionsConfig {
608 #[serde(default = "default_ask_questions_enabled")]
610 pub enabled: bool,
611}
612
613impl Default for AskQuestionsConfig {
614 fn default() -> Self {
615 Self {
616 enabled: default_ask_questions_enabled(),
617 }
618 }
619}
620
621#[cfg(test)]
622mod tests {
623 use super::*;
624 use serial_test::serial;
625
626 fn with_env_var<F>(key: &str, value: Option<&str>, f: F)
627 where
628 F: FnOnce(),
629 {
630 let previous = test_env_overrides::get(key);
631 test_env_overrides::set(key, value);
632 f();
633 test_env_overrides::restore(key, previous);
634 }
635
636 #[test]
637 #[serial]
638 fn fullscreen_defaults_match_expected_values() {
639 let fullscreen = UiFullscreenConfig::default();
640
641 assert!(fullscreen.mouse_capture);
642 assert!(fullscreen.copy_on_select);
643 assert_eq!(fullscreen.scroll_speed, 3);
644 }
645
646 #[test]
647 #[serial]
648 fn fullscreen_env_overrides_apply_to_defaults() {
649 with_env_var("VTCODE_FULLSCREEN_MOUSE_CAPTURE", Some("0"), || {
650 with_env_var("VTCODE_FULLSCREEN_COPY_ON_SELECT", Some("false"), || {
651 with_env_var("VTCODE_FULLSCREEN_SCROLL_SPEED", Some("7"), || {
652 let fullscreen = UiFullscreenConfig::default();
653 assert!(!fullscreen.mouse_capture);
654 assert!(!fullscreen.copy_on_select);
655 assert_eq!(fullscreen.scroll_speed, 7);
656 });
657 });
658 });
659 }
660
661 #[test]
662 #[serial]
663 fn fullscreen_scroll_speed_is_clamped() {
664 with_env_var("VTCODE_FULLSCREEN_SCROLL_SPEED", Some("0"), || {
665 assert_eq!(UiFullscreenConfig::default().scroll_speed, 1);
666 });
667
668 with_env_var("VTCODE_FULLSCREEN_SCROLL_SPEED", Some("99"), || {
669 assert_eq!(UiFullscreenConfig::default().scroll_speed, 20);
670 });
671 }
672}
673
674#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
676#[derive(Debug, Clone, Deserialize, Serialize)]
677pub struct PtyConfig {
678 #[serde(default = "default_pty_enabled")]
680 pub enabled: bool,
681
682 #[serde(default = "default_pty_rows")]
684 pub default_rows: u16,
685
686 #[serde(default = "default_pty_cols")]
688 pub default_cols: u16,
689
690 #[serde(default = "default_max_pty_sessions")]
692 pub max_sessions: usize,
693
694 #[serde(default = "default_pty_timeout")]
696 pub command_timeout_seconds: u64,
697
698 #[serde(default = "default_stdout_tail_lines")]
700 pub stdout_tail_lines: usize,
701
702 #[serde(default = "default_scrollback_lines")]
704 pub scrollback_lines: usize,
705
706 #[serde(default = "default_max_scrollback_bytes")]
708 pub max_scrollback_bytes: usize,
709
710 #[serde(default)]
712 pub emulation_backend: PtyEmulationBackend,
713
714 #[serde(default = "default_large_output_threshold_kb")]
716 pub large_output_threshold_kb: usize,
717
718 #[serde(default)]
720 pub preferred_shell: Option<String>,
721
722 #[serde(default = "default_shell_zsh_fork")]
724 pub shell_zsh_fork: bool,
725
726 #[serde(default)]
728 pub zsh_path: Option<String>,
729}
730
731impl Default for PtyConfig {
732 fn default() -> Self {
733 Self {
734 enabled: default_pty_enabled(),
735 default_rows: default_pty_rows(),
736 default_cols: default_pty_cols(),
737 max_sessions: default_max_pty_sessions(),
738 command_timeout_seconds: default_pty_timeout(),
739 stdout_tail_lines: default_stdout_tail_lines(),
740 scrollback_lines: default_scrollback_lines(),
741 max_scrollback_bytes: default_max_scrollback_bytes(),
742 emulation_backend: PtyEmulationBackend::default(),
743 large_output_threshold_kb: default_large_output_threshold_kb(),
744 preferred_shell: None,
745 shell_zsh_fork: default_shell_zsh_fork(),
746 zsh_path: None,
747 }
748 }
749}
750
751#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
752#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Default)]
753#[serde(rename_all = "snake_case")]
754pub enum PtyEmulationBackend {
755 #[default]
756 Ghostty,
757 LegacyVt100,
758}
759
760impl PtyEmulationBackend {
761 #[must_use]
762 pub const fn as_str(self) -> &'static str {
763 match self {
764 Self::Ghostty => "ghostty",
765 Self::LegacyVt100 => "legacy_vt100",
766 }
767 }
768}
769
770impl PtyConfig {
771 pub fn validate(&self) -> Result<()> {
772 self.zsh_fork_shell_path()?;
773 Ok(())
774 }
775
776 pub fn zsh_fork_shell_path(&self) -> Result<Option<&str>> {
777 if !self.shell_zsh_fork {
778 return Ok(None);
779 }
780
781 let zsh_path = self
782 .zsh_path
783 .as_deref()
784 .map(str::trim)
785 .filter(|path| !path.is_empty())
786 .ok_or_else(|| {
787 anyhow!(
788 "pty.shell_zsh_fork is enabled, but pty.zsh_path is not configured. \
789 Set pty.zsh_path to an absolute path to patched zsh."
790 )
791 })?;
792
793 #[cfg(not(unix))]
794 {
795 let _ = zsh_path;
796 bail!("pty.shell_zsh_fork is only supported on Unix platforms");
797 }
798
799 #[cfg(unix)]
800 {
801 let path = std::path::Path::new(zsh_path);
802 if !path.is_absolute() {
803 bail!(
804 "pty.zsh_path '{}' must be an absolute path when pty.shell_zsh_fork is enabled",
805 zsh_path
806 );
807 }
808 if !path.exists() {
809 bail!(
810 "pty.zsh_path '{}' does not exist (required when pty.shell_zsh_fork is enabled)",
811 zsh_path
812 );
813 }
814 if !path.is_file() {
815 bail!(
816 "pty.zsh_path '{}' is not a file (required when pty.shell_zsh_fork is enabled)",
817 zsh_path
818 );
819 }
820 }
821
822 Ok(Some(zsh_path))
823 }
824}
825
826fn default_pty_enabled() -> bool {
827 true
828}
829
830fn default_pty_rows() -> u16 {
831 24
832}
833
834fn default_pty_cols() -> u16 {
835 80
836}
837
838fn default_max_pty_sessions() -> usize {
839 10
840}
841
842fn default_pty_timeout() -> u64 {
843 300
844}
845
846fn default_shell_zsh_fork() -> bool {
847 false
848}
849
850fn default_stdout_tail_lines() -> usize {
851 crate::constants::defaults::DEFAULT_PTY_STDOUT_TAIL_LINES
852}
853
854fn default_scrollback_lines() -> usize {
855 crate::constants::defaults::DEFAULT_PTY_SCROLLBACK_LINES
856}
857
858fn default_max_scrollback_bytes() -> usize {
859 25_000_000 }
863
864fn default_large_output_threshold_kb() -> usize {
865 5_000 }
867
868fn default_tool_output_mode() -> ToolOutputMode {
869 ToolOutputMode::Compact
870}
871
872fn default_tool_output_max_lines() -> usize {
873 600
874}
875
876fn default_tool_output_spool_bytes() -> usize {
877 200_000
878}
879
880fn default_allow_tool_ansi() -> bool {
881 false
882}
883
884fn default_inline_viewport_rows() -> u16 {
885 crate::constants::ui::DEFAULT_INLINE_VIEWPORT_ROWS
886}
887
888fn default_reasoning_display_mode() -> ReasoningDisplayMode {
889 ReasoningDisplayMode::Toggle
890}
891
892fn default_reasoning_visible_default() -> bool {
893 crate::constants::ui::DEFAULT_REASONING_VISIBLE
894}
895
896#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
899#[derive(Debug, Clone, Deserialize, Serialize)]
900pub struct KeyboardProtocolConfig {
901 #[serde(default = "default_keyboard_protocol_enabled")]
903 pub enabled: bool,
904
905 #[serde(default = "default_keyboard_protocol_mode")]
907 pub mode: String,
908
909 #[serde(default = "default_disambiguate_escape_codes")]
911 pub disambiguate_escape_codes: bool,
912
913 #[serde(default = "default_report_event_types")]
915 pub report_event_types: bool,
916
917 #[serde(default = "default_report_alternate_keys")]
919 pub report_alternate_keys: bool,
920
921 #[serde(default = "default_report_all_keys")]
923 pub report_all_keys: bool,
924}
925
926impl Default for KeyboardProtocolConfig {
927 fn default() -> Self {
928 Self {
929 enabled: default_keyboard_protocol_enabled(),
930 mode: default_keyboard_protocol_mode(),
931 disambiguate_escape_codes: default_disambiguate_escape_codes(),
932 report_event_types: default_report_event_types(),
933 report_alternate_keys: default_report_alternate_keys(),
934 report_all_keys: default_report_all_keys(),
935 }
936 }
937}
938
939impl KeyboardProtocolConfig {
940 pub fn validate(&self) -> Result<()> {
941 match self.mode.as_str() {
942 "default" | "full" | "minimal" | "custom" => Ok(()),
943 _ => anyhow::bail!(
944 "Invalid keyboard protocol mode '{}'. Must be: default, full, minimal, or custom",
945 self.mode
946 ),
947 }
948 }
949}
950
951fn default_keyboard_protocol_enabled() -> bool {
952 std::env::var("VTCODE_KEYBOARD_PROTOCOL_ENABLED")
953 .ok()
954 .and_then(|v| v.parse().ok())
955 .unwrap_or(true)
956}
957
958fn default_keyboard_protocol_mode() -> String {
959 std::env::var("VTCODE_KEYBOARD_PROTOCOL_MODE").unwrap_or_else(|_| "default".to_string())
960}
961
962fn default_disambiguate_escape_codes() -> bool {
963 true
964}
965
966fn default_report_event_types() -> bool {
967 true
968}
969
970fn default_report_alternate_keys() -> bool {
971 true
972}
973
974fn default_report_all_keys() -> bool {
975 false
976}