Added terminal support for windows 10/11
This commit is contained in:
1 parent
6915fc2857
commit
317b773a18
4 files changed
+496
-14
No files matched your search
+82
-9
@@ -995,6 +995,10 @@ fn home_dir() -> PathBuf {
|
||||
home.push(path);
|
||||
Some(home)
|
||||
})
|
||||
// HOMEPATH on its own is relative ("\\Users\\name"), so it is
|
||||
// only used together with HOMEDRIVE. The config folder's
|
||||
// variable is a safe absolute fallback.
|
||||
.or_else(|| std::env::var_os("APPDATA").map(PathBuf::from))
|
||||
.unwrap_or_else(|| PathBuf::from("C:\\"))
|
||||
} else {
|
||||
std::env::var_os("HOME")
|
||||
@@ -1005,17 +1009,33 @@ fn home_dir() -> PathBuf {
|
||||
|
||||
fn default_start_path() -> String {
|
||||
if cfg!(windows) {
|
||||
std::env::var("HOMEPATH").unwrap_or_else(|_| home_dir().to_string_lossy().into_owned())
|
||||
app_data_dir().to_string_lossy().into_owned()
|
||||
} else {
|
||||
"~/".to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
/// Windows profile files live under `%APPDATA%`; an unconfigured start
|
||||
/// directory uses the same variable and falls back to the user's home.
|
||||
fn app_data_dir() -> PathBuf {
|
||||
std::env::var_os("APPDATA")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(home_dir)
|
||||
}
|
||||
|
||||
/// Resolves a typed path, expanding `~` to the home directory.
|
||||
fn resolve_start_path(input: &str) -> PathBuf {
|
||||
let input = input.trim();
|
||||
|
||||
if input.is_empty() || input == "~" {
|
||||
if input.is_empty() {
|
||||
return if cfg!(windows) {
|
||||
app_data_dir()
|
||||
} else {
|
||||
home_dir()
|
||||
};
|
||||
}
|
||||
|
||||
if input == "~" {
|
||||
return home_dir();
|
||||
}
|
||||
|
||||
@@ -9768,6 +9788,16 @@ 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.
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
/// 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(
|
||||
@@ -9786,7 +9816,7 @@ fn run_profile(
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
if profile.kind != ProfileKind::Terminal {
|
||||
let Some(shell) = local_shell(profile.kind) else {
|
||||
state.notifications.add(
|
||||
notifications::Notification::new(
|
||||
notifications::NotificationKind::Warning,
|
||||
@@ -9795,7 +9825,7 @@ fn run_profile(
|
||||
.title("Session"),
|
||||
);
|
||||
return Task::none();
|
||||
}
|
||||
};
|
||||
|
||||
let (start_dir, warning) = profile_start_directory(&profile);
|
||||
if let Some(warning) = warning {
|
||||
@@ -9812,7 +9842,7 @@ fn run_profile(
|
||||
.unwrap_or(Size::new(900.0, 600.0));
|
||||
let (rows, cols) = terminal_grid(state, size, false);
|
||||
|
||||
let session = match terminal::Session::spawn(&start_dir, rows, cols) {
|
||||
let session = match terminal::Session::spawn_with(&start_dir, rows, cols, shell) {
|
||||
Ok(session) => session,
|
||||
Err(error) => {
|
||||
state.notifications.add(
|
||||
@@ -9905,8 +9935,10 @@ fn terminal_tick(state: &mut Moorly) -> Task<Message> {
|
||||
continue;
|
||||
};
|
||||
|
||||
if terminal.exit.is_none()
|
||||
&& let Some(exit) = terminal.session.poll()
|
||||
// Keep draining output after the process exits so the final ConPTY
|
||||
// frame is rendered before the tab footer appears.
|
||||
if let Some(exit) = terminal.session.poll()
|
||||
&& terminal.exit.is_none()
|
||||
{
|
||||
terminal.exit = Some(exit);
|
||||
ended.push(id);
|
||||
@@ -10012,8 +10044,11 @@ fn restart_terminal(state: &mut Moorly, id: u32) {
|
||||
}
|
||||
|
||||
let (rows, cols) = terminal_grid(state, size, false);
|
||||
let Some(shell) = local_shell(profile.kind) else {
|
||||
return;
|
||||
};
|
||||
|
||||
match terminal::Session::spawn(&start_dir, rows, cols) {
|
||||
match terminal::Session::spawn_with(&start_dir, rows, cols, shell) {
|
||||
Ok(session) => {
|
||||
if let Some(terminal) = state.terminal_sessions.get_mut(&id) {
|
||||
terminal.session = session;
|
||||
@@ -12558,6 +12593,39 @@ mod tests {
|
||||
assert_eq!(state.profile_folders[0].profiles[0].start_path, "/tmp");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn profile_kind_selects_a_local_shell() {
|
||||
use super::terminal::Shell;
|
||||
use super::{ProfileKind, local_shell};
|
||||
|
||||
assert_eq!(local_shell(ProfileKind::Terminal), Some(Shell::Default));
|
||||
assert_eq!(local_shell(ProfileKind::Ssh), None);
|
||||
|
||||
if cfg!(windows) {
|
||||
assert_eq!(
|
||||
local_shell(ProfileKind::PowerShell),
|
||||
Some(Shell::PowerShell)
|
||||
);
|
||||
} else {
|
||||
assert_eq!(local_shell(ProfileKind::PowerShell), None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_start_path_is_never_relative() {
|
||||
let path = super::default_start_path();
|
||||
assert!(!path.trim().is_empty());
|
||||
|
||||
if cfg!(windows) {
|
||||
assert!(
|
||||
std::path::Path::new(&path).is_absolute(),
|
||||
"default start path {path} is not absolute"
|
||||
);
|
||||
} else {
|
||||
assert_eq!(path, "~/");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn session_end_labels_depend_on_the_profile_kind() {
|
||||
use super::{ProfileKind, SessionEnd};
|
||||
@@ -12627,8 +12695,13 @@ mod tests {
|
||||
profile.kind = super::ProfileKind::Terminal;
|
||||
profile.start_path = String::new();
|
||||
|
||||
let expected = if cfg!(windows) {
|
||||
super::app_data_dir()
|
||||
} else {
|
||||
super::home_dir()
|
||||
};
|
||||
let (directory, warning) = super::profile_start_directory(&profile);
|
||||
assert_eq!(directory, super::home_dir());
|
||||
assert_eq!(directory, expected);
|
||||
assert!(warning.is_none());
|
||||
|
||||
profile.start_path = "/definitely/not/a/moorly/directory".to_owned();
|
||||
|
||||
Reference in new issue
Block a user