use std::process::Command;
use super::{JUMP_TIMEOUT, RunOutcome, run_with_timeout};
use crate::core::pane::KittyWindow;
pub const REMOTE_CONTROL_HINT: &str = "turn on kitty's remote control: `allow_remote_control socket-only` \
and `listen_on unix:@kitty-{kitty_pid}` in kitty.conf, then restart kitty";
pub fn focus_args(
window: &KittyWindow,
own_kitty_pid: Option<u32>,
in_tmux: bool,
) -> Option<Vec<String>> {
let mut args = vec!["@".to_string()];
match &window.socket {
Some(socket) => args.extend(["--to".to_string(), socket.clone()]),
None => {
let same_kitty =
!in_tmux && own_kitty_pid.is_some() && window.kitty_pid == own_kitty_pid;
if !same_kitty {
return None;
}
}
}
args.extend([
"focus-window".into(),
"--match".into(),
format!("id:{}", window.id),
]);
Some(args)
}
#[derive(Debug, PartialEq, Eq)]
pub enum FocusError {
NoRemoteControl,
Failed(String),
}
fn classify_stderr(stderr: &str) -> FocusError {
if stderr.to_ascii_lowercase().contains("remote control") {
return FocusError::NoRemoteControl;
}
let first = stderr
.lines()
.find(|l| !l.trim().is_empty())
.unwrap_or("kitten failed");
FocusError::Failed(first.trim().to_string())
}
fn run_kitty(args: &[String]) -> Result<RunOutcome, FocusError> {
let run = |bin: &str| {
let mut cmd = Command::new(bin);
cmd.args(args);
run_with_timeout(&mut cmd, JUMP_TIMEOUT)
};
match run("kitten") {
Ok(outcome) => Ok(outcome),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
run("kitty").map_err(|e2| FocusError::Failed(format!("cannot run kitten: {e2}")))
}
Err(e) => Err(FocusError::Failed(format!("cannot run kitten: {e}"))),
}
}
pub fn focus(window: &KittyWindow) -> Result<(), FocusError> {
let own_pid = std::env::var("KITTY_PID")
.ok()
.and_then(|v| v.parse::<u32>().ok());
let in_tmux = std::env::var_os("TMUX").is_some();
let Some(args) = focus_args(window, own_pid, in_tmux) else {
return Err(FocusError::NoRemoteControl);
};
match run_kitty(&args)? {
RunOutcome::Exited(status, _, _) if status.success() => Ok(()),
RunOutcome::Exited(_, _, stderr) => Err(classify_stderr(&stderr)),
RunOutcome::TimedOut if window.socket.is_none() => Err(FocusError::NoRemoteControl),
RunOutcome::TimedOut => Err(FocusError::Failed("timed out waiting for kitty".into())),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn focus_goes_through_the_socket_when_there_is_one() {
let w = KittyWindow {
id: 7,
socket: Some("unix:@kitty-42".into()),
kitty_pid: Some(42),
};
assert_eq!(
focus_args(&w, None, false).unwrap(),
[
"@",
"--to",
"unix:@kitty-42",
"focus-window",
"--match",
"id:7"
]
);
assert!(focus_args(&w, Some(99), true).is_some());
}
#[test]
fn without_a_socket_only_from_the_same_kitty_outside_tmux() {
let w = KittyWindow {
id: 7,
socket: None,
kitty_pid: Some(42),
};
assert_eq!(
focus_args(&w, Some(42), false).unwrap(),
["@", "focus-window", "--match", "id:7"]
);
assert_eq!(focus_args(&w, Some(99), false), None);
assert_eq!(focus_args(&w, Some(42), true), None);
assert_eq!(focus_args(&w, None, false), None);
let unknown = KittyWindow {
id: 7,
socket: None,
kitty_pid: None,
};
assert_eq!(focus_args(&unknown, Some(42), false), None);
}
#[test]
fn classifies_kitty_stderr_case_insensitively() {
assert_eq!(
classify_stderr(
"Error: Remote control is disabled. Add allow_remote_control to your kitty.conf\n"
),
FocusError::NoRemoteControl
);
assert_eq!(
classify_stderr("remote control via TTY is disabled"),
FocusError::NoRemoteControl
);
assert_eq!(
classify_stderr("\n No matching windows \nmore detail\n"),
FocusError::Failed("No matching windows".into())
);
assert_eq!(
classify_stderr(" \n"),
FocusError::Failed("kitten failed".into())
);
}
}