Skip to main content

lean_ctx/core/patterns/
deno.rs

1pub fn compress(cmd: &str, output: &str) -> Option<String> {
2    let trimmed = output.trim();
3    if trimmed.is_empty() {
4        return Some("ok".to_string());
5    }
6
7    if cmd.contains("test") {
8        return Some(compress_test(trimmed));
9    }
10    if cmd.contains("lint") {
11        return Some(compress_lint(trimmed));
12    }
13    if cmd.contains("check") || cmd.contains("compile") {
14        return Some(compress_check(trimmed));
15    }
16    if cmd.contains("fmt") {
17        return Some(compress_fmt(trimmed));
18    }
19    if cmd.contains("task") || cmd.contains("run") {
20        return Some(compact_lines(trimmed, 15));
21    }
22
23    Some(compact_lines(trimmed, 15))
24}
25
26fn compress_test(output: &str) -> String {
27    let mut passed = 0u32;
28    let mut failed = 0u32;
29    let mut ignored = 0u32;
30    let mut time = String::new();
31    let mut failures = Vec::new();
32
33    for line in output.lines() {
34        let trimmed = line.trim();
35        if trimmed.starts_with("ok |") || trimmed.starts_with("test result:") {
36            for part in trimmed.split_whitespace() {
37                if let Ok(n) = part.parse::<u32>() {
38                    if trimmed.contains("passed") && passed == 0 {
39                        passed = n;
40                    } else if trimmed.contains("failed") && failed == 0 {
41                        failed = n;
42                    } else if trimmed.contains("ignored") && ignored == 0 {
43                        ignored = n;
44                    }
45                }
46            }
47            if let Some(pos) = trimmed.rfind('(') {
48                time = trimmed[pos..]
49                    .trim_matches(|c: char| c == '(' || c == ')')
50                    .to_string();
51            }
52        }
53        if trimmed.starts_with("FAILED") || trimmed.starts_with("failures:") {
54            failures.push(trimmed.to_string());
55        }
56        if trimmed.contains("... FAILED") {
57            failures.push(trimmed.to_string());
58        }
59    }
60
61    if passed == 0 && failed == 0 {
62        return compact_lines(output, 10);
63    }
64
65    let mut result = format!("deno test: {passed} passed");
66    if failed > 0 {
67        result.push_str(&format!(", {failed} failed"));
68    }
69    if ignored > 0 {
70        result.push_str(&format!(", {ignored} ignored"));
71    }
72    if !time.is_empty() {
73        result.push_str(&format!(" ({time})"));
74    }
75    for f in failures.iter().take(5) {
76        result.push_str(&format!("\n  {f}"));
77    }
78    result
79}
80
81fn compress_lint(output: &str) -> String {
82    let mut issues = Vec::new();
83    for line in output.lines() {
84        let trimmed = line.trim();
85        if trimmed.contains("(") && (trimmed.contains("warning") || trimmed.contains("error")) {
86            issues.push(trimmed.to_string());
87        }
88    }
89    if issues.is_empty() {
90        if output.contains("No problems found") || output.trim().is_empty() {
91            return "clean".to_string();
92        }
93        return compact_lines(output, 10);
94    }
95    format!(
96        "{} lint issues:\n{}",
97        issues.len(),
98        issues
99            .iter()
100            .take(10)
101            .map(|i| format!("  {i}"))
102            .collect::<Vec<_>>()
103            .join("\n")
104    )
105}
106
107fn compress_check(output: &str) -> String {
108    let errors: Vec<&str> = output
109        .lines()
110        .filter(|l| l.contains("error") || l.contains("Error"))
111        .collect();
112    if errors.is_empty() {
113        return "ok (type check passed)".to_string();
114    }
115    format!(
116        "{} errors:\n{}",
117        errors.len(),
118        errors
119            .iter()
120            .take(10)
121            .map(|e| format!("  {}", e.trim()))
122            .collect::<Vec<_>>()
123            .join("\n")
124    )
125}
126
127fn compress_fmt(output: &str) -> String {
128    let files: Vec<&str> = output.lines().filter(|l| !l.trim().is_empty()).collect();
129    if files.is_empty() {
130        return "ok (formatted)".to_string();
131    }
132    format!("{} files formatted", files.len())
133}
134
135fn compact_lines(text: &str, max: usize) -> String {
136    let lines: Vec<&str> = text.lines().filter(|l| !l.trim().is_empty()).collect();
137    if lines.len() <= max {
138        return lines.join("\n");
139    }
140    format!(
141        "{}\n... ({} more lines)",
142        lines[..max].join("\n"),
143        lines.len() - max
144    )
145}