tree2md 0.14.0

Like the tree command, but outputs in Markdown. Optimized for AI agents.
use crate::hotspot::{format_age, HotSpots, Metric, BULK_COMMIT_FILES};

/// `N commit(s)` with the right plural.
pub fn commits_label(commits: u32) -> String {
    if commits == 1 {
        "1 commit".to_string()
    } else {
        format!("{} commits", commits)
    }
}

/// The "Hot spots" section: the ranked files, hottest first.
pub fn render_hot_spots(hot: &HotSpots, now: i64) -> String {
    let span = &hot.map.span.label;
    let mut out = match hot.metric {
        Metric::Commits => format!("**Hot spots** (commits in the last {}", span),
        Metric::Recency => format!(
            "**Hot spots** (last {}, by recency: a commit's weight halves every 30 days",
            span
        ),
        Metric::Churn => format!("**Hot spots** (last {}, by lines changed", span),
        Metric::Risk => format!(
            "**Hot spots** (last {}, by risk: lines changed × file size",
            span
        ),
    };
    let skipped = hot.map.skipped_bulk;
    if skipped > 0 {
        out.push_str(&format!(
            "; skipped {} bulk commit{} of over {} files",
            skipped,
            if skipped == 1 { "" } else { "s" },
            BULK_COMMIT_FILES
        ));
    }
    out.push_str("):");

    let top = hot.top();
    if top.is_empty() {
        out.push_str(" none\n");
        return out;
    }
    out.push('\n');

    for (i, file) in top.iter().enumerate() {
        let h = &file.heat;
        let detail = match hot.metric {
            Metric::Recency => format!(", score {:.2}", file.score),
            Metric::Risk => format!(", {} lines", file.loc.unwrap_or(0)),
            Metric::Commits | Metric::Churn => String::new(),
        };
        out.push_str(&format!(
            "{}. {}: {}, {} author{}, +{}/-{}{}, {}\n",
            i + 1,
            file.path.display(),
            commits_label(h.commits),
            h.authors,
            if h.authors == 1 { "" } else { "s" },
            h.added,
            h.deleted,
            detail,
            format_age(h.last_commit, now)
        ));
    }
    out
}