use headroom_core::transforms;
pub fn detect_type(content: &str) -> String {
transforms::detect(content).as_str().to_string()
}
pub fn build_preview(type_str: &str, content: &str) -> String {
let lines = content.lines().count();
let bytes = content.len();
match type_str {
"build" | "build_output" | "build_error" => {
let e = content.matches("error").count();
let w = content.matches("warning").count();
format!("[{}:{}E {}W {}L]", type_str, e, w, lines)
},
"diff" => {
let f = content.matches("diff --git").count();
let a = content.lines().filter(|l| l.starts_with('+') && !l.starts_with("+++")).count();
let d = content.lines().filter(|l| l.starts_with('-') && !l.starts_with("---")).count();
format!("[diff:{}F +{}/-{} {}L]", f, a, d, lines)
},
"source_code" | "code_rust" | "code_python" | "code_go" | "code_js" | "code_ts" => {
let st = crate::struct_extract::extract_code_structure(content, "");
let mut parts: Vec<String> = Vec::new();
for (key, label) in [
("fns", "fns"),
("structs", "structs"),
("traits", "traits"),
("impls", "impls"),
("classes", "classes"),
("types", "types"),
] {
if let Some(v) = st.get(key) {
if !v.is_empty() {
parts.push(format!("{}{}", v.len(), label));
}
}
}
let summary = if parts.is_empty() {
format!("{}fns", content.matches("fn ").count() + content.matches("def ").count())
} else {
parts.join("|")
};
let sig = st
.get("fns")
.and_then(|v| v.first())
.map(|s| format!(" {}", s.chars().take(48).collect::<String>().trim()))
.unwrap_or_default();
format!("[code:{}{} {}L]", summary, sig, lines)
},
"search" => {
let h = content.lines().filter(|l| l.contains(':')).count();
format!("[search:{}hits {}L]", h, lines)
},
"json_array" => {
let i = content.matches("{\"").count();
format!("[json:{}items {}L]", i, lines)
},
"terminal" => {
let exit_line = content
.lines()
.rev()
.find(|l| l.contains("exit code:") || l.contains("Error:"))
.map(|l| l.trim());
let last_line = content.lines().rev().find(|l| !l.trim().is_empty()).map(|l| l.trim());
let summary = exit_line.or(last_line).unwrap_or("").chars().take(60).collect::<String>();
format!("[terminal:{}L {}]", lines, summary)
},
_ => format!("[{}:{}L {}B]", type_str, lines, bytes),
}
}