Skip to main content

lean_ctx/core/patterns/
wget.rs

1use regex::Regex;
2use std::sync::OnceLock;
3
4static PROGRESS_RE: OnceLock<Regex> = OnceLock::new();
5
6fn progress_re() -> &'static Regex {
7    PROGRESS_RE.get_or_init(|| Regex::new(r"^\s*\d+K\s+.*\d+%").unwrap())
8}
9
10pub fn compress(output: &str) -> Option<String> {
11    let trimmed = output.trim();
12    if trimmed.is_empty() {
13        return Some("ok".to_string());
14    }
15
16    let useful: Vec<&str> = trimmed
17        .lines()
18        .filter(|l| {
19            let t = l.trim();
20            !t.is_empty()
21                && !progress_re().is_match(t)
22                && !t.starts_with("Length:")
23                && !t.starts_with("Connecting to")
24                && !t.starts_with("Resolving")
25                && !t.starts_with("HTTP request sent")
26                && !t.starts_with("Reusing existing")
27        })
28        .collect();
29
30    let saved = trimmed.lines().find(|l| l.contains("saved"));
31
32    if let Some(saved_line) = saved {
33        let mut result = Vec::new();
34        for line in &useful {
35            if line.contains("Saving to") || line.contains("saved") || line.contains("--") {
36                result.push(line.to_string());
37            }
38        }
39        if result.is_empty() {
40            result.push(saved_line.trim().to_string());
41        }
42        return Some(result.join("\n"));
43    }
44
45    if useful.len() <= 5 {
46        return Some(useful.join("\n"));
47    }
48    Some(format!(
49        "{}\n... ({} more lines)",
50        useful[..3].join("\n"),
51        useful.len() - 3
52    ))
53}