From a8cf702d43d3d4b6741660f602f19d115900c55c Mon Sep 17 00:00:00 2001 From: James Magahern Date: Tue, 17 Nov 2026 18:11:37 -0800 Subject: [PATCH] Inherit the source shell directory when opening stacked panes --- README.md | 9 ++++--- src/server.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6c305b9..aed692a 100644 --- a/README.md +++ b/README.md @@ -82,9 +82,12 @@ GUI, and returns. Each invocation starts an independent session unless you pass ### Shells and sessions -Each new session starts with one tab and one shell. Windows opened with the -New Window shortcut start a fresh session in the focused pane’s directory on -the same host. A remote window reuses the focused SSH connection’s executable +Each new session starts with one tab and one shell. A pane opened above or +below another pane starts in that shell’s current working directory. New tabs +start in the server’s launch directory. + +The New Window shortcut starts a fresh session in the focused pane’s directory +on the same host. A remote window reuses the focused SSH connection’s executable and agent settings. For a nested SSH tab, the new window connects directly from your computer, so the nested host must be reachable using your local SSH configuration. Closing a window leaves its session available for reattachment. diff --git a/src/server.rs b/src/server.rs index e4ec0a8..3218f19 100644 --- a/src/server.rs +++ b/src/server.rs @@ -827,7 +827,7 @@ impl SessionState { ClientMessage::Split { pane, cwd } => { let (tab_index, stack_index, _) = self.pane_position(pane)?; self.ensure_new_pane_fits(tab_index)?; - let pane = self.new_pane(cwd)?; + let pane = self.new_pane(cwd.map(PathBuf::from))?; let tab = &mut self.tabs[tab_index]; tab.stacks.insert(stack_index + 1, single_pane_stack(pane)); flatten_panes(tab); @@ -850,7 +850,15 @@ impl SessionState { .context("no active pane")?; let (tab_index, stack_index, pane_index) = self.pane_position(pane)?; self.ensure_new_pane_fits(tab_index)?; - let pane = self.new_pane(cwd)?; + // Resolve the shell's live directory on the host that owns the pane. + let directory = cwd.map(PathBuf::from).or_else(|| { + self.panes + .get(&pane)? + .session + .working_directory() + .filter(|path| path.is_dir()) + }); + let pane = self.new_pane(directory)?; let tab = &mut self.tabs[tab_index]; let stack = &mut tab.stacks[stack_index]; let insert_at = pane_index + usize::from(direction == VerticalDirection::Down); @@ -1124,12 +1132,11 @@ impl SessionState { self.panes.get_mut(&id).context("unknown pane") } - fn new_pane(&mut self, cwd: Option) -> Result { + fn new_pane(&mut self, directory: Option) -> Result { ensure!( self.panes.len() < MAX_PANES, "at most {MAX_PANES} panes are supported" ); - let directory = cwd.map(PathBuf::from); if let Some(directory) = &directory { ensure!(directory.is_dir(), "working directory does not exist"); } @@ -1184,7 +1191,7 @@ impl SessionState { fn new_tab(&mut self, cwd: Option) -> Result<()> { ensure!(self.tabs.len() < 256, "a window supports at most 256 tabs"); - let pane = self.new_pane(cwd)?; + let pane = self.new_pane(cwd.map(PathBuf::from))?; let id = self.next_id; self.next_id += 1; self.tabs.push(TabInfo { @@ -1907,6 +1914,60 @@ mod tests { output(&mut actor, "VISIBLE", true); } + #[test] + fn stacked_panes_inherit_the_source_directory_and_new_tabs_use_the_default() { + let server = TestServer::start(); + let start = fs::canonicalize(server.socket.parent().unwrap()).unwrap(); + let changed = start.join("after cd"); + fs::create_dir(&changed).unwrap(); + let mut client = server.client(); + send( + &mut client, + ClientMessage::Connect(SessionTarget::New { + cwd: Some(start.to_str().unwrap().into()), + }), + ); + let first = snapshot(&mut client, |s| s.panes.len() == 1).panes[0].id; + // Wait for the shell to confirm cd, independently of the daemon's CWD refresh. + input( + &mut client, + first, + "cd 'after cd' && printf '\\033]2;cwd-ready\\007'\n", + ); + snapshot(&mut client, |s| s.panes[0].title == "cwd-ready"); + + for (source, direction, cwd, expected) in [ + (None, VerticalDirection::Down, None, &changed), + ( + None, + VerticalDirection::Up, + Some(start.to_str().unwrap().into()), + &start, + ), + (Some(first), VerticalDirection::Up, None, &changed), + ] { + send( + &mut client, + ClientMessage::NewStackPane { + pane: source, + direction, + cwd, + }, + ); + let frame = list_after_commands(&mut client); + assert_ne!(frame.panes[0].id, first); + assert_eq!(Path::new(&frame.panes[0].cwd), expected); + } + + send(&mut client, ClientMessage::NewTab { cwd: None }); + let frame = list_after_commands(&mut client); + assert_eq!(frame.tabs.len(), 2); + assert_eq!( + Path::new(&frame.panes[0].cwd), + fs::canonicalize(".").unwrap() + ); + } + #[test] fn stacks_insert_in_both_directions_and_only_expand_one_grid() { let server = TestServer::start();