aboutsummaryrefslogtreecommitdiff
path: root/crates/dinggerd/src/tmux.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/dinggerd/src/tmux.rs')
-rw-r--r--crates/dinggerd/src/tmux.rs40
1 files changed, 20 insertions, 20 deletions
diff --git a/crates/dinggerd/src/tmux.rs b/crates/dinggerd/src/tmux.rs
index 6610161..54da39d 100644
--- a/crates/dinggerd/src/tmux.rs
+++ b/crates/dinggerd/src/tmux.rs
@@ -280,31 +280,31 @@ pub fn client_format(client: &str, format: &str) -> Option<String> {
.filter(|value| !value.is_empty())
}
-/// Attached clients as (name, pid), most recently active first.
-pub fn clients() -> Result<Vec<(String, u32)>, String> {
- let out = run_tmux(&[
- "list-clients",
- "-F",
- "#{client_activity}\t#{client_name}\t#{client_pid}",
- ])?;
- let mut clients: Vec<(u64, String, u32)> = out
+/// The sessions holding a pane's window (several: a session group, a linked
+/// window).
+pub fn sessions_with_pane(pane_id: &str) -> Vec<String> {
+ run_tmux(&["list-panes", "-a", "-F", "#{session_id}\t#{pane_id}"])
+ .unwrap_or_default()
.lines()
- .filter_map(|line| {
- let mut fields = line.split('\t');
- let activity = fields.next()?.parse().ok()?;
- let name = fields.next()?.to_string();
- let pid = fields.next()?.parse().ok()?;
- Some((activity, name, pid))
- })
- .collect();
- clients.sort_by_key(|client| std::cmp::Reverse(client.0));
- Ok(clients.into_iter().map(|(_, name, pid)| (name, pid)).collect())
+ .filter_map(|line| line.split_once('\t').filter(|(_, pane)| *pane == pane_id).map(|(s, _)| s.to_string()))
+ .collect()
}
/// Show a pane in a client: switch the client to the pane's window and make
/// the pane the active one.
-pub fn show_pane(client: &str, pane_id: &str) -> Result<(), String> {
- run_tmux(&["switch-client", "-c", client, "-t", pane_id])?;
+///
+/// `session`: the client's own session, when it holds the pane's window;
+/// the client stays on it (a pane target alone lets tmux pick a session,
+/// which in a session group may be another one).
+pub fn show_pane(client: &str, pane_id: &str, session: Option<&str>) -> Result<(), String> {
+ let target = match session {
+ Some(session) => {
+ let window = pane_format(pane_id, "#{window_id}").ok_or_else(|| format!("no pane {pane_id}"))?;
+ format!("{session}:{window}")
+ }
+ None => pane_id.to_string(),
+ };
+ run_tmux(&["switch-client", "-c", client, "-t", &target])?;
run_tmux(&["select-pane", "-t", pane_id]).map(|_| ())
}