use std::path::{Path, PathBuf};
use crate::util::home_dir;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum UnixShell {
Posix,
Fish,
}
pub(crate) struct UnixShellProfile {
pub(crate) label: &'static str,
pub(crate) path: PathBuf,
pub(crate) shell: UnixShell,
}
pub(crate) fn unix_shell_profile() -> Result<UnixShellProfile, String> {
let shell = std::env::var_os("SHELL")
.and_then(|value| PathBuf::from(value).file_name().map(|name| name.to_owned()))
.and_then(|name| name.to_str().map(str::to_owned));
let profile = match shell.as_deref() {
Some("zsh") => UnixShellProfile {
label: "zprofile",
path: home_dir().join(".zprofile"),
shell: UnixShell::Posix,
},
Some("bash") if cfg!(target_os = "macos") => UnixShellProfile {
label: "bash_profile",
path: home_dir().join(".bash_profile"),
shell: UnixShell::Posix,
},
Some("bash") => UnixShellProfile {
label: "bashrc",
path: home_dir().join(".bashrc"),
shell: UnixShell::Posix,
},
Some("sh" | "dash" | "ksh") => UnixShellProfile {
label: "profile",
path: home_dir().join(".profile"),
shell: UnixShell::Posix,
},
Some("fish") => UnixShellProfile {
label: "fish config",
path: home_dir().join(".config/fish/config.fish"),
shell: UnixShell::Fish,
},
None if cfg!(target_os = "macos") => UnixShellProfile {
label: "zprofile",
path: home_dir().join(".zprofile"),
shell: UnixShell::Posix,
},
None => UnixShellProfile {
label: "profile",
path: home_dir().join(".profile"),
shell: UnixShell::Posix,
},
Some(shell) => {
return Err(format!(
"automatic persistence for the {shell} user environment is not supported"
));
}
};
Ok(profile)
}
pub(crate) fn display_paths(paths: &[PathBuf]) -> String {
paths
.iter()
.map(|path| path.display().to_string())
.collect::<Vec<_>>()
.join(", ")
}
pub(crate) fn paths_equal(left: &Path, right: &Path) -> bool {
if cfg!(windows) {
left.to_string_lossy()
.eq_ignore_ascii_case(&right.to_string_lossy())
} else {
left == right
}
}