| 61 | } |
| 62 | |
| 63 | pub fn choose(prompt: impl Display, options: &[impl ToString]) -> Result<Option<usize>> { |
| 64 | if options.is_empty() { |
| 65 | bail!("no options passed to choose") |
| 66 | } |
| 67 | |
| 68 | if !stdout().is_terminal() { |
| 69 | warn!("called choose while stdout is not a terminal"); |
| 70 | return Ok(Some(0)); |
| 71 | } |
| 72 | |
| 73 | match Select::with_theme(&dialoguer_theme()) |
| 74 | .items(options) |
| 75 | .default(0) |
| 76 | .with_prompt(prompt.to_string()) |
| 77 | .interact_opt() |
| 78 | { |
| 79 | Ok(ok) => Ok(ok), |
| 80 | Err(dialoguer::Error::IO(io)) if io.kind() == ErrorKind::Interrupted => Ok(None), |
| 81 | Err(e) => Err(e).wrap_err("Failed to choose"), |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | pub fn input(prompt: &str, initial_text: Option<&str>) -> Result<String> { |
| 86 | if !stdout().is_terminal() { |