use anyhow::Result;
use clap::Parser;
use folder_auto_banner::cli;
use std::env;
const BUILTIN_ALIASES: &[(&str, &[&str])] = &[
("tree", &["-R", "-D"]), ("flat", &["-o"]), ("compact", &["-c"]), ("verbose", &["-v"]), ("hidden", &["-a"]), ("dirs", &["-D"]), ("new", &["-t"]), ("old", &["-t", "-r"]), ("big", &["-S"]), ("small", &["-S", "-r"]), ("ext", &["-X"]), ("git", &["-G"]), ("nosort", &["-U"]), ("top", &["-S", "-r", "-m", "20"]), ("newest", &["-t", "-r", "-m", "20"]), ("recurse", &["-R"]), ("edit", &["-e"]), ("run", &["-x"]), ];
fn lookup_alias(name: &str) -> Option<&'static [&'static str]> {
BUILTIN_ALIASES
.iter()
.find(|(n, _)| *n == name)
.map(|(_, flags)| *flags)
}
fn expand_aliases_in_args(args: &[String]) -> Vec<String> {
let mut new_args: Vec<String> = vec!["f".to_string(), "banner".to_string()];
for a in args {
if a.starts_with('-') || a.parse::<usize>().is_ok() {
new_args.push(a.clone());
} else if let Some(flags) = lookup_alias(a) {
for flag in flags {
new_args.push(flag.to_string());
}
}
}
new_args
}
const KNOWN_SUBCOMMANDS: &[&str] = &["banner", "env", "install", "config", "daemon", "help"];
fn args_contain_something_useful(args: &[String]) -> bool {
args.iter()
.any(|a| a.starts_with('-') || lookup_alias(a).is_some() || a.parse::<usize>().is_ok())
}
fn should_exit_silently(args: &[String]) -> bool {
!args.is_empty() && !args_contain_something_useful(args)
}
fn main() -> Result<()> {
let args: Vec<String> = env::args().skip(1).collect();
if let Some(first) = args.first() {
if KNOWN_SUBCOMMANDS.contains(&first.as_str()) {
let cli = cli::Cli::parse();
return cli.run();
}
}
if should_exit_silently(&args) {
return Ok(());
}
let has_alias_or_number = args
.iter()
.any(|a| lookup_alias(a).is_some() || a.parse::<usize>().is_ok());
if !has_alias_or_number && args.iter().any(|a| a.starts_with('-')) {
let cli = cli::Cli::parse();
return cli.run();
}
let expanded = expand_aliases_in_args(&args);
let cli = cli::Cli::parse_from(expanded);
cli.run()
}
#[cfg(test)]
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_builtin_aliases_count_is_18() {
assert_eq!(
BUILTIN_ALIASES.len(),
18,
"BUILTIN_ALIASES should have 18 entries"
);
}
#[test]
fn test_builtin_aliases_no_duplicate_names() {
let mut names: Vec<&str> = BUILTIN_ALIASES.iter().map(|(n, _)| *n).collect();
names.sort();
names.dedup();
assert_eq!(
names.len(),
BUILTIN_ALIASES.len(),
"BUILTIN_ALIASES must have no duplicate names"
);
}
#[test]
fn test_builtin_aliases_no_empty_flag_list() {
for (name, flags) in BUILTIN_ALIASES {
assert!(!flags.is_empty(), "alias {:?} has empty flag list", name);
}
}
#[test]
fn test_builtin_aliases_value_flag_pairs_valid() {
const VALUE_TAKING: &[&str] = &["-m", "-f", "-L"];
for (name, flags) in BUILTIN_ALIASES {
let mut i = 0;
while i < flags.len() {
let flag = flags[i];
if VALUE_TAKING.contains(&flag) {
assert!(
i + 1 < flags.len(),
"alias {:?} has value-taking flag {:?} without a value",
name,
flag
);
let value = flags[i + 1];
assert!(
!value.starts_with('-'),
"alias {:?} has value-taking flag {:?} followed by another flag {:?}",
name,
flag,
value
);
i += 2;
} else {
assert!(
flag.starts_with('-'),
"alias {:?} has flag {:?} that doesn't start with '-'",
name,
flag
);
i += 1;
}
}
}
}
#[test]
fn test_lookup_tree() {
assert_eq!(lookup_alias("tree"), Some(&["-R", "-D"][..]));
}
#[test]
fn test_lookup_flat() {
assert_eq!(lookup_alias("flat"), Some(&["-o"][..]));
}
#[test]
fn test_lookup_compact() {
assert_eq!(lookup_alias("compact"), Some(&["-c"][..]));
}
#[test]
fn test_lookup_verbose() {
assert_eq!(lookup_alias("verbose"), Some(&["-v"][..]));
}
#[test]
fn test_lookup_hidden() {
assert_eq!(lookup_alias("hidden"), Some(&["-a"][..]));
}
#[test]
fn test_lookup_dirs() {
assert_eq!(lookup_alias("dirs"), Some(&["-D"][..]));
}
#[test]
fn test_lookup_new() {
assert_eq!(lookup_alias("new"), Some(&["-t"][..]));
}
#[test]
fn test_lookup_old() {
assert_eq!(lookup_alias("old"), Some(&["-t", "-r"][..]));
}
#[test]
fn test_lookup_big() {
assert_eq!(lookup_alias("big"), Some(&["-S"][..]));
}
#[test]
fn test_lookup_small() {
assert_eq!(lookup_alias("small"), Some(&["-S", "-r"][..]));
}
#[test]
fn test_lookup_ext() {
assert_eq!(lookup_alias("ext"), Some(&["-X"][..]));
}
#[test]
fn test_lookup_git() {
assert_eq!(lookup_alias("git"), Some(&["-G"][..]));
}
#[test]
fn test_lookup_nosort() {
assert_eq!(lookup_alias("nosort"), Some(&["-U"][..]));
}
#[test]
fn test_lookup_top() {
assert_eq!(lookup_alias("top"), Some(&["-S", "-r", "-m", "20"][..]));
}
#[test]
fn test_lookup_newest() {
assert_eq!(lookup_alias("newest"), Some(&["-t", "-r", "-m", "20"][..]));
}
#[test]
fn test_lookup_recurse() {
assert_eq!(lookup_alias("recurse"), Some(&["-R"][..]));
}
#[test]
fn test_lookup_edit() {
assert_eq!(lookup_alias("edit"), Some(&["-e"][..]));
}
#[test]
fn test_lookup_run() {
assert_eq!(lookup_alias("run"), Some(&["-x"][..]));
}
#[test]
fn test_lookup_unknown_returns_none() {
assert_eq!(lookup_alias(""), None);
assert_eq!(lookup_alias("foo"), None);
assert_eq!(lookup_alias("Tree"), None); assert_eq!(lookup_alias("TREE"), None); assert_eq!(lookup_alias("t"), None); assert_eq!(lookup_alias("trc"), None); }
#[test]
fn test_expand_aliases_empty_args() {
let result = expand_aliases_in_args(&[]);
assert_eq!(result, vec!["f", "banner"]);
}
#[test]
fn test_should_exit_silently_no_args() {
assert!(!should_exit_silently(&[]));
}
#[test]
fn test_should_exit_silently_unknown_word() {
assert!(should_exit_silently(&["t".to_string()]));
assert!(should_exit_silently(&["foo".to_string()]));
assert!(should_exit_silently(&["Downloads".to_string()]));
}
#[test]
fn test_should_exit_silently_with_flag() {
assert!(!should_exit_silently(&["-e".to_string()]));
assert!(!should_exit_silently(&["-t".to_string()]));
assert!(!should_exit_silently(&["--version".to_string()]));
}
#[test]
fn test_should_exit_silently_with_path() {
assert!(should_exit_silently(&["./src".to_string()]));
assert!(should_exit_silently(&["/tmp".to_string()]));
assert!(should_exit_silently(&["~/Downloads".to_string()]));
}
#[test]
fn test_should_exit_silently_with_alias() {
assert!(!should_exit_silently(&["tree".to_string()]));
assert!(!should_exit_silently(&[
"hidden".to_string(),
"verbose".to_string()
]));
}
#[test]
fn test_should_exit_silently_with_number() {
assert!(!should_exit_silently(&["4".to_string()]));
assert!(!should_exit_silently(&["42".to_string()]));
}
#[test]
fn test_should_exit_silently_mixed() {
assert!(!should_exit_silently(&["t".to_string(), "4".to_string()]));
assert!(should_exit_silently(&[
"t".to_string(),
"./src".to_string()
]));
}
#[test]
fn test_expand_aliases_single_alias() {
let result = expand_aliases_in_args(&["tree".to_string()]);
assert_eq!(result, vec!["f", "banner", "-R", "-D"]);
}
#[test]
fn test_expand_aliases_two_aliases() {
let result = expand_aliases_in_args(&["hidden".to_string(), "verbose".to_string()]);
assert_eq!(result, vec!["f", "banner", "-a", "-v"]);
}
#[test]
fn test_expand_aliases_three_aliases() {
let result = expand_aliases_in_args(&[
"new".to_string(),
"recurse".to_string(),
"hidden".to_string(),
]);
assert_eq!(result, vec!["f", "banner", "-t", "-R", "-a"]);
}
#[test]
fn test_expand_aliases_unknown_word_dropped() {
let result = expand_aliases_in_args(&["nonexistentword".to_string()]);
assert_eq!(result, vec!["f", "banner"]);
}
#[test]
fn test_expand_aliases_drops_paths() {
let result = expand_aliases_in_args(&["tree".to_string(), "./src".to_string()]);
assert_eq!(result, vec!["f", "banner", "-R", "-D"]);
}
#[test]
fn test_expand_aliases_drops_unknown_words() {
let result =
expand_aliases_in_args(&["f".to_string(), "foo".to_string(), "Downloads".to_string()]);
assert_eq!(result, vec!["f", "banner"]);
}
#[test]
fn test_expand_aliases_with_number() {
let result = expand_aliases_in_args(&["1".to_string()]);
assert_eq!(result, vec!["f", "banner", "1"]);
}
#[test]
fn test_expand_aliases_alias_with_value_flags() {
let result = expand_aliases_in_args(&["top".to_string()]);
assert_eq!(result, vec!["f", "banner", "-S", "-r", "-m", "20"]);
}
#[test]
fn test_expand_aliases_mix_of_alias_and_path_drops_path() {
let result = expand_aliases_in_args(&[
"hidden".to_string(),
"/tmp".to_string(),
"verbose".to_string(),
]);
assert_eq!(result, vec!["f", "banner", "-a", "-v"]);
}
}