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