use forge::signal::compactor;
use once_cell::sync::Lazy;
use regex::Regex;
static PROGRESS_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"(?m)^\s+[\d\.]+%\[[\s=>-]+\][^\n]+\n?").unwrap());
static HTTP_VERBOSE_RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"(?m)^(HTTP request sent|Location:|Resolving |Connecting to )[^\n]+\n?").unwrap()
});
pub fn compress_wget(raw: &str) -> String {
let cleaned = compactor::normalise(raw);
let s = PROGRESS_RE.replace_all(&cleaned, "");
let s = HTTP_VERBOSE_RE.replace_all(&s, "");
let kept: Vec<&str> = s
.lines()
.filter(|l| {
let t = l.trim();
t.is_empty()
|| t.starts_with("Saving to:")
|| t.contains("saved")
|| t.contains("Downloaded")
|| t.contains("ERROR")
|| t.contains("error")
|| t.contains("failed")
|| t.contains("200 OK")
|| t.contains("404")
|| t.contains("403")
})
.collect();
if kept.is_empty() {
return compactor::collapse_blanks(&s);
}
kept.join("\n")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn strips_progress_bars() {
let raw = "Resolving example.com...\nConnecting to example.com...\nHTTP request sent, awaiting response... 200 OK\n 100%[==============================>] 1,234,567 1.23MB/s\n2024-01-01 12:00:00 (1.23 MB/s) - 'file.tar.gz' saved [1234567]\n";
let out = compress_wget(raw);
assert!(!out.contains("100%"), "{out}");
assert!(!out.contains("Resolving example.com"), "{out}");
assert!(out.contains("saved"), "{out}");
}
}