Skip to content

Commit

Permalink
check command similarity
Browse files Browse the repository at this point in the history
  • Loading branch information
Caznix committed Dec 6, 2024
1 parent 8dc0765 commit b289e3d
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions engine/src/core/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ pub struct CommandList {
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 {
fn new() -> Self {
CommandList {
Expand Down Expand Up @@ -131,6 +149,25 @@ impl CommandList {
}
} else {
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 => {}
}
}
}
}

0 comments on commit b289e3d

Please sign in to comment.