forked from nonsensical-dev/zenyx-engine
check command similarity
This commit is contained in:
parent
f4500478fc
commit
8cf6716a09
1 changed files with 37 additions and 0 deletions
|
@ -62,6 +62,24 @@ pub struct CommandList {
|
||||||
pub aliases: RwLock<HashMap<String, String>>,
|
pub aliases: RwLock<HashMap<String, String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn check_similarity(target: &str, strings: &[String]) -> Option<String> {
|
||||||
|
strings
|
||||||
|
.iter().filter(|s| {
|
||||||
|
target.chars().zip(s.chars()).any(|(c1, c2)| c1 == c2)
|
||||||
|
})
|
||||||
|
.min_by_key(|s| {
|
||||||
|
let mut diff_count = 0;
|
||||||
|
for (c1, c2) in target.chars().zip(s.chars()) {
|
||||||
|
if c1 != c2 {
|
||||||
|
diff_count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
diff_count += target.len().abs_diff(s.len());
|
||||||
|
diff_count
|
||||||
|
})
|
||||||
|
.cloned()
|
||||||
|
}
|
||||||
|
|
||||||
impl CommandList {
|
impl CommandList {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
CommandList {
|
CommandList {
|
||||||
|
@ -131,6 +149,25 @@ impl CommandList {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
eprintln!("Command: '{}' was not found", name.red().italic());
|
eprintln!("Command: '{}' was not found", name.red().italic());
|
||||||
|
|
||||||
|
let most_similar = check_similarity(
|
||||||
|
&name,
|
||||||
|
&self
|
||||||
|
.commands
|
||||||
|
.read()
|
||||||
|
.iter()
|
||||||
|
.map(|cmd| cmd.name.to_string())
|
||||||
|
.collect::<Vec<String>>(),
|
||||||
|
);
|
||||||
|
match most_similar {
|
||||||
|
Some(similar) => {
|
||||||
|
eprintln!(
|
||||||
|
"Did you mean: '{}'?",
|
||||||
|
similar.green().italic().bold()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue