Skip to main content

lean_ctx/core/patterns/
gem.rs

1//! RubyGems (`gem`) output compression.
2//!
3//! `gem install`/`update` interleaves the few lines that matter
4//! (`Successfully installed …`, gem count) with documentation/fetch noise
5//! (`Fetching`, `Parsing documentation`, `Installing ri/rdoc`).
6//!
7//! NOTE: `gem list` is classified **Verbatim** by the output policy
8//! (`is_package_manager_info`, alongside `npm list`/`pip list`/`cargo tree`) —
9//! installed-package inventories are reference data the agent reads in full, so
10//! they never reach this compressor. The `name (versions)` count+cap path here
11//! therefore serves `gem search` (remote listing noise), not `gem list`.
12
13use crate::core::compressor::strip_ansi;
14
15pub fn compress(cmd: &str, output: &str) -> Option<String> {
16    let trimmed = output.trim();
17    if trimmed.is_empty() {
18        return Some("gem: ok".to_string());
19    }
20    if cmd.contains(" list") || cmd.contains(" search") {
21        return Some(compress_list(trimmed));
22    }
23    Some(compress_install(trimmed))
24}
25
26fn compress_list(output: &str) -> String {
27    let rows: Vec<&str> = output
28        .lines()
29        .map(str::trim)
30        .filter(|l| !l.is_empty() && !l.starts_with("***") && l.contains('('))
31        .collect();
32    if rows.is_empty() {
33        return fallback(output);
34    }
35    let n = rows.len();
36    let cap = 30.min(n);
37    let mut s = format!("gem: {n} gem(s)\n{}", rows[..cap].join("\n"));
38    if n > cap {
39        s.push_str(&format!("\n... +{} more", n - cap));
40    }
41    s
42}
43
44fn compress_install(output: &str) -> String {
45    let mut kept: Vec<String> = Vec::new();
46    for raw in output.lines() {
47        let line = strip_ansi(raw);
48        let t = line.trim();
49        if t.is_empty() {
50            continue;
51        }
52        let l = t.to_ascii_lowercase();
53        if l.starts_with("fetching")
54            || l.starts_with("parsing documentation")
55            || l.starts_with("installing ri")
56            || l.starts_with("installing rdoc")
57            || l.starts_with("done installing documentation")
58            || l.starts_with("building native extensions")
59        {
60            continue;
61        }
62        if l.starts_with("successfully installed")
63            || l.starts_with("successfully uninstalled")
64            || l.contains("gems installed")
65            || l.contains("gem installed")
66            || l.contains("error")
67            || l.contains("could not")
68            || l.contains("conflict")
69        {
70            kept.push(t.to_string());
71        }
72    }
73    if kept.is_empty() {
74        return "gem: ok".to_string();
75    }
76    kept.join("\n")
77}
78
79fn fallback(text: &str) -> String {
80    let lines: Vec<&str> = text.lines().filter(|l| !l.trim().is_empty()).collect();
81    let n = lines.len().min(10);
82    let mut s = lines[..n].join("\n");
83    if lines.len() > n {
84        s.push_str(&format!("\n... (+{} lines)", lines.len() - n));
85    }
86    s
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    #[test]
94    fn install_keeps_success_drops_docs() {
95        let out = "Fetching rails-7.1.0.gem\nFetching activesupport-7.1.0.gem\nSuccessfully installed activesupport-7.1.0\nSuccessfully installed rails-7.1.0\nParsing documentation for rails-7.1.0\nInstalling ri documentation for rails-7.1.0\nDone installing documentation for rails after 3 seconds\n2 gems installed";
96        let r = compress("gem install rails", out).unwrap();
97        assert!(r.contains("Successfully installed rails-7.1.0"), "{r}");
98        assert!(r.contains("2 gems installed"), "{r}");
99        assert!(!r.contains("Fetching"), "drops fetch noise: {r}");
100        assert!(!r.contains("Parsing documentation"), "drops doc noise: {r}");
101    }
102
103    #[test]
104    fn search_counts_and_caps() {
105        // `gem search` (remote) is the reachable list path — `gem list` is
106        // intercepted upstream as Verbatim and never lands here.
107        let out = "*** REMOTE GEMS ***\n\nbundler (2.5.0)\nrails (7.1.0)\nrake (13.1.0)";
108        let r = compress("gem search rails", out).unwrap();
109        assert!(r.contains("gem: 3 gem(s)"), "{r}");
110        assert!(r.contains("rails (7.1.0)"), "{r}");
111    }
112
113    #[test]
114    fn empty_is_ok() {
115        assert_eq!(compress("gem install x", "").unwrap(), "gem: ok");
116    }
117}