Added support for powershell on windows and linux, information if powershell is not installed and prevents running that profile
Moorly CI / windows-build (push) Successful in 3m20s
Moorly CI / linux-build (push) Successful in 3m43s

This commit is contained in:
Squid committed 2026-11-08 17:14:23 +01:00
1 parent 7260e2411f
commit c05dc3f9f5
3 files changed
+237 -41

No files matched your search

+90 -27
View File
@@ -9032,27 +9032,32 @@ fn profile_folder_row<'a>(
folder: index,
profile: profile_index,
});
let unavailable = profile_unavailable_reason(profile.kind);
let name_color = if profile.hidden {
hidden_profile_color
} else {
visible_profile_color
};
let profile_row = button(
row![
profile_glyph(profile.kind, profile.glyph_color),
text(profile.name.as_str()).size(13).color(name_color)
]
.spacing(8)
.align_y(Alignment::Center),
)
.width(Length::Fill)
.padding(Padding {
top: 4.0,
right: 26.0,
bottom: 4.0,
left: if show_checks { 0.0 } else { 26.0 + indent },
})
.style(move |theme, status| theme::selectable_row(theme, status, profile_selected));
let mut label = row![
profile_glyph(profile.kind, profile.glyph_color),
text(profile.name.as_str()).size(13).color(name_color)
]
.spacing(8)
.align_y(Alignment::Center);
if unavailable.is_some() {
label = label.push(text("⚠\u{fe0e}").size(12).color(theme::DANGER));
}
let profile_row = button(label)
.width(Length::Fill)
.padding(Padding {
top: 4.0,
right: 26.0,
bottom: 4.0,
left: if show_checks { 0.0 } else { 26.0 + indent },
})
.style(move |theme, status| theme::selectable_row(theme, status, profile_selected));
let press = if show_checks {
Message::ProfilePressed {
window,
@@ -9093,6 +9098,17 @@ fn profile_folder_row<'a>(
settings: show_checks,
});
let profile_row: Element<'_, Message> = if let Some(reason) = unavailable {
tooltip(
profile_row,
container(text(reason).size(11)).padding(4),
tooltip::Position::FollowCursor,
)
.into()
} else {
profile_row.into()
};
if show_checks {
let profile_checkbox =
container(checkbox(profile_checked).size(16).on_toggle(move |_| {
@@ -9788,15 +9804,21 @@ fn tab_scroll_limits(state: &Moorly, window: window::Id) -> (bool, bool) {
const TERMINAL_FOOTER_HEIGHT: f32 = 38.0;
/// Local profile kinds that can start a shell on this platform. PowerShell is
/// a Windows shell; SSH keeps its existing path.
/// resolved at runtime so profiles stay portable across operating systems; SSH
/// keeps its existing path.
fn local_shell(kind: ProfileKind) -> Option<terminal::Shell> {
match kind {
ProfileKind::Terminal => Some(terminal::Shell::Default),
ProfileKind::PowerShell if cfg!(windows) => Some(terminal::Shell::PowerShell),
_ => None,
ProfileKind::PowerShell => Some(terminal::Shell::PowerShell),
ProfileKind::Ssh => None,
}
}
/// Why a local profile cannot start on this system, if it cannot.
fn profile_unavailable_reason(kind: ProfileKind) -> Option<String> {
local_shell(kind)?.unavailable_reason()
}
/// Starts a local terminal from a profile, either in the current window or in
/// a fresh one. SSH profiles keep their existing (not yet implemented) path.
fn run_profile(
@@ -9826,6 +9848,14 @@ fn run_profile(
return Task::none();
};
if let Some(reason) = shell.unavailable_reason() {
state.notifications.add(
notifications::Notification::new(notifications::NotificationKind::Warning, reason)
.title(format!("{} unavailable", profile.kind.label())),
);
return Task::none();
}
let (start_dir, warning) = profile_start_directory(&profile);
if let Some(warning) = warning {
state.notifications.add(
@@ -10047,6 +10077,14 @@ fn restart_terminal(state: &mut Moorly, id: u32) {
return;
};
if let Some(reason) = shell.unavailable_reason() {
state.notifications.add(
notifications::Notification::new(notifications::NotificationKind::Warning, reason)
.title(format!("{} unavailable", profile.kind.label())),
);
return;
}
match terminal::Session::spawn_with(&start_dir, rows, cols, shell) {
Ok(session) => {
if let Some(terminal) = state.terminal_sessions.get_mut(&id) {
@@ -12599,15 +12637,40 @@ mod tests {
assert_eq!(local_shell(ProfileKind::Terminal), Some(Shell::Default));
assert_eq!(local_shell(ProfileKind::Ssh), None);
assert_eq!(
local_shell(ProfileKind::PowerShell),
Some(Shell::PowerShell)
);
if cfg!(windows) {
assert_eq!(
local_shell(ProfileKind::PowerShell),
Some(Shell::PowerShell)
);
} else {
assert_eq!(local_shell(ProfileKind::PowerShell), None);
}
// Terminal profiles never depend on an optional executable.
assert_eq!(
super::profile_unavailable_reason(ProfileKind::Terminal),
None
);
assert_eq!(super::profile_unavailable_reason(ProfileKind::Ssh), None);
}
#[test]
fn powershell_profiles_round_trip_through_toml_on_every_platform() {
use super::{ProfileFile, ProfileKind};
let text = r#"
name = "Pwsh"
parent_id = "folder-id"
kind = "powershell"
session_end = "reconnect"
start_path = "/tmp"
"#;
let file: ProfileFile = toml::from_str(text).unwrap();
let profile = file.into_profile();
assert_eq!(profile.kind, ProfileKind::PowerShell);
assert_eq!(profile.session_end, super::SessionEnd::Reconnect);
let encoded = toml::to_string(&ProfileFile::from_profile(&profile, "Work")).unwrap();
assert!(encoded.contains("kind = \"powershell\""));
assert!(encoded.contains("start_path = \"/tmp\""));
assert!(!encoded.contains("unavailable"));
}
#[test]
+146 -13
View File
@@ -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));