From 1338cce7667d56e04ba45734dcc55939d826d0cf Mon Sep 17 00:00:00 2001 From: GeekMr <140129782+WilliamWang1721@users.noreply.github.com> Date: Mon, 21 Sep 2026 21:38:12 +0800 Subject: [PATCH] feat(gpui): remember terminal startup directories --- nebula_app/src/directory_history.rs | 6 +-- .../src/gpui_shell/terminal/view/startup.rs | 43 ++++++++++++++++++- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/nebula_app/src/directory_history.rs b/nebula_app/src/directory_history.rs index db6280d..88e4bb7 100644 --- a/nebula_app/src/directory_history.rs +++ b/nebula_app/src/directory_history.rs @@ -1,7 +1,7 @@ -//! Shared directory intelligence derived from successful cwd transitions. +//! Shared directory intelligence derived from successful local terminal locations. //! -//! Unlike command-text heuristics, this service records only directories the -//! shell actually entered (reported through OSC cwd/title integration). The +//! Unlike command-text heuristics, this service records only directories a local +//! terminal successfully starts in or later reports through cwd integration. The //! resulting frecency score is reused by ghost text, path completion and UI //! directory pickers; no shell-specific command or alias is introduced. diff --git a/nebula_app/src/gpui_shell/terminal/view/startup.rs b/nebula_app/src/gpui_shell/terminal/view/startup.rs index 19364aa..b0252a4 100644 --- a/nebula_app/src/gpui_shell/terminal/view/startup.rs +++ b/nebula_app/src/gpui_shell/terminal/view/startup.rs @@ -2,6 +2,16 @@ use super::*; +fn startup_history_directory( + options: &nebula_terminal::tty::Options, + env: &crate::display::SuggestEnv, +) -> Option { + env.is_this_machine() + .then(|| options.working_directory.clone().or_else(|| std::env::current_dir().ok())) + .flatten() + .and_then(|path| std::path::absolute(path).ok()) +} + impl TerminalView { /// `spawn_grid`:PTY 出生网格(宿主已把窗口定形到该几何)。首帧布局 /// 与之相同则零下发;见 `set_layout` 的启动稳定闸。 @@ -116,7 +126,14 @@ impl TerminalView { }, ); let options = session::local_options(effective, pane_id, cwd); + let history_cwd = startup_history_directory(&options, &suggest_env); let exec_context = crate::runtime_exec::PaneExecContext::from_pty_options(&options); + let spawned = session::spawn(initial, term_config, options); + if spawned.is_ok() + && let Some(cwd) = history_cwd.as_deref().and_then(|path| path.to_str()) + { + crate::directory_history::global().record(cwd); + } ( None, String::from("shell"), @@ -124,7 +141,7 @@ impl TerminalView { suggest_env, Some(exec_context), session_launch, - session::spawn(initial, term_config, options), + spawned, ) }, TerminalLaunch::Ssh { destination, cwd } => ( @@ -332,3 +349,27 @@ impl TerminalView { view } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn startup_history_uses_only_local_initial_directories() { + let directory = tempfile::tempdir().unwrap(); + let mut options = nebula_terminal::tty::Options::default(); + options.working_directory = Some(directory.path().to_path_buf()); + + assert_eq!( + startup_history_directory(&options, &crate::display::SuggestEnv::Local).as_deref(), + Some(directory.path()) + ); + assert_eq!( + startup_history_directory( + &options, + &crate::display::SuggestEnv::Wsl { distro: "Debian".into() } + ), + None + ); + } +}