autom8/commands/status.rs
1//! Status command handler.
2//!
3//! Displays the current run status for a project or across all projects.
4
5use crate::error::Result;
6use crate::output::{print_global_status, print_sessions_status, print_status};
7use crate::state::StateManager;
8use crate::Runner;
9
10use super::ensure_project_dir;
11
12/// Display status for the current project.
13///
14/// Shows the current run state including branch, current story,
15/// iteration count, and timestamps.
16///
17/// # Arguments
18///
19/// * `runner` - The Runner instance to query status from
20///
21/// # Returns
22///
23/// * `Ok(())` on success
24/// * `Err(Autom8Error)` if reading state fails
25pub fn status_command(runner: &Runner) -> Result<()> {
26 ensure_project_dir()?;
27
28 match runner.status() {
29 Ok(Some(state)) => {
30 print_status(&state);
31 Ok(())
32 }
33 Ok(None) => {
34 println!("No active run.");
35 Ok(())
36 }
37 Err(e) => Err(e),
38 }
39}
40
41/// Display status across all projects.
42///
43/// Shows a summary of all projects with their run status,
44/// highlighting those that need attention (active or failed runs).
45///
46/// # Returns
47///
48/// * `Ok(())` on success
49/// * `Err(Autom8Error)` if reading project statuses fails
50pub fn global_status_command() -> Result<()> {
51 let statuses = crate::config::global_status()?;
52 print_global_status(&statuses);
53 Ok(())
54}
55
56/// Display status for all sessions in a project.
57///
58/// Shows a list of all sessions (worktrees) for the project, including:
59/// - Session ID and worktree path
60/// - Branch name and current state
61/// - Current story (if any)
62/// - Started time / duration
63///
64/// Sessions are sorted with the current session first, then by last active time.
65/// Stale sessions (deleted worktrees) are marked accordingly.
66///
67/// # Arguments
68///
69/// * `project` - Optional project name. If None, uses the current directory to determine the project.
70///
71/// # Returns
72///
73/// * `Ok(())` on success
74/// * `Err(Autom8Error)` if reading session data fails
75pub fn all_sessions_status_command(project: Option<&str>) -> Result<()> {
76 let state_manager = if let Some(project_name) = project {
77 StateManager::for_project(project_name)?
78 } else {
79 ensure_project_dir()?;
80 StateManager::new()?
81 };
82 let sessions = state_manager.list_sessions_with_status()?;
83
84 if sessions.is_empty() {
85 println!("No sessions found for this project.");
86 println!();
87 println!("Run `autom8 run` to start a session.");
88 return Ok(());
89 }
90
91 print_sessions_status(&sessions);
92 Ok(())
93}