Skip to main content

lean_ctx_patterns/
deno.rs

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