use std::io::Write;
use askama::Template;
use crate::analyzer::stats::{AnalysisResult, LanguageSummary, Summary};
use crate::error::Result;
use super::format::{OutputFormat, OutputOptions};
#[derive(Template)]
#[template(path = "report.html")]
struct HtmlReport<'a> {
title: &'a str,
generated_at: String,
summary: &'a Summary,
by_language: Vec<(&'a str, &'a LanguageSummary)>,
elapsed_secs: f64,
}
pub struct HtmlOutput;
impl HtmlOutput {
pub fn new() -> Self {
Self
}
}
impl Default for HtmlOutput {
fn default() -> Self {
Self::new()
}
}
impl OutputFormat for HtmlOutput {
fn name(&self) -> &'static str {
"html"
}
fn extension(&self) -> &'static str {
"html"
}
fn write(
&self,
result: &AnalysisResult,
options: &OutputOptions,
writer: &mut dyn Write,
) -> Result<()> {
let mut by_language: Vec<_> = result
.summary
.by_language
.iter()
.map(|(k, v)| (k.as_str(), v))
.collect();
if let Some(n) = options.top_n {
by_language.truncate(n);
}
let report = HtmlReport {
title: "Codelens - Code Statistics Report",
generated_at: chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string(),
summary: &result.summary,
by_language,
elapsed_secs: result.elapsed.as_secs_f64(),
};
write!(writer, "{}", report.render()?)?;
Ok(())
}
}