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 match crate::cli::request(session, &req, false).await {
6 Ok(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 1;
13 }
14 }
15 } else {
16 println!(
17 "{:<12} {:<8} {:<10} {:<6} UPTIME",
18 "NAME", "PID", "STATE", "EXIT"
19 );
20 for p in &processes {
21 let state = match p.state {
22 ProcessState::Running => "running",
23 ProcessState::Exited => "exited",
24 };
25 let exit = p.exit_code.map(|c| c.to_string()).unwrap_or("-".into());
26 let uptime = p.uptime_secs.map(format_uptime).unwrap_or("-".into());
27 println!(
28 "{:<12} {:<8} {:<10} {:<6} {}",
29 p.name, p.pid, state, exit, uptime
30 );
31 }
32 }
33 0
34 }
35 Ok(Response::Error { code, message }) => {
36 eprintln!("error: {}", message);
37 code
38 }
39 _ => 1,
40 }
41}
42
43fn format_uptime(secs: u64) -> String {
44 if secs < 60 {
45 format!("{}s", secs)
46 } else if secs < 3600 {
47 format!("{}m{}s", secs / 60, secs % 60)
48 } else {
49 format!("{}h{}m", secs / 3600, (secs % 3600) / 60)
50 }
51}