mirror of
https://github.com/rust-lang/cargo.git
synced 2025-10-01 11:30:39 +00:00
Reformat main::list_commands to return CommandInfo over a tuple
This commit is contained in:
parent
cef7c5667c
commit
c3422cfc40
@ -69,14 +69,17 @@ Run with 'cargo -Z [FLAG] [SUBCOMMAND]'"
|
|||||||
if args.is_present("list") {
|
if args.is_present("list") {
|
||||||
println!("Installed Commands:");
|
println!("Installed Commands:");
|
||||||
for command in list_commands(config) {
|
for command in list_commands(config) {
|
||||||
let (command, path) = command;
|
match command {
|
||||||
if is_verbose {
|
CommandInfo::BuiltIn { name } => {
|
||||||
match path {
|
println!(" {:<20} {}", name)
|
||||||
Some(p) => println!(" {:<20} {}", command, p),
|
}
|
||||||
None => println!(" {:<20}", command),
|
CommandInfo::External { name, path } => {
|
||||||
|
if is_verbose {
|
||||||
|
println!(" {:<20} {}", name, path.display())
|
||||||
|
} else {
|
||||||
|
println!(" {:<20}", name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
println!(" {}", command);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
@ -411,3 +411,18 @@ pub fn values(args: &ArgMatches, name: &str) -> Vec<String> {
|
|||||||
.map(|s| s.to_string())
|
.map(|s| s.to_string())
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, PartialOrd, Eq, Ord)]
|
||||||
|
pub enum CommandInfo {
|
||||||
|
BuiltIn { name: String },
|
||||||
|
External { name: String, path: PathBuf },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CommandInfo {
|
||||||
|
pub fn name(&self) -> String {
|
||||||
|
match self {
|
||||||
|
CommandInfo::BuiltIn { name, .. } => name.to_string(),
|
||||||
|
CommandInfo::External { name, .. } => name.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -26,6 +26,8 @@ mod cli;
|
|||||||
mod command_prelude;
|
mod command_prelude;
|
||||||
mod commands;
|
mod commands;
|
||||||
|
|
||||||
|
use command_prelude::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
cargo::core::maybe_allow_nightly_features();
|
cargo::core::maybe_allow_nightly_features();
|
||||||
@ -81,7 +83,7 @@ fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<Str
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// List all runnable commands
|
/// List all runnable commands
|
||||||
fn list_commands(config: &Config) -> BTreeSet<(String, Option<String>)> {
|
fn list_commands(config: &Config) -> BTreeSet<CommandInfo> {
|
||||||
let prefix = "cargo-";
|
let prefix = "cargo-";
|
||||||
let suffix = env::consts::EXE_SUFFIX;
|
let suffix = env::consts::EXE_SUFFIX;
|
||||||
let mut commands = BTreeSet::new();
|
let mut commands = BTreeSet::new();
|
||||||
@ -101,16 +103,18 @@ fn list_commands(config: &Config) -> BTreeSet<(String, Option<String>)> {
|
|||||||
}
|
}
|
||||||
if is_executable(entry.path()) {
|
if is_executable(entry.path()) {
|
||||||
let end = filename.len() - suffix.len();
|
let end = filename.len() - suffix.len();
|
||||||
commands.insert((
|
commands.insert(CommandInfo::External {
|
||||||
filename[prefix.len()..end].to_string(),
|
name: filename[prefix.len()..end].to_string(),
|
||||||
Some(path.display().to_string()),
|
path: path.clone(),
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for cmd in commands::builtin() {
|
for cmd in commands::builtin() {
|
||||||
commands.insert((cmd.get_name().to_string(), None));
|
commands.insert(CommandInfo::BuiltIn {
|
||||||
|
name: cmd.get_name().to_string(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
commands
|
commands
|
||||||
@ -121,7 +125,8 @@ fn find_closest(config: &Config, cmd: &str) -> Option<String> {
|
|||||||
// Only consider candidates with a lev_distance of 3 or less so we don't
|
// Only consider candidates with a lev_distance of 3 or less so we don't
|
||||||
// suggest out-of-the-blue options.
|
// suggest out-of-the-blue options.
|
||||||
cmds.into_iter()
|
cmds.into_iter()
|
||||||
.map(|(c, _)| (lev_distance(&c, cmd), c))
|
.map(|c| c.name())
|
||||||
|
.map(|c| (lev_distance(&c, cmd), c))
|
||||||
.filter(|&(d, _)| d < 4)
|
.filter(|&(d, _)| d < 4)
|
||||||
.min_by_key(|a| a.0)
|
.min_by_key(|a| a.0)
|
||||||
.map(|slot| slot.1)
|
.map(|slot| slot.1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user