You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.9 KiB
Rust

use dialoguer::{theme::Theme, FuzzySelect, Select};
use nu_protocol::Span;
use super::{create_labeled_error, UserPrompt};
pub enum GenericSelect<'a> {
Fuzzy(FuzzySelect<'a>),
Normal(Select<'a>),
}
impl<'a> GenericSelect<'a> {
pub fn fuzzy(theme: &'a dyn Theme) -> Self {
Self::Fuzzy(FuzzySelect::with_theme(theme))
}
pub fn normal(theme: &'a dyn Theme) -> Self {
Self::Normal(Select::with_theme(theme))
}
pub fn items<T: ToString>(&mut self, items: &[T]) -> &mut Self {
match self {
GenericSelect::Fuzzy(f) => f.items(items).nop(),
GenericSelect::Normal(n) => n.items(items).nop(),
}
self
}
pub fn default(&mut self, val: usize) -> &mut Self {
match self {
GenericSelect::Fuzzy(f) => f.default(val).nop(),
GenericSelect::Normal(n) => n.default(val).nop(),
}
self
}
pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Self {
match self {
GenericSelect::Fuzzy(f) => f.with_prompt(prompt).nop(),
GenericSelect::Normal(n) => n.with_prompt(prompt).nop(),
}
self
}
}
impl<'a> UserPrompt for GenericSelect<'a> {
type Output = usize;
fn ask(&self, span: Span) -> Result<Self::Output, nu_plugin::LabeledError> {
match self {
GenericSelect::Fuzzy(f) => f.interact(),
GenericSelect::Normal(n) => n.interact(),
}
.map_err(|e| create_labeled_error(e, span))
}
fn ask_opt(&self, span: Span) -> Result<Option<Self::Output>, nu_plugin::LabeledError> {
match self {
GenericSelect::Fuzzy(f) => f.interact_opt(),
GenericSelect::Normal(n) => n.interact_opt(),
}
.map_err(|e| create_labeled_error(e, span))
}
}
trait Nop {
fn nop(&mut self) {}
}
impl<'a> Nop for Select<'a> {}
impl<'a> Nop for FuzzySelect<'a> {}