use ui::backend::Backend;
use super::Input;
use crate::question::{Completions, Options};
#[derive(Debug)]
pub struct InputBuilder<'a> {
opts: Options<'a>,
input: Input<'a>,
}
impl<'a> InputBuilder<'a> {
pub(crate) fn new(name: String) -> Self {
InputBuilder {
opts: Options::new(name),
input: Default::default(),
}
}
crate::impl_options_builder! {
message
when
ask_if_answered
on_esc
}
pub fn default<I: Into<String>>(mut self, default: I) -> Self {
let default = default.into();
let len = default.chars().count();
self.input.default = Some((default, len));
self
}
crate::impl_auto_complete_builder! {
String; input
}
pub fn page_size(mut self, page_size: usize) -> Self {
assert!(page_size >= 5, "page size can be a minimum of 5");
self.input.page_size = page_size;
self
}
pub fn should_loop(mut self, should_loop: bool) -> Self {
self.input.should_loop = should_loop;
self
}
crate::impl_filter_builder! {
String; input
}
crate::impl_validate_builder! {
str; input
}
crate::impl_validate_on_key_builder! {
str; input
}
crate::impl_transform_builder! {
str; input
}
pub fn build(self) -> crate::question::Question<'a> {
crate::question::Question::new(self.opts, crate::question::QuestionKind::Input(self.input))
}
}
impl<'a> From<InputBuilder<'a>> for crate::question::Question<'a> {
fn from(builder: InputBuilder<'a>) -> Self {
builder.build()
}
}