use std::sync::{Arc, Mutex};
use crate::session_manager::{ManagedError, ManagedTmuxDriver};
pub(crate) struct FakeTmux {
pub(crate) sends: Mutex<Vec<(String, String)>>,
pub(crate) pane_sends: Mutex<Vec<(String, String, String)>>,
pub(crate) env_sets: Mutex<Vec<(String, String, String)>>,
pane_text: Mutex<String>,
}
impl FakeTmux {
pub(crate) fn new() -> Arc<Self> {
Arc::new(Self {
sends: Mutex::new(Vec::new()),
pane_sends: Mutex::new(Vec::new()),
env_sets: Mutex::new(Vec::new()),
pane_text: Mutex::new(String::new()),
})
}
fn answer_probe(&self, text: &str) -> bool {
let Some(out) = crate::runtime::pane_handshake::probe_reply(text) else {
return false;
};
let mut pane = self.pane_text.lock().expect("FakeTmux pane text poisoned");
pane.push_str(&out);
pane.push('\n');
true
}
}
impl ManagedTmuxDriver for FakeTmux {
fn create_session(&self, _name: &str, _workdir: &str) -> Result<(), ManagedError> {
Ok(())
}
fn kill_session(&self, _name: &str) -> Result<(), ManagedError> {
Ok(())
}
fn send_line(&self, name: &str, text: &str) -> Result<(), ManagedError> {
if self.answer_probe(text) {
return Ok(());
}
self.sends
.lock()
.expect("FakeTmux send log mutex poisoned")
.push((name.to_owned(), text.to_owned()));
Ok(())
}
fn send_line_to_pane(&self, name: &str, pane_id: &str, text: &str) -> Result<(), ManagedError> {
if self.answer_probe(text) {
return Ok(());
}
self.pane_sends
.lock()
.expect("FakeTmux pane-send log mutex poisoned")
.push((name.to_owned(), pane_id.to_owned(), text.to_owned()));
Ok(())
}
fn capture(&self, _name: &str, _lines: usize) -> Result<String, ManagedError> {
Ok(self
.pane_text
.lock()
.expect("FakeTmux pane text poisoned")
.clone())
}
fn capture_pane(
&self,
name: &str,
_pane_id: &str,
lines: usize,
) -> Result<String, ManagedError> {
self.capture(name, lines)
}
fn list_sessions(&self) -> Result<Vec<String>, ManagedError> {
Ok(Vec::new())
}
fn session_exists_checked(&self, _name: &str) -> Result<bool, ManagedError> {
Ok(false)
}
fn set_environment(&self, name: &str, key: &str, value: &str) -> Result<(), ManagedError> {
self.env_sets
.lock()
.expect("FakeTmux env-set log mutex poisoned")
.push((name.to_owned(), key.to_owned(), value.to_owned()));
Ok(())
}
}