From f1ef76590eaa9c1cdc0ce663b47d965c704c13d4 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Wed, 9 Feb 2022 22:51:41 +0000 Subject: [PATCH] Ask user if they want to overwrite existing file --- src/lib.rs | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index faab318..030f14b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ #![cfg_attr(all(feature = "bench", test), feature(test))] use std::fs::File; -use std::io::{BufWriter, Error, ErrorKind, Write}; +use std::io::{BufWriter, Error, stdin, stdout, Write}; use std::path::Path; use std::str::from_utf8_unchecked; @@ -146,13 +146,19 @@ impl<'a> UwUify<'a> { // Handle Text Output if !self.outfile.is_empty() { if Path::new(&self.outfile).exists() { - return Err(Error::new( - ErrorKind::AlreadyExists, - format!( - "File '{}' already exists, aborting operation", - &self.outfile - ), - )); + let mut overwrite_file = String::new(); + print!( + "File '{}' already exists, would you like to overwrite this file? [Y/n] ", + self.outfile + ); + + stdout().flush()?; + stdin().read_line(&mut overwrite_file)?; + + if overwrite_file.to_lowercase().trim() != "y" { + println!("Exiting..."); + return Ok(()); + } } let uwu_progress_bar = progress_bar!(); @@ -167,13 +173,19 @@ impl<'a> UwUify<'a> { } else { // Handle File I/O if Path::new(&self.outfile).exists() { - return Err(Error::new( - ErrorKind::AlreadyExists, - format!( - "File '{}' already exists, aborting operation", - &self.outfile - ), - )); + let mut overwrite_file = String::new(); + print!( + "File '{}' already exists, would you like to overwrite this file? [Y/n] ", + self.outfile + ); + + stdout().flush()?; + stdin().read_line(&mut overwrite_file)?; + + if overwrite_file.to_lowercase().trim() != "y" { + println!("Exiting..."); + return Ok(()); + } } let uwu_progress_bar = progress_bar!();