Added support for powershell on windows and linux, information if powershell is not installed and prevents running that profile
This commit is contained in:
1 parent
7260e2411f
commit
c05dc3f9f5
3 files changed
+237
-41
No files matched your search
+146
-13
@@ -136,6 +136,109 @@ pub enum Shell {
|
||||
PowerShell,
|
||||
}
|
||||
|
||||
impl Shell {
|
||||
/// Why this shell cannot run on the current system, or `None` when it can.
|
||||
///
|
||||
/// Availability is derived from the filesystem each time it is asked, so a
|
||||
/// profile copied from another platform works as soon as the executable is
|
||||
/// installed. It is never stored in profile files.
|
||||
pub fn unavailable_reason(self) -> Option<String> {
|
||||
match self {
|
||||
Self::Default => None,
|
||||
Self::PowerShell => power_shell_program().err(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolves the executable for a shell, returning a user-facing error when it
|
||||
/// is missing.
|
||||
fn shell_program(shell: Shell) -> Result<std::ffi::OsString, String> {
|
||||
match shell {
|
||||
Shell::Default => Ok(default_shell_program()),
|
||||
Shell::PowerShell => power_shell_program().map(std::path::PathBuf::into_os_string),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn default_shell_program() -> std::ffi::OsString {
|
||||
std::env::var_os("SHELL")
|
||||
.filter(|shell| !shell.is_empty())
|
||||
.unwrap_or_else(|| "/bin/sh".into())
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn default_shell_program() -> std::ffi::OsString {
|
||||
std::env::var_os("COMSPEC")
|
||||
.filter(|value| !value.is_empty())
|
||||
.unwrap_or_else(|| "cmd.exe".into())
|
||||
}
|
||||
|
||||
/// Finds PowerShell 7 first, then falls back to Windows PowerShell on Windows.
|
||||
#[cfg(windows)]
|
||||
fn power_shell_program() -> Result<std::path::PathBuf, String> {
|
||||
if let Some(path) = find_in_path("pwsh.exe").or_else(|| {
|
||||
std::env::var_os("ProgramFiles")
|
||||
.map(std::path::PathBuf::from)
|
||||
.map(|dir| dir.join("PowerShell").join("7").join("pwsh.exe"))
|
||||
.filter(|path| path.is_file())
|
||||
}) {
|
||||
return Ok(path);
|
||||
}
|
||||
|
||||
if let Some(path) = find_in_path("powershell.exe").or_else(|| {
|
||||
std::env::var_os("SystemRoot")
|
||||
.map(std::path::PathBuf::from)
|
||||
.map(|dir| {
|
||||
dir.join("System32")
|
||||
.join("WindowsPowerShell")
|
||||
.join("v1.0")
|
||||
.join("powershell.exe")
|
||||
})
|
||||
.filter(|path| path.is_file())
|
||||
}) {
|
||||
return Ok(path);
|
||||
}
|
||||
|
||||
Err(power_shell_missing())
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn power_shell_program() -> Result<std::path::PathBuf, String> {
|
||||
find_in_path("pwsh").ok_or_else(power_shell_missing)
|
||||
}
|
||||
|
||||
fn power_shell_missing() -> String {
|
||||
if cfg!(windows) {
|
||||
"PowerShell was not found. Install PowerShell 7 (pwsh.exe) or enable Windows PowerShell (powershell.exe).".to_owned()
|
||||
} else {
|
||||
"PowerShell was not found. Install PowerShell 7 (pwsh) to run this profile.".to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
fn find_in_path(name: &str) -> Option<std::path::PathBuf> {
|
||||
let path = std::env::var_os("PATH")?;
|
||||
find_in_search_path(name, &path)
|
||||
}
|
||||
|
||||
fn find_in_search_path(name: &str, search_path: &std::ffi::OsStr) -> Option<std::path::PathBuf> {
|
||||
std::env::split_paths(search_path)
|
||||
.map(|dir| dir.join(name))
|
||||
.find(|candidate| is_executable(candidate))
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn is_executable(path: &Path) -> bool {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
path.metadata()
|
||||
.is_ok_and(|metadata| metadata.is_file() && metadata.permissions().mode() & 0o111 != 0)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn is_executable(path: &Path) -> bool {
|
||||
path.is_file()
|
||||
}
|
||||
|
||||
/// A running terminal session: an emulator fed by a local PTY.
|
||||
pub struct Session {
|
||||
emulator: Emulator,
|
||||
@@ -455,20 +558,17 @@ mod pty {
|
||||
|
||||
impl Pty {
|
||||
pub fn spawn(start_dir: &Path, rows: u16, cols: u16) -> Result<Self, String> {
|
||||
let shell = std::env::var_os("SHELL")
|
||||
.filter(|shell| !shell.is_empty())
|
||||
.unwrap_or_else(|| OsStr::new("/bin/sh").to_owned());
|
||||
|
||||
Self::spawn_shell(start_dir, rows, cols, &shell)
|
||||
Self::spawn_with(start_dir, rows, cols, Shell::Default)
|
||||
}
|
||||
|
||||
pub fn spawn_with(
|
||||
start_dir: &Path,
|
||||
rows: u16,
|
||||
cols: u16,
|
||||
_shell: Shell,
|
||||
shell: Shell,
|
||||
) -> Result<Self, String> {
|
||||
Self::spawn(start_dir, rows, cols)
|
||||
let program = super::shell_program(shell)?;
|
||||
Self::spawn_shell(start_dir, rows, cols, &program)
|
||||
}
|
||||
|
||||
fn spawn_shell(
|
||||
@@ -700,12 +800,7 @@ mod pty {
|
||||
cols: u16,
|
||||
shell: Shell,
|
||||
) -> Result<Self, String> {
|
||||
let program = match shell {
|
||||
Shell::PowerShell => OsString::from("powershell.exe"),
|
||||
Shell::Default => std::env::var_os("COMSPEC")
|
||||
.filter(|value| !value.is_empty())
|
||||
.unwrap_or_else(|| OsString::from("cmd.exe")),
|
||||
};
|
||||
let program = super::shell_program(shell)?;
|
||||
|
||||
Self::spawn_shell(start_dir, rows, cols, &program)
|
||||
}
|
||||
@@ -1071,6 +1166,44 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn power_shell_lookup_finds_executables_and_reports_missing() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
let root = std::env::temp_dir().join(format!("moorly-pwsh-{}", std::process::id()));
|
||||
let bin = root.join("bin");
|
||||
std::fs::create_dir_all(&bin).unwrap();
|
||||
let pwsh = bin.join("pwsh");
|
||||
std::fs::write(&pwsh, "#!/bin/sh\n").unwrap();
|
||||
std::fs::set_permissions(&pwsh, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
find_in_search_path("pwsh", bin.as_os_str()),
|
||||
Some(pwsh.clone())
|
||||
);
|
||||
assert_eq!(
|
||||
find_in_search_path("pwsh", root.join("empty").as_os_str()),
|
||||
None
|
||||
);
|
||||
|
||||
// A non-executable file is not a usable shell.
|
||||
let plain_dir = root.join("plain");
|
||||
std::fs::create_dir_all(&plain_dir).unwrap();
|
||||
std::fs::write(plain_dir.join("pwsh"), "").unwrap();
|
||||
assert_eq!(find_in_search_path("pwsh", plain_dir.as_os_str()), None);
|
||||
assert!(power_shell_missing().contains("PowerShell"));
|
||||
|
||||
std::fs::remove_dir_all(&root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_power_shell_never_falls_back_to_the_default_shell() {
|
||||
if Shell::PowerShell.unavailable_reason().is_some() {
|
||||
let result = pty::Pty::spawn_with(Path::new("/"), 24, 80, Shell::PowerShell);
|
||||
assert!(result.is_err(), "PowerShell silently started another shell");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grid_never_has_zero_cells() {
|
||||
assert_eq!(grid_size(iced::Size::new(0.0, 0.0), 14.0), (1, 1));
|
||||
|
||||
Reference in new issue
Block a user