diff options
| author | Dave Willett <willett@hey.com> | 2026-07-08 09:47:15 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-07-08 18:47:15 +0200 |
| commit | 01412c2e0999de5b40b68a9d1a731eea52844338 (patch) | |
| tree | 8e57345f32224f9c087648f62e67436fe65b5d0a /src/bin | |
| parent | chore(deps): bump cached from 1.1.0 to 2.0.2 (#269) (diff) | |
| download | sl0pstatus-01412c2e0999de5b40b68a9d1a731eea52844338.tar.gz sl0pstatus-01412c2e0999de5b40b68a9d1a731eea52844338.tar.xz sl0pstatus-01412c2e0999de5b40b68a9d1a731eea52844338.zip | |
feat(command): add {focused_pane_cwd} to run commands in focused cwd (#273)
* feat(command): add {focused_pane_cwd} to run commands in the focused pane's directory
Command widgets configured with command_<name>_cwd "{focused_pane_cwd}"
now execute in the current working directory of the focused terminal pane,
following it as the user changes directories.
This relies on zellij's CwdChanged event and get_pane_cwd API. The focused
terminal pane of the active tab is tracked via PaneUpdate (seeding the cwd
on focus change) and updated on CwdChanged. Affected command results are
invalidated so they re-run, and stale results from a previous cwd are
discarded to avoid showing the wrong directory.
Closes #92
* fix(command): release focused cwd command locks
Focused-cwd commands can be invalidated while their first run is still in flight. If that result is later discarded as stale, no command result is stored but the per-command lock remains, preventing future reruns.
Release the command lock when invalidating focused-cwd commands so the branch badge can recover on subsequent renders or cwd updates.
Add a regression test for rerunning a command with no stored result after its lock is released.
Diffstat (limited to 'src/bin')
| -rw-r--r-- | src/bin/zjstatus.rs | 120 |
1 files changed, 119 insertions, 1 deletions
diff --git a/src/bin/zjstatus.rs b/src/bin/zjstatus.rs index 9a9ae7b..f9d95c7 100644 --- a/src/bin/zjstatus.rs +++ b/src/bin/zjstatus.rs @@ -1,7 +1,7 @@ use zellij_tile::prelude::*; use chrono::Local; -use std::{collections::BTreeMap, sync::Arc}; +use std::{collections::BTreeMap, path::PathBuf, sync::Arc}; use uuid::Uuid; use zjstatus::{ @@ -31,6 +31,7 @@ struct State { userspace_configuration: BTreeMap<String, String>, module_config: config::ModuleConfig, widget_map: BTreeMap<String, Arc<dyn Widget>>, + focus_cwd_commands: Vec<String>, err: Option<anyhow::Error>, } @@ -77,6 +78,7 @@ impl ZellijPlugin for State { EventType::TabUpdate, EventType::SessionUpdate, EventType::RunCommandResult, + EventType::CwdChanged, ]); set_timeout(REFRESH_INTERVAL_SECONDS); @@ -88,6 +90,8 @@ impl ZellijPlugin for State { } }; self.widget_map = register_widgets(&configuration); + self.focus_cwd_commands = + zjstatus::widgets::command::focus_cwd_command_names(&configuration); self.userspace_configuration = configuration; self.pending_events = Vec::new(); self.got_permissions = false; @@ -105,6 +109,8 @@ impl ZellijPlugin for State { start_time: Local::now(), cache_mask: 0, incoming_notification: None, + focused_pane_id: None, + focused_pane_cwd: None, }; } @@ -180,6 +186,56 @@ impl ZellijPlugin for State { } impl State { + fn update_focused_pane(&mut self) { + let active_tab = self.state.tabs.iter().find(|t| t.active); + + let new_id = active_tab + .and_then(|tab| self.state.panes.panes.get(&tab.position)) + .and_then(|panes| panes.iter().find(|p| p.is_focused && !p.is_plugin)) + .map(|p| PaneId::Terminal(p.id)); + + if new_id == self.state.focused_pane_id { + return; + } + + self.state.focused_pane_id = new_id; + + let new_cwd = match new_id { + Some(pane_id) => match get_pane_cwd(pane_id) { + Ok(cwd) => Some(cwd), + Err(e) => { + tracing::debug!("could not get pane cwd: {e}"); + None + } + }, + None => None, + }; + + self.set_focused_pane_cwd(new_cwd); + } + + fn set_focused_pane_cwd(&mut self, new_cwd: Option<PathBuf>) -> bool { + if new_cwd == self.state.focused_pane_cwd { + return false; + } + + self.state.focused_pane_cwd = new_cwd; + + if self.focus_cwd_commands.is_empty() { + return false; + } + + self.invalidate_focus_cwd_commands(); + true + } + + fn invalidate_focus_cwd_commands(&mut self) { + for name in &self.focus_cwd_commands { + pipe::invalidate_command_result(&mut self.state, name); + zjstatus::widgets::command::release_command_lock(&self.state, name); + } + } + fn handle_event(&mut self, event: Event) -> bool { let mut should_render = false; match event { @@ -224,8 +280,21 @@ impl State { self.state.panes = pane_info; self.state.cache_mask = UpdateEventMask::Tab as u8; + self.update_focused_pane(); + should_render = true; } + Event::CwdChanged(pane_id, cwd, _clients) => { + tracing::Span::current().record("event_type", "Event::CwdChanged"); + tracing::debug!(pane_id = ?pane_id, cwd = ?cwd); + + if Some(pane_id) == self.state.focused_pane_id + && self.set_focused_pane_cwd(Some(cwd)) + { + self.state.cache_mask = UpdateEventMask::Command as u8; + should_render = true; + } + } Event::PermissionRequestResult(result) => { tracing::Span::current().record("event_type", "Event::PermissionRequestResult"); tracing::debug!(result = ?result); @@ -243,6 +312,13 @@ impl State { self.state.cache_mask = UpdateEventMask::Command as u8; if let Some(name) = context.get("name") { + if self.focus_cwd_commands.iter().any(|n| n == name) + && context.get("cwd").map(PathBuf::from) != self.state.focused_pane_cwd + { + tracing::debug!("discarding stale command result for {name}"); + return false; + } + let stdout = match String::from_utf8(stdout) { Ok(s) => s, Err(_) => "".to_owned(), @@ -343,3 +419,45 @@ fn register_widgets(configuration: &BTreeMap<String, String>) -> BTreeMap<String widget_map } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn set_focused_pane_cwd_only_invalidates_on_change() { + let mut state = State { + focus_cwd_commands: vec!["command_branch".to_owned()], + ..State::default() + }; + state.state.focused_pane_cwd = Some(PathBuf::from("/tmp")); + state.state.command_results.insert( + "command_branch".to_owned(), + CommandResult { + context: BTreeMap::from([( + "timestamp".to_owned(), + Local::now() + .format(zjstatus::widgets::command::TIMESTAMP_FORMAT) + .to_string(), + )]), + ..CommandResult::default() + }, + ); + + let original_timestamp = + state.state.command_results["command_branch"].context["timestamp"].clone(); + + assert!(!state.set_focused_pane_cwd(Some(PathBuf::from("/tmp")))); + assert_eq!( + state.state.command_results["command_branch"].context["timestamp"], + original_timestamp + ); + + assert!(state.set_focused_pane_cwd(Some(PathBuf::from("/var")))); + assert_eq!(state.state.focused_pane_cwd, Some(PathBuf::from("/var"))); + assert_ne!( + state.state.command_results["command_branch"].context["timestamp"], + original_timestamp + ); + } +} |
