tree2md 0.13.0

Like the tree command, but outputs in Markdown. Optimized for AI agents.
use crate::hotspot::{format_age, HeatMap, BULK_COMMIT_FILES, TOP_N};
use crate::render::pipeline::IrFile;

/// `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 `TOP_N` files that changed most often,
/// hottest first, as given by `IrDir::hot_files`.
pub fn render_hot_spots(ranked: &[&IrFile], heat: &HeatMap, now: i64) -> String {
    let mut out = format!("**Hot spots** (commits in the last {}", heat.span.label);
    if heat.skipped_bulk > 0 {
        out.push_str(&format!(
            "; skipped {} bulk commit{} of over {} files",
            heat.skipped_bulk,
            if heat.skipped_bulk == 1 { "" } else { "s" },
            BULK_COMMIT_FILES
        ));
    }
    out.push_str("):");

    if ranked.is_empty() {
        out.push_str(" none\n");
        return out;
    }
    out.push('\n');

    for (i, file) in ranked.iter().take(TOP_N).enumerate() {
        let h = file.heat.unwrap_or_default();
        out.push_str(&format!(
            "{}. {}: {}, +{}/-{}, {}\n",
            i + 1,
            file.display_path.display(),
            commits_label(h.commits),
            h.added,
            h.deleted,
            format_age(h.last_commit, now)
        ));
    }
    out
}