vtcode_config/types/
shell_profile.rs1use std::env;
5use std::fmt;
6
7use serde::{Deserialize, Deserializer, Serialize};
8
9#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
15#[serde(rename_all = "snake_case")]
16#[derive(Default)]
17pub enum ShellPromptProfile {
18 #[default]
21 Auto,
22 UnixLike,
24 #[serde(rename = "powershell")]
30 PowerShell,
31}
32
33impl ShellPromptProfile {
34 pub(crate) fn as_str(self) -> &'static str {
36 match self {
37 Self::Auto => "auto",
38 Self::UnixLike => "unix_like",
39 Self::PowerShell => "powershell",
40 }
41 }
42
43 pub(crate) fn parse(value: &str) -> Option<Self> {
45 let normalized = value.trim();
46 if normalized.eq_ignore_ascii_case("auto") {
47 Some(Self::Auto)
48 } else if normalized.eq_ignore_ascii_case("unix_like")
49 || normalized.eq_ignore_ascii_case("unix-like")
50 || normalized.eq_ignore_ascii_case("unix")
51 || normalized.eq_ignore_ascii_case("posix")
52 {
53 Some(Self::UnixLike)
54 } else if normalized.eq_ignore_ascii_case("powershell")
55 || normalized.eq_ignore_ascii_case("power_shell")
56 || normalized.eq_ignore_ascii_case("windows")
57 {
58 Some(Self::PowerShell)
59 } else {
60 None
61 }
62 }
63
64 pub fn allowed_values() -> &'static [&'static str] {
66 &["auto", "unix_like", "powershell"]
67 }
68
69 pub fn resolve_for_current_platform(self) -> ResolvedShellPromptProfile {
71 self.resolve_for_platform(ShellProfilePlatform::current())
72 }
73
74 pub(crate) fn resolve_for_platform(self, platform: ShellProfilePlatform) -> ResolvedShellPromptProfile {
76 match self {
77 Self::Auto => match platform {
78 ShellProfilePlatform::NativeWindows => ResolvedShellPromptProfile::PowerShell,
79 ShellProfilePlatform::UnixLike | ShellProfilePlatform::Wsl => ResolvedShellPromptProfile::UnixLike,
80 },
81 Self::UnixLike => ResolvedShellPromptProfile::UnixLike,
82 Self::PowerShell => ResolvedShellPromptProfile::PowerShell,
83 }
84 }
85}
86
87impl fmt::Display for ShellPromptProfile {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 f.write_str(self.as_str())
90 }
91}
92
93impl<'de> Deserialize<'de> for ShellPromptProfile {
94 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
95 where
96 D: Deserializer<'de>,
97 {
98 let raw = String::deserialize(deserializer)?;
99 if let Some(parsed) = Self::parse(&raw) {
100 Ok(parsed)
101 } else {
102 Ok(Self::default())
103 }
104 }
105}
106
107#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
109#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
110#[serde(rename_all = "snake_case")]
111pub enum ShellProfilePlatform {
112 UnixLike,
113 Wsl,
114 NativeWindows,
115}
116
117impl ShellProfilePlatform {
118 fn current() -> Self {
119 if cfg!(windows) {
120 Self::NativeWindows
121 } else if is_wsl_environment() {
122 Self::Wsl
123 } else {
124 Self::UnixLike
125 }
126 }
127}
128
129#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
131#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
132#[serde(rename_all = "snake_case")]
133pub enum ResolvedShellPromptProfile {
134 UnixLike,
135 #[serde(rename = "powershell")]
138 PowerShell,
139}
140
141impl ResolvedShellPromptProfile {
142 pub(crate) fn as_str(self) -> &'static str {
143 match self {
144 Self::UnixLike => "unix_like",
145 Self::PowerShell => "powershell",
146 }
147 }
148}
149
150impl fmt::Display for ResolvedShellPromptProfile {
151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152 f.write_str(self.as_str())
153 }
154}
155
156fn is_wsl_environment() -> bool {
157 if env::var_os("WSL_DISTRO_NAME").is_some() || env::var_os("WSL_INTEROP").is_some() {
158 return true;
159 }
160
161 for path in ["/proc/sys/kernel/osrelease", "/proc/version"] {
162 if let Ok(contents) = std::fs::read_to_string(path)
163 && contents.to_ascii_lowercase().contains("microsoft")
164 {
165 return true;
166 }
167 }
168
169 false
170}