Skip to main content

agent_procs/cli/
status.rs

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