diff options
Diffstat (limited to 'src/cli_interactor.rs')
| -rw-r--r-- | src/cli_interactor.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/cli_interactor.rs b/src/cli_interactor.rs index dc15c87..4cf6357 100644 --- a/src/cli_interactor.rs +++ b/src/cli_interactor.rs | |||
| @@ -14,6 +14,7 @@ pub trait InteractorPrompt { | |||
| 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 | fn choice(&self, params: PromptChoiceParms) -> Result<usize>; |
| 17 | fn multi_choice(&self, params: PromptMultiChoiceParms) -> Result<Vec<usize>>; | ||
| 17 | } | 18 | } |
| 18 | impl InteractorPrompt for Interactor { | 19 | impl InteractorPrompt for Interactor { |
| 19 | fn input(&self, parms: PromptInputParms) -> Result<String> { | 20 | fn input(&self, parms: PromptInputParms) -> Result<String> { |
| @@ -53,6 +54,18 @@ impl InteractorPrompt for Interactor { | |||
| 53 | } | 54 | } |
| 54 | choice.interact().context("failed to get choice") | 55 | choice.interact().context("failed to get choice") |
| 55 | } | 56 | } |
| 57 | fn multi_choice(&self, parms: PromptMultiChoiceParms) -> Result<Vec<usize>> { | ||
| 58 | // the colorful theme is not very clear so falling back to default | ||
| 59 | let mut choice = dialoguer::MultiSelect::default(); | ||
| 60 | choice | ||
| 61 | .with_prompt(parms.prompt) | ||
| 62 | .report(parms.report) | ||
| 63 | .items(&parms.choices); | ||
| 64 | if let Some(defaults) = parms.defaults { | ||
| 65 | choice.defaults(&defaults); | ||
| 66 | } | ||
| 67 | choice.interact().context("failed to get choice") | ||
| 68 | } | ||
| 56 | } | 69 | } |
| 57 | 70 | ||
| 58 | #[derive(Default)] | 71 | #[derive(Default)] |
| @@ -140,3 +153,34 @@ impl PromptChoiceParms { | |||
| 140 | self | 153 | self |
| 141 | } | 154 | } |
| 142 | } | 155 | } |
| 156 | |||
| 157 | #[derive(Default)] | ||
| 158 | pub struct PromptMultiChoiceParms { | ||
| 159 | pub prompt: String, | ||
| 160 | pub choices: Vec<String>, | ||
| 161 | pub defaults: Option<Vec<bool>>, | ||
| 162 | pub report: bool, | ||
| 163 | } | ||
| 164 | |||
| 165 | impl PromptMultiChoiceParms { | ||
| 166 | pub fn with_prompt<S: Into<String>>(mut self, prompt: S) -> Self { | ||
| 167 | self.prompt = prompt.into(); | ||
| 168 | self.report = true; | ||
| 169 | self | ||
| 170 | } | ||
| 171 | |||
| 172 | pub fn dont_report(mut self) -> Self { | ||
| 173 | self.report = false; | ||
| 174 | self | ||
| 175 | } | ||
| 176 | |||
| 177 | pub fn with_choices(mut self, choices: Vec<String>) -> Self { | ||
| 178 | self.choices = choices; | ||
| 179 | self | ||
| 180 | } | ||
| 181 | |||
| 182 | pub fn with_defaults(mut self, defaults: Vec<bool>) -> Self { | ||
| 183 | self.defaults = Some(defaults); | ||
| 184 | self | ||
| 185 | } | ||
| 186 | } | ||