use keymap_core::cmd::CommandIndex;
#[derive(Debug, PartialEq)]
enum Action {
Write,
WriteQuit,
WriteQuitAll,
Quit,
QuitAll,
}
fn main() {
let mut idx = CommandIndex::new();
idx.bind("write", Action::Write);
idx.bind("write-quit", Action::WriteQuit);
idx.bind("write-quit-all", Action::WriteQuitAll);
idx.bind("quit", Action::Quit);
idx.bind("quit-all", Action::QuitAll);
println!("All commands:");
for (name, action) in idx.iter() {
println!(" {name:20} -> {action:?}");
}
println!("\nCompletions for \"w\":");
for (name, action) in idx.complete("w") {
println!(" {name:20} -> {action:?}");
}
println!("\nCompletions for \"write-q\":");
for (name, action) in idx.complete("write-q") {
println!(" {name:20} -> {action:?}");
}
println!("\nExact lookup:");
match idx.get("write") {
Some(action) => println!(" \"write\" -> {action:?}"),
None => println!(" \"write\" -> unknown"),
}
let user_input = "Write";
let normalized = user_input.to_lowercase();
println!("\nUser typed {user_input:?}, normalized to {normalized:?}:");
match idx.get(&normalized) {
Some(action) => println!(" -> {action:?}"),
None => println!(" -> unknown"),
}
let mut vim = CommandIndex::new();
vim.bind(":w", "write");
vim.bind(":wq", "write-quit");
vim.bind(":wqa", "write-quit-all");
println!("\nVim-style coexistence:");
println!(" get(\":w\") = {:?}", vim.get(":w"));
let completions: Vec<&str> = vim.complete(":w").map(|(n, _)| n).collect();
println!(" complete(\":w\") = {completions:?}");
}