Skip to main content

dejavu/exec/
command_key.rs

1//! `command_key` helpers: strip cosmetic noise, keep significant args.
2
3/// Purely cosmetic flags that don't change *what* ran (spec ยง11.2).
4pub const NOISE_FLAGS: &[&str] = &[
5    "--color",
6    "--colour",
7    "--no-color",
8    "--no-colour",
9    "--progress",
10    "--no-progress",
11];
12
13pub fn is_noise(arg: &str) -> bool {
14    NOISE_FLAGS
15        .iter()
16        .any(|n| arg == *n || arg.starts_with(&format!("{n}=")))
17}
18
19/// Drop noise flags, keeping the rest in order.
20pub fn drop_noise(args: &[String]) -> Vec<String> {
21    args.iter().filter(|a| !is_noise(a)).cloned().collect()
22}
23
24/// Human-facing command string, e.g. `pnpm run test`.
25pub fn command_original(shim: &str, args: &[String]) -> String {
26    if args.is_empty() {
27        shim.to_string()
28    } else {
29        format!("{shim} {}", args.join(" "))
30    }
31}
32
33/// Build a colon-joined key from a family prefix and significant tokens,
34/// falling back to `default` when there are none.
35pub fn build_key(prefix: &str, sig: &[String]) -> String {
36    if sig.is_empty() {
37        format!("{prefix}:default")
38    } else {
39        format!("{prefix}:{}", sig.join(":"))
40    }
41}