use std::path::Path;
use std::time::Duration;
use anyhow::Result;
use serde_json::json;
use trusty_code::cli_client::StdioRpcClient;
use trusty_code::cli_client::render::render_event_line;
use trusty_code::events::Event;
use super::tcode_exe;
const POLL_INTERVAL: Duration = Duration::from_millis(500);
pub async fn run(project: &Path, session_id: &str) -> Result<()> {
let exe = tcode_exe::resolve()?;
let mut client = StdioRpcClient::spawn(&exe, project)?;
let attach_result = client
.call("session.attach", json!({"session_id": session_id}))
.await?;
let replay = attach_result
.get("events")
.and_then(|v| v.as_array())
.cloned()
.unwrap_or_default();
let mut done = false;
for raw in &replay {
if let Ok(envelope) =
serde_json::from_value::<trusty_code::events::SessionEventEnvelope>(raw.clone())
{
println!("{}", render_event_line(&envelope));
done = done || matches!(envelope.event, Event::SessionDone { .. });
}
}
while !done {
tokio::select! {
_ = tokio::signal::ctrl_c() => {
eprintln!("tcode attach: detaching (Ctrl-C)");
break;
}
notification = client.next_notification(POLL_INTERVAL) => {
match notification? {
Some(envelope) => {
println!("{}", render_event_line(&envelope));
done = matches!(envelope.event, Event::SessionDone { .. });
}
None => continue,
}
}
}
}
client.shutdown(Duration::from_secs(5)).await?;
Ok(())
}