use cmdtree::completion::*;
use cmdtree::{Builder, BuilderChain};
fn main() {
let cmder = Builder::default_config("arg-completion")
.add_action("path", "complete path names", |_, _| ())
.add_action("no-complete", "", |_, _| ())
.begin_class("nested", "")
.add_action("path", "", |_, _| ())
.into_commander()
.unwrap();
cmder.run_with_completion(|c| ArgCompleter {
items: create_action_completion_items(c),
});
}
struct ArgCompleter<'a> {
items: Vec<ActionMatch<'a>>,
}
impl<'a, T: Terminal> Completer<T> for ArgCompleter<'a> {
fn complete(
&self,
_word: &str,
prompter: &Prompter<T>,
_start: usize,
end: usize,
) -> Option<Vec<Completion>> {
let actions = [".path", "nested..path"];
let line = prompter.buffer();
let action_match = self.items.iter().find(|x| {
line.starts_with(x.info.completestr.as_str())
&& actions.contains(&x.qualified_path.as_str())
})?;
let path_completer = linefeed::complete::PathCompleter;
let arg_line = &line[action_match.info.completestr.len()..]; let word_start = linefeed::complete::word_break_start(arg_line, " "); let word = &arg_line[word_start..];
path_completer.complete(word, prompter, word_start, end)
}
}