nu_command/filesystem/
util.rs1use dialoguer::Input;
2use std::error::Error;
3
4pub fn try_interaction(
5 interactive: bool,
6 prompt: String,
7) -> (Result<Option<bool>, Box<dyn Error>>, bool) {
8 let interaction = if interactive {
9 match get_interactive_confirmation(prompt) {
10 Ok(i) => Ok(Some(i)),
11 Err(e) => Err(e),
12 }
13 } else {
14 Ok(None)
15 };
16
17 let confirmed = match interaction {
18 Ok(maybe_input) => maybe_input.unwrap_or(false),
19 Err(_) => false,
20 };
21
22 (interaction, confirmed)
23}
24
25fn get_interactive_confirmation(prompt: String) -> Result<bool, Box<dyn Error>> {
26 let input = Input::new()
27 .with_prompt(prompt)
28 .validate_with(|c_input: &String| -> Result<(), String> {
29 if c_input.len() == 1
30 && (c_input == "y" || c_input == "Y" || c_input == "n" || c_input == "N")
31 {
32 Ok(())
33 } else if c_input.len() > 1 {
34 Err("Enter only one letter (Y/N)".to_string())
35 } else {
36 Err("Input not valid".to_string())
37 }
38 })
39 .default("Y/N".into())
40 .interact_text()?;
41
42 if input == "y" || input == "Y" {
43 Ok(true)
44 } else {
45 Ok(false)
46 }
47}