agent_procs/cli/
status.rs1use crate::protocol::{ProcessState, Request, Response};
2
3pub async fn execute(session: &str, json: bool) -> i32 {
4 let req = Request::Status;
5 crate::cli::request_and_handle(session, &req, false, |resp| match resp {
6 Response::Status { processes } => {
7 if json {
8 match serde_json::to_string_pretty(&processes) {
9 Ok(json) => println!("{}", json),
10 Err(e) => {
11 eprintln!("error: failed to serialize status: {}", e);
12 return Some(1);
13 }
14 }
15 } else {
16 let has_urls = processes.iter().any(|p| p.url.is_some());
17 if has_urls {
18 println!(
19 "{:<12} {:<8} {:<10} {:<6} {:<30} UPTIME",
20 "NAME", "PID", "STATE", "EXIT", "URL"
21 );
22 } else {
23 println!(
24 "{:<12} {:<8} {:<10} {:<6} UPTIME",
25 "NAME", "PID", "STATE", "EXIT"
26 );
27 }
28 for p in &processes {
29 let state = match p.state {
30 ProcessState::Running => "running",
31 ProcessState::Exited => "exited",
32 };
33 let exit = p.exit_code.map_or("-".into(), |c| c.to_string());
34 let uptime = p.uptime_secs.map_or("-".into(), format_uptime);
35 if has_urls {
36 let url = p.url.as_deref().unwrap_or("-");
37 println!(
38 "{:<12} {:<8} {:<10} {:<6} {:<30} {}",
39 p.name, p.pid, state, exit, url, uptime
40 );
41 } else {
42 println!(
43 "{:<12} {:<8} {:<10} {:<6} {}",
44 p.name, p.pid, state, exit, uptime
45 );
46 }
47 }
48 }
49 Some(0)
50 }
51 _ => None,
52 })
53 .await
54}
55
56fn format_uptime(secs: u64) -> String {
57 if secs < 60 {
58 format!("{}s", secs)
59 } else if secs < 3600 {
60 format!("{}m{}s", secs / 60, secs % 60)
61 } else {
62 format!("{}h{}m", secs / 3600, (secs % 3600) / 60)
63 }
64}