diff options
-rw-r--r-- | examples/config.rs | 2 | ||||
-rw-r--r-- | examples/simple.rs | 2 | ||||
-rw-r--r-- | src/backend/dialoguer.rs | 18 | ||||
-rw-r--r-- | src/value_builder.rs | 2 | ||||
-rw-r--r-- | tests/bool.rs | 2 | ||||
-rw-r--r-- | tests/common/mod.rs | 6 | ||||
-rw-r--r-- | tests/struct.rs | 2 |
7 files changed, 19 insertions, 15 deletions
diff --git a/examples/config.rs b/examples/config.rs index 453b2e2..25610ff 100644 --- a/examples/config.rs +++ b/examples/config.rs @@ -22,7 +22,7 @@ impl<B: 'static + Backend> CollectionBuilder for ConfigStructBuilder<B> { fn build_collection(&self, value_desc: &str) -> Result<Self::Output> { println!("Building {}", value_desc); - let verbose = bool::builder(self.0).build_value("Be verbose?")?; + let verbose = bool::builder(self.0).build_value("Be verbose?", Some(&false))?; let values = Vec::<u8>::builder(self.0).build_collection("List of values")?; Ok(Configuration { diff --git a/examples/simple.rs b/examples/simple.rs index bd406c4..a9dd6c9 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -2,7 +2,7 @@ use interactive_object_builder::*; use interactive_object_builder::backend::dialoguer::DialoguerBackend; fn main() -> Result<()> { - let b = bool::builder(DialoguerBackend).build_value("Success?")?; + let b = bool::builder(DialoguerBackend).build_value("Success?", Some(&false))?; if b { println!("Success"); diff --git a/src/backend/dialoguer.rs b/src/backend/dialoguer.rs index 564750f..53eae8d 100644 --- a/src/backend/dialoguer.rs +++ b/src/backend/dialoguer.rs @@ -101,11 +101,15 @@ impl<T> ValueBuilder for DialoguerValueBuilder<T> { type Output = T; - fn build_value(&self, question: &str) -> Result<Self::Output> { - dialoguer::Input::<T>::new() - .with_prompt(question) - .interact_text() - .map_err(crate::error::IOBError::from) + fn build_value(&self, question: &str, default: Option<&Self::Output>) -> Result<Self::Output> { + let mut dia = dialoguer::Input::<T>::new(); + dia.with_prompt(question); + + if let Some(def) = default { + dia.with_initial_text(def.to_string()); + } + + dia.interact_text().map_err(crate::error::IOBError::from) } } @@ -121,7 +125,7 @@ impl<T: BuildableValue> CollectionBuilder for DialoguerOptionValueBuilder<T> { if b { T::builder(DialoguerBackend) - .build_value(value_desc) + .build_value(value_desc, None) .map(Some) } else { Ok(None) @@ -140,7 +144,7 @@ impl<T> CollectionBuilder for DialoguerVecBuilder<T> let mut buf = vec![]; loop { - let v: T = T::builder(DialoguerBackend).build_value(value_desc)?; + let v: T = T::builder(DialoguerBackend).build_value(value_desc, None)?; buf.push(v); if !dialoguer::Input::<bool>::new().with_prompt("Another one?").interact_text()? { diff --git a/src/value_builder.rs b/src/value_builder.rs index d7e045b..e450843 100644 --- a/src/value_builder.rs +++ b/src/value_builder.rs @@ -4,6 +4,6 @@ use crate::error::Result; pub trait ValueBuilder { type Output: Sized; - fn build_value(&self, question: &str) -> Result<Self::Output>; + fn build_value(&self, question: &str, default: Option<&Self::Output>) -> Result<Self::Output>; } diff --git a/tests/bool.rs b/tests/bool.rs index 17d23e8..d08b5b0 100644 --- a/tests/bool.rs +++ b/tests/bool.rs @@ -5,7 +5,7 @@ use interactive_object_builder::*; #[test] fn test_build_value() { - let v = bool::builder(DummyBackend).build_value("Are you sure?").unwrap(); + let v = bool::builder(DummyBackend).build_value("Are you sure?", Some(&false)).unwrap(); assert!(v); } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 0ab668a..7e111a5 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -75,7 +75,7 @@ pub struct BoolValBuilder; impl ValueBuilder for BoolValBuilder { type Output = bool; - fn build_value(&self, _question: &str) -> Result<Self::Output> { + fn build_value(&self, _question: &str, _default: Option<&Self::Output>) -> Result<Self::Output> { Ok(true) } } @@ -84,7 +84,7 @@ pub struct U8ValueBuilder; impl ValueBuilder for U8ValueBuilder { type Output = u8; - fn build_value(&self, _question: &str) -> Result<Self::Output> { + fn build_value(&self, _question: &str, _default: Option<&Self::Output>) -> Result<Self::Output> { Ok(42) } } @@ -97,7 +97,7 @@ impl<T: BuildableValue> CollectionBuilder for VecBuilder<T> { let mut buf = vec![]; loop { - let v: T = T::builder(DummyBackend).build_value(value_desc)?; + let v: T = T::builder(DummyBackend).build_value(value_desc, None)?; buf.push(v); if buf.len() == 3 { diff --git a/tests/struct.rs b/tests/struct.rs index 9845278..3a14750 100644 --- a/tests/struct.rs +++ b/tests/struct.rs @@ -23,7 +23,7 @@ impl<B: 'static + Backend> CollectionBuilder for ConfigStructBuilder<B> { fn build_collection(&self, value_desc: &str) -> Result<Self::Output> { println!("Building {}", value_desc); - let verbose = bool::builder(self.0).build_value(value_desc)?; + let verbose = bool::builder(self.0).build_value(value_desc, None)?; let values = Vec::<u8>::builder(self.0).build_collection(value_desc)?; Ok(Configuration { |