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.
This commit is contained in:
Wez Furlong committed 2020-09-26 11:51:21 -07:00
1 parent 5031a53ff9
commit 2d3f4f9bf9
6 files changed
+51 -1

No files matched your search

+2
View File
@@ -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
+5
View File
@@ -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<Box<dyn std::io::Write + Send>, 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<libc::pid_t>;
}
/// Represents a child process spawned into the pty.
+6
View File
@@ -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<libc::pid_t> {
// N/A: there is no local process
None
}
}
struct Reader {
+6
View File
@@ -183,6 +183,12 @@ impl MasterPty for SshMaster {
pty: self.pty.clone(),
}))
}
#[cfg(unix)]
fn process_group_leader(&self) -> Option<libc::pid_t> {
// N/A: there is no local process
None
}
}
struct SshSlave {
+7
View File
@@ -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<libc::pid_t> {
match unsafe { libc::tcgetpgrp(self.fd.0.as_raw_fd()) } {
pid if pid > 0 => Some(pid),
_ => None,
}
}
}
impl Write for UnixMasterPty {
+25 -1
View File
@@ -102,7 +102,11 @@ impl Pane for LocalPane {
}
fn get_current_working_dir(&self) -> Option<Url> {
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<Vec<SearchResult>> {
@@ -228,6 +232,26 @@ impl LocalPane {
domain_id,
}
}
#[cfg(target_os = "linux")]
fn divine_current_working_dir_linux(&self) -> Option<Url> {
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<Url> {
#[cfg(target_os = "linux")]
{
return self.divine_current_working_dir_linux();
}
#[allow(unreachable_code)]
None
}
}
impl Drop for LocalPane {