Skip to main content

sqz_engine/cmd_formatters/
mod.rs

1mod preprocess;
2mod truncate;
3mod test_output;
4mod git;
5mod cargo;
6mod npm;
7mod docker;
8mod kubectl;
9mod system;
10mod js;
11mod lint;
12mod gh;
13mod python;
14mod go;
15mod cloud;
16mod jvm;
17mod ruby;
18#[cfg(test)]
19mod props;
20
21pub fn format_command(cmd: &str, output: &str) -> Option<String> {
22    let cleaned = preprocess::clean(output);
23    let input = if cleaned.len() < output.len() { &cleaned } else { output };
24
25    dispatch(cmd, input)
26}
27
28fn dispatch(cmd: &str, output: &str) -> Option<String> {
29    let mut parts: Vec<&str> = cmd.split_whitespace().collect();
30
31    // `bundle exec rspec ...` / `bin/rails ...` — unwrap the bundler prefix so
32    // the real Ruby tool is dispatched. `bundle install/update` is left intact.
33    if parts.first().map(|s| s.rsplit('/').next().unwrap_or(s)) == Some("bundle")
34        && parts.get(1) == Some(&"exec")
35    {
36        parts.drain(0..2);
37    }
38
39    let base = parts.first().map(|s| s.rsplit('/').next().unwrap_or(s)).unwrap_or("");
40
41    match base {
42        // Version control
43        "git" => git::format_git(parts.get(1).copied(), output),
44        "gh" => gh::format_gh(parts.get(1).copied(), output),
45
46        // Rust
47        "cargo" => cargo::format_cargo(parts.get(1).copied(), output),
48
49        // JavaScript/TypeScript
50        "npm" | "npx" => npm::format_npm(parts.get(1).copied(), output),
51        "pnpm" => npm::format_pnpm(parts.get(1).copied(), output),
52        "yarn" | "bun" => npm::format_npm(parts.get(1).copied(), output),
53        "tsc" => Some(js::format_tsc(output)),
54        "eslint" | "biome" => Some(lint::format_lint(output)),
55
56        // Python
57        "pytest" => Some(test_output::format_test_failures(output)),
58        "python" | "python3" if cmd.contains("pytest") || cmd.contains("-m pytest") => {
59            Some(test_output::format_test_failures(output))
60        }
61        "ruff" => python::format_python(cmd, parts.get(1).copied(), output),
62        "mypy" => python::format_python(cmd, None, output),
63        "pip" | "pip3" => python::format_python(cmd, parts.get(1).copied(), output),
64
65        // Go
66        "go" => go::format_go(parts.get(1).copied(), output),
67        "golangci-lint" => Some(lint::format_lint(output)),
68
69        // JVM
70        "gradle" | "gradlew" | "./gradlew" => jvm::format_gradle(parts.get(1).copied(), output),
71        "mvn" | "maven" => jvm::format_maven(parts.get(1).copied(), output),
72
73        // Ruby
74        "rspec" => Some(ruby::format_rspec(output)),
75        "rubocop" => Some(ruby::format_rubocop(output)),
76        "rake" | "rails" => ruby::format_rake(parts.get(1).copied(), output),
77        "bundle" | "bundler" => ruby::format_bundle(parts.get(1).copied(), output),
78
79        // Containers / orchestration
80        "docker" | "podman" => docker::format_docker(parts.get(1).copied(), output),
81        "kubectl" => kubectl::format_kubectl(parts.get(1).copied(), output),
82
83        // Cloud CLIs
84        "aws" => cloud::format_aws(parts.get(1).copied(), output),
85        "terraform" | "tf" => cloud::format_terraform(parts.get(1).copied(), output),
86        "gcloud" => cloud::format_gcloud(output),
87
88        // System utilities
89        "ls" => Some(system::format_ls(output)),
90        "find" | "fd" => Some(system::format_find(output)),
91        "grep" | "rg" | "ag" => Some(system::format_grep(output)),
92        "tree" => Some(system::format_tree(output)),
93        "curl" | "wget" => Some(system::format_curl(output)),
94
95        _ => None,
96    }
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    fn test_format_command_routing() {
105        assert!(format_command("git status", "nothing to commit").is_some());
106        assert!(format_command("cargo test", "test result: ok").is_some());
107        assert!(format_command("unknown_tool", "output").is_none());
108    }
109
110    #[test]
111    fn test_ansi_stripped_before_dispatch() {
112        let output = "\x1b[32mOn branch main\x1b[0m\nnothing to commit, working tree clean\n";
113        let result = format_command("git status", output);
114        assert_eq!(result, Some("clean".to_string()));
115    }
116
117    #[test]
118    fn test_new_commands_dispatch() {
119        assert!(format_command("gh pr list", "[]").is_some());
120        assert!(format_command("ruff check", "All checks passed!").is_some());
121        assert!(format_command("go test ./...", "ok").is_some());
122        assert!(format_command("grep foo bar.txt", "bar.txt:1:foo").is_some());
123        assert!(format_command("tree", ".\n├── src\n└── Cargo.toml").is_some());
124        assert!(format_command("curl http://example.com", "response").is_some());
125    }
126
127    #[test]
128    fn test_phase4_commands_dispatch() {
129        assert!(format_command("aws s3 ls", "2024-01-01 bucket/file.txt").is_some());
130        assert!(format_command("terraform plan", "No changes. Infrastructure is up-to-date.").is_some());
131        assert!(format_command("gradle build", "BUILD SUCCESSFUL in 3s").is_some());
132        assert!(format_command("mvn compile", "[INFO] BUILD SUCCESS").is_some());
133        assert!(format_command("kubectl describe pod nginx", "Name: nginx").is_some());
134    }
135
136    #[test]
137    fn test_ruby_commands_dispatch() {
138        assert!(format_command("rspec", "1 example, 0 failures").is_some());
139        assert!(
140            format_command("rubocop", "10 files inspected, no offenses detected").is_some()
141        );
142        assert!(format_command(
143            "rake test",
144            "8 runs, 9 assertions, 0 failures, 0 errors, 0 skips"
145        )
146        .is_some());
147        assert!(format_command(
148            "bundle install",
149            "Bundle complete! 85 dependencies"
150        )
151        .is_some());
152        // `bundle exec` prefix unwraps to the real tool.
153        assert!(format_command("bundle exec rspec", "1 example, 0 failures").is_some());
154        // Non-test rake tasks fall through to generic compression.
155        assert!(format_command("rake db:migrate", "== migrating ==").is_none());
156    }
157}