Skip to main content

csd/commands/
ps.rs

1//! `csd ps` — list tracked sessions with liveness + live state, mirroring `tmx agents --json`.
2
3use serde::Serialize;
4
5use crate::backend;
6use crate::detect::{self, State};
7use crate::error::Result;
8use crate::session::Session;
9use crate::tmux;
10
11#[derive(Debug, Serialize)]
12pub struct PsResult {
13    pub sessions: Vec<PsEntry>,
14}
15
16#[derive(Debug, Serialize)]
17pub struct PsEntry {
18    #[serde(flatten)]
19    pub session: Session,
20    pub alive: bool,
21    pub state: State,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub marker_warning: Option<String>,
24}
25
26pub fn run() -> Result<PsResult> {
27    let mut entries = Vec::new();
28    for session in Session::list()? {
29        let alive = tmux::has_session(&session.name)?;
30        let state = if alive { detect::detect(&session)? } else { State::Dead };
31        let marker_warning = backend::resolve(&session.backend)
32            .ok()
33            .and_then(|b| backend::marker_warning(b.as_ref(), session.backend_version.as_deref()));
34        entries.push(PsEntry {
35            session,
36            alive,
37            state,
38            marker_warning,
39        });
40    }
41    Ok(PsResult { sessions: entries })
42}