From 2d3f4f9bf9d86c2febe53644ace2d4836018cfe6 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 26 Sep 2020 11:48:58 -0700 Subject: [PATCH] wezterm: guess at cwd associated with pty on linux We still prefer to consume the OSC 7 value, but can fall back to guessing the cwd for a target process. --- docs/changelog.markdown | 2 ++ pty/src/lib.rs | 5 +++++ pty/src/serial.rs | 6 ++++++ pty/src/ssh.rs | 6 ++++++ pty/src/unix.rs | 7 +++++++ src/localtab.rs | 26 +++++++++++++++++++++++++- 6 files changed, 51 insertions(+), 1 deletion(-) diff --git a/docs/changelog.markdown b/docs/changelog.markdown index baf4f59ae..3100e5e2d 100644 --- a/docs/changelog.markdown +++ b/docs/changelog.markdown @@ -22,6 +22,8 @@ brief notes about them may accumulate here. The displayed number is 1-based. You can set `tab_and_split_indices_are_zero_based=true` if you prefer the number to be zero based. +* On Linux systems, wezterm can now attempt to guess the current + working directory that shouldbe set in newly spawned local panes/tabs. * Experimental support for splits/panes. Not currently supported for multiplexer connections. See [#157](https://github.com/wez/wezterm/issues/157) for diff --git a/pty/src/lib.rs b/pty/src/lib.rs index 820bb5a45..e0d05f848 100644 --- a/pty/src/lib.rs +++ b/pty/src/lib.rs @@ -103,6 +103,11 @@ pub trait MasterPty: std::io::Write { /// slave end. This is equivalent to the Write impl on MasterPty /// itself, but allows splitting it off into a separate object. fn try_clone_writer(&self) -> Result, Error>; + + /// If applicable to the type of the tty, return the local process id + /// of the process group or session leader + #[cfg(unix)] + fn process_group_leader(&self) -> Option; } /// Represents a child process spawned into the pty. diff --git a/pty/src/serial.rs b/pty/src/serial.rs index 4d053aea5..43a87194b 100644 --- a/pty/src/serial.rs +++ b/pty/src/serial.rs @@ -177,6 +177,12 @@ impl MasterPty for Master { let port = Arc::clone(&self.port); Ok(Box::new(Master { port })) } + + #[cfg(unix)] + fn process_group_leader(&self) -> Option { + // N/A: there is no local process + None + } } struct Reader { diff --git a/pty/src/ssh.rs b/pty/src/ssh.rs index b4e25303e..782304fd0 100644 --- a/pty/src/ssh.rs +++ b/pty/src/ssh.rs @@ -183,6 +183,12 @@ impl MasterPty for SshMaster { pty: self.pty.clone(), })) } + + #[cfg(unix)] + fn process_group_leader(&self) -> Option { + // N/A: there is no local process + None + } } struct SshSlave { diff --git a/pty/src/unix.rs b/pty/src/unix.rs index 63b38bdf7..e7fc4fc21 100644 --- a/pty/src/unix.rs +++ b/pty/src/unix.rs @@ -262,6 +262,13 @@ impl MasterPty for UnixMasterPty { let fd = PtyFd(self.fd.try_clone()?); Ok(Box::new(UnixMasterPty { fd })) } + + fn process_group_leader(&self) -> Option { + match unsafe { libc::tcgetpgrp(self.fd.0.as_raw_fd()) } { + pid if pid > 0 => Some(pid), + _ => None, + } + } } impl Write for UnixMasterPty { diff --git a/src/localtab.rs b/src/localtab.rs index 6bf4046db..ee00d48d4 100644 --- a/src/localtab.rs +++ b/src/localtab.rs @@ -102,7 +102,11 @@ impl Pane for LocalPane { } fn get_current_working_dir(&self) -> Option { - self.terminal.borrow().get_current_dir().cloned() + self.terminal + .borrow() + .get_current_dir() + .cloned() + .or_else(|| self.divine_current_working_dir()) } async fn search(&self, mut pattern: Pattern) -> anyhow::Result> { @@ -228,6 +232,26 @@ impl LocalPane { domain_id, } } + + #[cfg(target_os = "linux")] + fn divine_current_working_dir_linux(&self) -> Option { + if let Some(pid) = self.pty.borrow().process_group_leader() { + if let Ok(path) = std::fs::read_link(format!("/proc/{}/cwd", pid)) { + return Url::parse(&format!("file://localhost{}", path.display())).ok(); + } + } + None + } + + fn divine_current_working_dir(&self) -> Option { + #[cfg(target_os = "linux")] + { + return self.divine_current_working_dir_linux(); + } + + #[allow(unreachable_code)] + None + } } impl Drop for LocalPane {