aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorIgor Soarez <igor@soarez.org>2026-10-25 06:41:34 +0200
committerIgor Soarez <igor@soarez.org>2026-10-25 06:41:34 +0200
commitb77427190408cbe3ebea5677cd67bce53e73f313 (patch)
treeacffe9de5554747d0ff505e4428a748e2c40ae7d /crates
parentdd3462c3cd0a7ff8dbb31c5b4e6d133a04506419 (diff)
terminals: ask Ghostty only once focus has settled
across a Space switch (a full screen window) tmux sees the new client focused about 0.3 s before Ghostty's front window changes, so learn paired the client with the terminal that had focus. live, raising a full screen window and back crossed the two pairings, and from then on every jump raised the wrong window (old binary: 0 of 10 jumps right). learn now waits a second before asking Ghostty, then still requires the client to be focused. it also drops pairings of clients no longer attached, which piled up with every closed window. e2e step 20: Ghostty lagging behind tmux's focus, crossed pairings healing, pruning; the old learn fails all five. live, two Ghostty windows, one full screen: 16 of 16 jumps right, starting from crossed pairings.
Diffstat (limited to 'crates')
-rw-r--r--crates/dinggerd/src/terminals.rs53
1 files changed, 38 insertions, 15 deletions
diff --git a/crates/dinggerd/src/terminals.rs b/crates/dinggerd/src/terminals.rs
index 413ba0d..85ae398 100644
--- a/crates/dinggerd/src/terminals.rs
+++ b/crates/dinggerd/src/terminals.rs
@@ -12,6 +12,11 @@
//! still focused after Ghostty answered, so a fast switch cannot pair the
//! wrong terminal.
//!
+//! Ghostty's answer lags tmux's focus: across a Space switch (a full screen
+//! window) tmux sees the new client focused about 0.3 s before Ghostty's
+//! front window changes, and meanwhile Ghostty names the terminal that had
+//! focus. So Ghostty is asked only once focus has settled (`SETTLE`).
+//!
//! Apple Events go out from children of the tmux server (the focus hook, and
//! `tmux run-shell` for raising), so macOS attributes them to the terminal
//! that started tmux: Ghostty scripting itself needs no permission.
@@ -112,11 +117,33 @@ fn pairings() -> Vec<(String, Pairing)> {
.collect()
}
-fn client_focused(tty: &str) -> bool {
- tmux::run(&["list-clients", "-F", "#{client_tty}\t#{client_flags}"])
- .unwrap_or_default()
- .lines()
- .any(|line| line.split_once('\t').is_some_and(|(t, flags)| t == tty && flags.split(',').any(|f| f == "focused")))
+/// The pairing options of the attached clients, and whether `tty` is focused.
+fn attached(tty: &str) -> (Vec<String>, bool) {
+ let mut names = Vec::new();
+ let mut focused = false;
+ for line in tmux::run(&["list-clients", "-F", "#{client_tty}\t#{client_flags}"]).unwrap_or_default().lines() {
+ if let Some((t, flags)) = line.split_once('\t') {
+ names.push(option_name(t));
+ focused |= t == tty && flags.split(',').any(|f| f == "focused");
+ }
+ }
+ (names, focused)
+}
+
+/// How long after a focus-in Ghostty is asked: its front window lags
+/// tmux's focus by about 0.3 s across a Space switch.
+const SETTLE: std::time::Duration = std::time::Duration::from_millis(1000);
+
+fn ghostty_focused_terminal() -> Result<String, String> {
+ let script = format!(
+ "tell application id \"{GHOSTTY_ID}\" to if frontmost then return id of focused terminal of selected tab of front window"
+ );
+ let out = Command::new(osascript()).args(["-e", &script]).output().map_err(|err| err.to_string())?;
+ let terminal = String::from_utf8_lossy(&out.stdout).trim().to_string();
+ if !valid_id(&terminal) {
+ return Err(format!("Ghostty reported no focused terminal ({terminal:?})"));
+ }
+ Ok(terminal)
}
/// `dingger terminal focused`: the client gained focus; pair it with the
@@ -129,17 +156,12 @@ pub fn learn(client: &str) -> Result<(), String> {
if !is_ghostty(&app) {
return Ok(());
}
- let script = format!(
- "tell application id \"{GHOSTTY_ID}\" to if frontmost then return id of focused terminal of selected tab of front window"
- );
- let out = Command::new(osascript()).args(["-e", &script]).output().map_err(|err| err.to_string())?;
- let terminal = String::from_utf8_lossy(&out.stdout).trim().to_string();
- if !valid_id(&terminal) {
- return Err(format!("Ghostty reported no focused terminal ({terminal:?})"));
- }
+ std::thread::sleep(SETTLE);
+ let terminal = ghostty_focused_terminal()?;
// Focus moved on while Ghostty answered: the answer may be another
// terminal's. The next focus-in pairs it.
- if !client_focused(tty) {
+ let (live, focused) = attached(tty);
+ if !focused {
return Err("focus moved on; not paired".to_string());
}
let ours = option_name(tty);
@@ -149,7 +171,8 @@ pub fn learn(client: &str) -> Result<(), String> {
return Ok(()); // a later focus-in already paired it
}
// A terminal holds one client: an older pairing with it is stale.
- if name != ours && pairing.terminal == terminal && pairing.focused_ms <= started {
+ // So is a client's that is no longer attached.
+ if name != ours && ((pairing.terminal == terminal && pairing.focused_ms <= started) || !live.contains(&name)) {
stale.push(name);
}
}