2020-09-16 04:37:43 +00:00
|
|
|
use crate::completion::{Completer, Context, Suggestion};
|
2020-08-21 19:37:51 +00:00
|
|
|
use crate::context;
|
|
|
|
|
2020-09-16 04:37:43 +00:00
|
|
|
pub struct FlagCompleter {
|
|
|
|
pub(crate) cmd: String,
|
|
|
|
}
|
2020-08-21 19:37:51 +00:00
|
|
|
|
2020-09-16 04:37:43 +00:00
|
|
|
impl Completer for FlagCompleter {
|
|
|
|
fn complete(&self, ctx: &Context<'_>, partial: &str) -> Vec<Suggestion> {
|
2020-08-21 19:37:51 +00:00
|
|
|
let context: &context::Context = ctx.as_ref();
|
|
|
|
|
2020-09-16 04:37:43 +00:00
|
|
|
if let Some(cmd) = context.registry.get_command(&self.cmd) {
|
2020-08-21 19:37:51 +00:00
|
|
|
let sig = cmd.signature();
|
|
|
|
let mut suggestions = Vec::new();
|
|
|
|
for (name, (named_type, _desc)) in sig.named.iter() {
|
|
|
|
suggestions.push(format!("--{}", name));
|
|
|
|
|
|
|
|
if let Some(c) = named_type.get_short() {
|
|
|
|
suggestions.push(format!("-{}", c));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
suggestions
|
|
|
|
.into_iter()
|
|
|
|
.filter(|v| v.starts_with(partial))
|
|
|
|
.map(|v| Suggestion {
|
|
|
|
replacement: format!("{} ", v),
|
|
|
|
display: v,
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|