diff options
Diffstat (limited to 'src/cli_interactor.rs')
| -rw-r--r-- | src/cli_interactor.rs | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/cli_interactor.rs b/src/cli_interactor.rs index cf6e3d0..e52cefb 100644 --- a/src/cli_interactor.rs +++ b/src/cli_interactor.rs | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | use anyhow::Result; | 1 | use anyhow::{Context, Result}; |
| 2 | use dialoguer::{theme::ColorfulTheme, Confirm, Input, Password}; | 2 | use dialoguer::{theme::ColorfulTheme, Confirm, Input, Password}; |
| 3 | #[cfg(test)] | 3 | #[cfg(test)] |
| 4 | use mockall::*; | 4 | use mockall::*; |
| @@ -13,6 +13,7 @@ pub trait InteractorPrompt { | |||
| 13 | fn input(&self, parms: PromptInputParms) -> Result<String>; | 13 | fn input(&self, parms: PromptInputParms) -> Result<String>; |
| 14 | fn password(&self, parms: PromptPasswordParms) -> Result<String>; | 14 | fn password(&self, parms: PromptPasswordParms) -> Result<String>; |
| 15 | fn confirm(&self, params: PromptConfirmParms) -> Result<bool>; | 15 | fn confirm(&self, params: PromptConfirmParms) -> Result<bool>; |
| 16 | fn choice(&self, params: PromptChoiceParms) -> Result<usize>; | ||
| 16 | } | 17 | } |
| 17 | impl InteractorPrompt for Interactor { | 18 | impl InteractorPrompt for Interactor { |
| 18 | fn input(&self, parms: PromptInputParms) -> Result<String> { | 19 | fn input(&self, parms: PromptInputParms) -> Result<String> { |
| @@ -37,6 +38,12 @@ impl InteractorPrompt for Interactor { | |||
| 37 | .interact()?; | 38 | .interact()?; |
| 38 | Ok(confirm) | 39 | Ok(confirm) |
| 39 | } | 40 | } |
| 41 | fn choice(&self, parms: PromptChoiceParms) -> Result<usize> { | ||
| 42 | dialoguer::Select::with_theme(&self.theme) | ||
| 43 | .items(&parms.choices) | ||
| 44 | .interact() | ||
| 45 | .context("failed to get choice") | ||
| 46 | } | ||
| 40 | } | 47 | } |
| 41 | 48 | ||
| 42 | #[derive(Default)] | 49 | #[derive(Default)] |
| @@ -84,3 +91,20 @@ impl PromptConfirmParms { | |||
| 84 | self | 91 | self |
| 85 | } | 92 | } |
| 86 | } | 93 | } |
| 94 | |||
| 95 | #[derive(Default)] | ||
| 96 | pub struct PromptChoiceParms { | ||
| 97 | pub prompt: String, | ||
| 98 | pub choices: Vec<String>, | ||
| 99 | } | ||
| 100 | |||
| 101 | impl PromptChoiceParms { | ||
| 102 | pub fn with_prompt<S: Into<String>>(mut self, prompt: S) -> Self { | ||
| 103 | self.prompt = prompt.into(); | ||
| 104 | self | ||
| 105 | } | ||
| 106 | pub fn with_choices(mut self, choices: Vec<String>) -> Self { | ||
| 107 | self.choices = choices; | ||
| 108 | self | ||
| 109 | } | ||
| 110 | } | ||