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                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                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            0
50        }
51        Ok(Response::Error { code, message }) => {
52            eprintln!("error: {}", message);
53            code
54        }
55        _ => 1,
56    }
57}
58
59fn format_uptime(secs: u64) -> String {
60    if secs < 60 {
61        format!("{}s", secs)
62    } else if secs < 3600 {
63        format!("{}m{}s", secs / 60, secs % 60)
64    } else {
65        format!("{}h{}m", secs / 3600, (secs % 3600) / 60)
66    }
67}