Skip to main content

test_connectivity/
test-connectivity.rs

1use kitty_rc::Kitty;
2
3#[tokio::main]
4async fn main() -> Result<(), Box<dyn std::error::Error>> {
5    // Find kitty socket
6    let socket_path = find_kitty_socket()?;
7
8    println!("Connecting to: {}", socket_path);
9
10    // Test basic connection
11    let mut kitty = Kitty::builder()
12        .socket_path(&socket_path)
13        .connect()
14        .await
15        .map_err(|e| format!("Connection failed: {}", e))?;
16
17    println!("✓ Connected successfully!");
18
19    // Test sending a simple command
20    use kitty_rc::command::CommandBuilder;
21
22    let cmd = CommandBuilder::new("ls")
23        .build();
24
25    match kitty.execute(&cmd).await {
26        Ok(response) => {
27            println!("✓ Command executed successfully!");
28            if let Some(data) = response.data {
29                println!("Response: {}", data);
30            }
31        }
32        Err(e) => {
33            eprintln!("✗ Command failed: {}", e);
34        }
35    }
36
37    Ok(())
38}
39
40fn find_kitty_socket() -> Result<String, Box<dyn std::error::Error>> {
41    // Try XDG runtime directory first
42    if let Ok(runtime) = std::env::var("XDG_RUNTIME_DIR") {
43        let dir = std::path::Path::new(&runtime).join("kitty");
44        if let Some(sock) = find_socket_in_dir(&dir) {
45            return Ok(sock);
46        }
47    }
48
49    // Try /run/user/<uid>/kitty
50    let uid = std::env::var("UID").unwrap_or_else(|_| "1000".to_string());
51    let dir = format!("/run/user/{}/kitty", uid);
52    let dir = std::path::Path::new(&dir);
53    if let Some(sock) = find_socket_in_dir(&dir) {
54        return Ok(sock);
55    }
56
57    // Try /tmp/kitty
58    let dir = std::path::Path::new("/tmp/kitty");
59    if let Some(sock) = find_socket_in_dir(&dir) {
60        return Ok(sock);
61    }
62
63    Err("Could not find kitty socket. Please ensure kitty is running with remote control enabled.".into())
64}
65
66fn find_socket_in_dir(dir: &std::path::Path) -> Option<String> {
67    if let Ok(entries) = dir.read_dir() {
68        for entry in entries.flatten() {
69            if let Some(name) = entry.file_name().to_str() {
70                if name.ends_with(".sock") {
71                    return Some(dir.join(name).to_string_lossy().to_string());
72                }
73            }
74        }
75    }
76    None
77}