Skip to content

Commit

Permalink
When -v is passed with --list, display path to custom commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Woolcock committed Feb 14, 2018
1 parent d0ca5bc commit f76db9c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
21 changes: 16 additions & 5 deletions src/bin/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,15 @@ fn execute(flags: Flags, config: &mut Config) -> CliResult {
if flags.flag_list {
println!("Installed Commands:");
for command in list_commands(config) {
println!(" {}", command);
let (command, path) = command;
if flags.flag_verbose > 0 {
match path {
Some(p) => println!(" {:<20} {}", command, p),
None => println!(" {:<20}", command),
}
} else {
println!(" {}", command);
}
}
return Ok(());
}
Expand Down Expand Up @@ -301,7 +309,7 @@ fn find_closest(config: &Config, cmd: &str) -> Option<String> {
// Only consider candidates with a lev_distance of 3 or less so we don't
// suggest out-of-the-blue options.
let mut filtered = cmds.iter()
.map(|c| (lev_distance(c, cmd), c))
.map(|&(ref c, _)| (lev_distance(c, cmd), c))
.filter(|&(d, _)| d < 4)
.collect::<Vec<_>>();
filtered.sort_by(|a, b| a.0.cmp(&b.0));
Expand Down Expand Up @@ -347,7 +355,7 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[String]) -> C
}

/// List all runnable commands
fn list_commands(config: &Config) -> BTreeSet<String> {
fn list_commands(config: &Config) -> BTreeSet<(String, Option<String>)> {
let prefix = "cargo-";
let suffix = env::consts::EXE_SUFFIX;
let mut commands = BTreeSet::new();
Expand All @@ -367,13 +375,16 @@ fn list_commands(config: &Config) -> BTreeSet<String> {
}
if is_executable(entry.path()) {
let end = filename.len() - suffix.len();
commands.insert(filename[prefix.len()..end].to_string());
commands.insert(
(filename[prefix.len()..end].to_string(),
Some(path.display().to_string()))
);
}
}
}

macro_rules! add_cmd {
($cmd:ident) => ({ commands.insert(stringify!($cmd).replace("_", "-")); })
($cmd:ident) => ({ commands.insert((stringify!($cmd).replace("_", "-"), None)); })
}
each_subcommand!(add_cmd);
commands
Expand Down
4 changes: 2 additions & 2 deletions tests/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn list_command_looks_at_path() {
.env("PATH", &path);
let output = output.exec_with_output().unwrap();
let output = str::from_utf8(&output.stdout).unwrap();
assert!(output.contains("\n 1\n"), "missing 1: {}", output);
assert!(output.contains("\n 1 "), "missing 1: {}", output);
}

// windows and symlinks don't currently agree that well
Expand All @@ -95,7 +95,7 @@ fn list_command_resolves_symlinks() {
.env("PATH", &path);
let output = output.exec_with_output().unwrap();
let output = str::from_utf8(&output.stdout).unwrap();
assert!(output.contains("\n 2\n"), "missing 2: {}", output);
assert!(output.contains("\n 2 "), "missing 2: {}", output);
}

#[test]
Expand Down

0 comments on commit f76db9c

Please sign in to comment.