diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 35 |
1 files changed, 32 insertions, 3 deletions
@@ -5,7 +5,7 @@ pub mod user_info; use comfy_table::Table; use humansize::{format_size, BINARY}; use parser::*; -use rustyline::error::ReadlineError; +use rustyline::{error::ReadlineError, DefaultEditor}; use std::{io, ops::ControlFlow}; use thiserror::Error; @@ -60,8 +60,15 @@ pub fn run_command(user_input: &str) -> Result<ControlFlow<(), ()>, ShackleError println!("Successfully created \"{}\"", init_result.path.display()); } Ok(ShackleCommand::Delete(DeleteArgs { directory })) => { - git::delete(&directory)?; - println!("Successfully deleted \"{}\"", directory.display()); + if confirm_risky_action(format!( + "Are you sure you want to delete \"{}\"?", + directory.display() + ))? { + git::delete(&directory)?; + println!("Successfully deleted \"{}\"", directory.display()); + } else { + println!("Action cancelled"); + } } Ok(ShackleCommand::Housekeeping(HousekeepingArgs { directory })) => match directory { Some(directory) => { @@ -92,6 +99,28 @@ pub fn run_command(user_input: &str) -> Result<ControlFlow<(), ()>, ShackleError Ok(ControlFlow::Continue(())) } +fn confirm_risky_action(prompt: String) -> Result<bool, ShackleError> { + let mut rl = DefaultEditor::new()?; + loop { + let user_input = rl + .readline(&format!("{} (yes/no) ", prompt)) + .map(|user_input| user_input.to_lowercase())?; + + match user_input.trim() { + "" => {} + "yes" => { + return Ok(true); + } + "no" => { + return Ok(false); + } + _ => { + println!("Please answer 'yes' or 'no'."); + } + } + } +} + #[derive(Error, Debug)] pub enum ShackleError { #[error(transparent)] |