use std::env;
use std::fmt;
use serde::{Deserialize, Deserializer, Serialize};
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum ShellPromptProfile {
#[default]
Auto,
UnixLike,
#[serde(rename = "powershell")]
PowerShell,
}
impl ShellPromptProfile {
pub(crate) fn as_str(self) -> &'static str {
match self {
Self::Auto => "auto",
Self::UnixLike => "unix_like",
Self::PowerShell => "powershell",
}
}
pub(crate) fn parse(value: &str) -> Option<Self> {
let normalized = value.trim();
if normalized.eq_ignore_ascii_case("auto") {
Some(Self::Auto)
} else if normalized.eq_ignore_ascii_case("unix_like")
|| normalized.eq_ignore_ascii_case("unix-like")
|| normalized.eq_ignore_ascii_case("unix")
|| normalized.eq_ignore_ascii_case("posix")
{
Some(Self::UnixLike)
} else if normalized.eq_ignore_ascii_case("powershell")
|| normalized.eq_ignore_ascii_case("power_shell")
|| normalized.eq_ignore_ascii_case("windows")
{
Some(Self::PowerShell)
} else {
None
}
}
pub fn allowed_values() -> &'static [&'static str] {
&["auto", "unix_like", "powershell"]
}
pub fn resolve_for_current_platform(self) -> ResolvedShellPromptProfile {
self.resolve_for_platform(ShellProfilePlatform::current())
}
pub(crate) fn resolve_for_platform(self, platform: ShellProfilePlatform) -> ResolvedShellPromptProfile {
match self {
Self::Auto => match platform {
ShellProfilePlatform::NativeWindows => ResolvedShellPromptProfile::PowerShell,
ShellProfilePlatform::UnixLike | ShellProfilePlatform::Wsl => ResolvedShellPromptProfile::UnixLike,
},
Self::UnixLike => ResolvedShellPromptProfile::UnixLike,
Self::PowerShell => ResolvedShellPromptProfile::PowerShell,
}
}
}
impl fmt::Display for ShellPromptProfile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for ShellPromptProfile {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let raw = String::deserialize(deserializer)?;
if let Some(parsed) = Self::parse(&raw) {
Ok(parsed)
} else {
Ok(Self::default())
}
}
}
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ShellProfilePlatform {
UnixLike,
Wsl,
NativeWindows,
}
impl ShellProfilePlatform {
fn current() -> Self {
if cfg!(windows) {
Self::NativeWindows
} else if is_wsl_environment() {
Self::Wsl
} else {
Self::UnixLike
}
}
}
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ResolvedShellPromptProfile {
UnixLike,
#[serde(rename = "powershell")]
PowerShell,
}
impl ResolvedShellPromptProfile {
pub(crate) fn as_str(self) -> &'static str {
match self {
Self::UnixLike => "unix_like",
Self::PowerShell => "powershell",
}
}
}
impl fmt::Display for ResolvedShellPromptProfile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
fn is_wsl_environment() -> bool {
if env::var_os("WSL_DISTRO_NAME").is_some() || env::var_os("WSL_INTEROP").is_some() {
return true;
}
for path in ["/proc/sys/kernel/osrelease", "/proc/version"] {
if let Ok(contents) = std::fs::read_to_string(path)
&& contents.to_ascii_lowercase().contains("microsoft")
{
return true;
}
}
false
}