diff options
| author | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-05-29 20:38:55 +0300 |
|---|---|---|
| committer | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-05-29 20:38:55 +0300 |
| commit | 9e302984ccb13a65e3e504fa7692b223f546ff84 (patch) | |
| tree | 52808a27bd82c05f55117ad129137fe3ef46a8cb /src | |
| parent | 77242943ba65a80cbc1180258dd64f06635233f9 (diff) | |
feat: expose foreground cwd in pane info
refs #345
Diffstat (limited to 'src')
| -rw-r--r-- | src/api/schema.rs | 6 | ||||
| -rw-r--r-- | src/app/agents.rs | 1 | ||||
| -rw-r--r-- | src/app/creation.rs | 3 | ||||
| -rw-r--r-- | src/pane.rs | 49 | ||||
| -rw-r--r-- | src/platform/linux.rs | 6 | ||||
| -rw-r--r-- | src/platform/macos.rs | 6 | ||||
| -rw-r--r-- | src/terminal/runtime.rs | 4 | ||||
| -rw-r--r-- | src/workspace/tab.rs | 11 |
8 files changed, 86 insertions, 0 deletions
diff --git a/src/api/schema.rs b/src/api/schema.rs index cd82f99f..5d933af9 100644 --- a/src/api/schema.rs +++ b/src/api/schema.rs @@ -811,6 +811,8 @@ pub struct AgentInfo { pub focused: bool, #[serde(default, skip_serializing_if = "Option::is_none")] pub cwd: Option<String>, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub foreground_cwd: Option<String>, pub revision: u64, } @@ -824,6 +826,8 @@ pub struct PaneInfo { #[serde(default, skip_serializing_if = "Option::is_none")] pub cwd: Option<String>, #[serde(default, skip_serializing_if = "Option::is_none")] + pub foreground_cwd: Option<String>, + #[serde(default, skip_serializing_if = "Option::is_none")] pub label: Option<String>, #[serde(default, skip_serializing_if = "Option::is_none")] pub agent: Option<String>, @@ -1407,6 +1411,7 @@ mod tests { tab_id: "w_1:1".into(), focused: true, cwd: Some("/worktrees/herdr/worktree-api".into()), + foreground_cwd: None, label: None, agent: None, title: None, @@ -1457,6 +1462,7 @@ mod tests { tab_id: "w_1:2".into(), focused: false, cwd: Some("/tmp/review".into()), + foreground_cwd: None, label: None, agent: None, title: None, diff --git a/src/app/agents.rs b/src/app/agents.rs index 55ee15df..6d463b28 100644 --- a/src/app/agents.rs +++ b/src/app/agents.rs @@ -423,6 +423,7 @@ impl App { pane_id: pane.pane_id, focused: pane.focused, cwd: pane.cwd, + foreground_cwd: pane.foreground_cwd, revision: pane.revision, }) } diff --git a/src/app/creation.rs b/src/app/creation.rs index d483eb1a..0f63d60d 100644 --- a/src/app/creation.rs +++ b/src/app/creation.rs @@ -288,6 +288,9 @@ impl App { cwd: ws.tabs[tab_idx] .cwd_for_pane(pane_id, &self.state.terminals, &self.terminal_runtimes) .map(|cwd| cwd.display().to_string()), + foreground_cwd: ws.tabs[tab_idx] + .foreground_cwd_for_pane(pane_id, &self.terminal_runtimes) + .map(|cwd| cwd.display().to_string()), label: terminal.manual_label.clone(), agent: terminal.effective_agent_label().map(str::to_string), title: presentation.title, diff --git a/src/pane.rs b/src/pane.rs index 41bb439c..885fb11d 100644 --- a/src/pane.rs +++ b/src/pane.rs @@ -124,6 +124,29 @@ fn should_clear_agent_for_foreground_shell( previous_agent.is_some() && new_agent.is_none() && foreground_is_pane_shell } +fn usable_process_cwd(pid: u32) -> Option<std::path::PathBuf> { + crate::platform::process_cwd(pid).filter(|cwd| cwd.is_absolute() && cwd.is_dir()) +} + +fn foreground_member_cwd_different_from_shell( + shell_pid: u32, + shell_cwd: Option<&std::path::PathBuf>, +) -> Option<std::path::PathBuf> { + let job = crate::detect::foreground_job(shell_pid)?; + for process in job.processes { + if process.pid == shell_pid { + continue; + } + let Some(cwd) = usable_process_cwd(process.pid) else { + continue; + }; + if shell_cwd != Some(&cwd) { + return Some(cwd); + } + } + None +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum ForegroundShellAgentAction { ObserveProbe, @@ -2111,6 +2134,32 @@ impl PaneRuntime { let pid = self.child_pid.load(Ordering::Relaxed); crate::platform::process_cwd(pid) } + + /// Get the current working directory of the process group controlling the pane PTY. + pub fn foreground_cwd(&self) -> Option<std::path::PathBuf> { + #[cfg(unix)] + { + let pid = self.child_pid.load(Ordering::Acquire); + let shell_cwd = usable_process_cwd(pid); + let foreground_pgid = self + .master_fd() + .and_then(crate::platform::foreground_process_group_id_for_tty_fd) + .or_else(|| crate::platform::foreground_process_group_id(pid)); + let leader_cwd = foreground_pgid.and_then(usable_process_cwd); + + if leader_cwd.as_ref() == shell_cwd.as_ref() { + foreground_member_cwd_different_from_shell(pid, shell_cwd.as_ref()).or(leader_cwd) + } else { + leader_cwd + .or_else(|| foreground_member_cwd_different_from_shell(pid, shell_cwd.as_ref())) + } + } + + #[cfg(not(unix))] + { + None + } + } } #[cfg(test)] diff --git a/src/platform/linux.rs b/src/platform/linux.rs index 046e72c7..d1ae07f1 100644 --- a/src/platform/linux.rs +++ b/src/platform/linux.rs @@ -1,5 +1,6 @@ use std::{ io::Write, + os::fd::RawFd, path::PathBuf, process::{Command, Stdio}, }; @@ -86,6 +87,11 @@ pub fn foreground_process_group_id(child_pid: u32) -> Option<u32> { (tpgid > 0).then_some(tpgid as u32) } +pub fn foreground_process_group_id_for_tty_fd(fd: RawFd) -> Option<u32> { + let pgid = unsafe { libc::tcgetpgrp(fd) }; + (pgid > 0).then_some(pgid as u32) +} + fn process_pgrp_and_comm(pid: u32) -> Option<(i32, String)> { let stat = std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?; let close = stat.rfind(')')?; diff --git a/src/platform/macos.rs b/src/platform/macos.rs index ac0e160b..e38b29eb 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -1,5 +1,6 @@ use std::ffi::OsStr; use std::io::Write; +use std::os::fd::RawFd; use std::os::unix::ffi::OsStrExt; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; @@ -177,6 +178,11 @@ pub fn foreground_process_group_id(pid: u32) -> Option<u32> { } } +pub fn foreground_process_group_id_for_tty_fd(fd: RawFd) -> Option<u32> { + let pgid = unsafe { libc::tcgetpgrp(fd) }; + (pgid > 0).then_some(pgid as u32) +} + /// Get the effective process name from `argv[0]` via `sysctl(KERN_PROCARGS2)`. /// /// This is the macOS equivalent of reading `/proc/{pid}/cmdline` on Linux. diff --git a/src/terminal/runtime.rs b/src/terminal/runtime.rs index 636972a1..b1a25ee9 100644 --- a/src/terminal/runtime.rs +++ b/src/terminal/runtime.rs @@ -373,6 +373,10 @@ impl TerminalRuntime { self.0.cwd() } + pub fn foreground_cwd(&self) -> Option<std::path::PathBuf> { + self.0.foreground_cwd() + } + pub(crate) fn current_size(&self) -> (u16, u16) { self.0.current_size() } diff --git a/src/workspace/tab.rs b/src/workspace/tab.rs index d5ebe00f..100e67d0 100644 --- a/src/workspace/tab.rs +++ b/src/workspace/tab.rs @@ -405,4 +405,15 @@ impl Tab { .map(|terminal| terminal.cwd.clone()) }) } + + pub fn foreground_cwd_for_pane( + &self, + pane_id: PaneId, + terminal_runtimes: &TerminalRuntimeRegistry, + ) -> Option<PathBuf> { + let terminal_id = self.terminal_id(pane_id)?; + terminal_runtimes + .get(terminal_id) + .and_then(|rt| rt.foreground_cwd()) + } } |
