2026-03-05 19:17:22 -05:00
|
|
|
// Tmux interaction: pane detection and prompt injection.
|
|
|
|
|
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
use std::thread;
|
|
|
|
|
use std::time::Duration;
|
|
|
|
|
use tracing::info;
|
|
|
|
|
|
|
|
|
|
/// Find Claude Code's tmux pane by scanning for the "claude" process.
|
|
|
|
|
pub fn find_claude_pane() -> Option<String> {
|
|
|
|
|
let out = Command::new("tmux")
|
|
|
|
|
.args([
|
|
|
|
|
"list-panes",
|
|
|
|
|
"-a",
|
|
|
|
|
"-F",
|
|
|
|
|
"#{session_name}:#{window_index}.#{pane_index}\t#{pane_current_command}",
|
|
|
|
|
])
|
|
|
|
|
.output()
|
|
|
|
|
.ok()?;
|
|
|
|
|
|
|
|
|
|
let stdout = String::from_utf8_lossy(&out.stdout);
|
|
|
|
|
for line in stdout.lines() {
|
|
|
|
|
if let Some((pane, cmd)) = line.split_once('\t') {
|
|
|
|
|
if cmd == "claude" {
|
|
|
|
|
return Some(pane.to_string());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Send a prompt to a tmux pane. Returns true on success.
|
|
|
|
|
///
|
2026-03-08 18:49:30 -04:00
|
|
|
/// Types the message literally then presses Enter.
|
2026-03-05 19:17:22 -05:00
|
|
|
pub fn send_prompt(pane: &str, msg: &str) -> bool {
|
2026-03-05 21:15:40 -05:00
|
|
|
let preview: String = msg.chars().take(100).collect();
|
|
|
|
|
info!("SEND [{pane}]: {preview}...");
|
2026-03-05 19:17:22 -05:00
|
|
|
|
2026-03-08 19:41:32 -04:00
|
|
|
// Type the message literally (flatten newlines — they'd submit the input early)
|
|
|
|
|
let flat: String = msg.chars().map(|c| if c == '\n' { ' ' } else { c }).collect();
|
2026-03-08 18:39:47 -04:00
|
|
|
let ok = Command::new("tmux")
|
2026-03-08 19:41:32 -04:00
|
|
|
.args(["send-keys", "-t", pane, "-l", &flat])
|
2026-03-08 18:39:47 -04:00
|
|
|
.output()
|
|
|
|
|
.is_ok();
|
|
|
|
|
if !ok {
|
2026-03-05 19:17:22 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
2026-03-08 18:49:30 -04:00
|
|
|
thread::sleep(Duration::from_millis(200));
|
2026-03-05 19:17:22 -05:00
|
|
|
|
|
|
|
|
// Submit
|
2026-03-08 18:49:30 -04:00
|
|
|
Command::new("tmux")
|
|
|
|
|
.args(["send-keys", "-t", pane, "Enter"])
|
|
|
|
|
.output()
|
|
|
|
|
.is_ok()
|
2026-03-05 19:17:22 -05:00
|
|
|
}
|