bctx-weave 0.1.29

bctx-weave — FilterMesh lens pipeline, CLI interception, domain compression
Documentation
use forge::signal::compactor;
use once_cell::sync::Lazy;
use regex::Regex;

// wget/curl progress bars: "100%[====>   ] 1,234,567 1.23MB/s"
static PROGRESS_RE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?m)^\s+[\d\.]+%\[[\s=>-]+\][^\n]+\n?").unwrap());
// HTTP redirect/header lines verbose output
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, "");
    // Keep: "Saving to:", "2024-01-01 filename saved", error lines
    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}");
    }
}