From ed34264b95bb175012c8144b2659da4908e130d9 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Wed, 16 Nov 2022 15:15:02 +0100 Subject: [PATCH] cp: use uucore prompt_yes instead of custom --- src/uu/cp/src/cp.rs | 24 +++--------------------- tests/by-util/test_cp.rs | 2 +- 2 files changed, 4 insertions(+), 22 deletions(-) diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 9618e39cb..0dddceab6 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -18,7 +18,7 @@ use std::env; #[cfg(not(windows))] use std::ffi::CString; use std::fs::{self, File, OpenOptions}; -use std::io::{self, stderr, stdin, Write}; +use std::io; #[cfg(unix)] use std::os::unix::ffi::OsStrExt; #[cfg(unix)] @@ -39,7 +39,7 @@ use uucore::error::{set_exit_code, UClapError, UError, UResult, UUsageError}; use uucore::fs::{ canonicalize, paths_refer_to_same_file, FileInformation, MissingHandling, ResolveMode, }; -use uucore::{crash, crash_if_err, format_usage, show_error, show_warning}; +use uucore::{crash, format_usage, prompt_yes, show_error, show_warning}; mod copydir; use crate::copydir::copy_directory; @@ -102,24 +102,6 @@ impl UError for Error { } } -/// Prompts the user yes/no and returns `true` if they successfully -/// answered yes. -macro_rules! prompt_yes( - ($($args:tt)+) => ({ - eprint!($($args)+); - eprint!(" [y/N]: "); - crash_if_err!(1, stderr().flush()); - let mut s = String::new(); - match stdin().read_line(&mut s) { - Ok(_) => match s.char_indices().next() { - Some((_, x)) => x == 'y' || x == 'Y', - _ => false - }, - _ => false - } - }) -); - pub type CopyResult = Result; pub type Source = PathBuf; pub type SourceSlice = Path; @@ -1085,7 +1067,7 @@ impl OverwriteMode { match *self { Self::NoClobber => Err(Error::NotAllFilesCopied), Self::Interactive(_) => { - if prompt_yes!("{}: overwrite {}? ", uucore::util_name(), path.quote()) { + if prompt_yes!("overwrite {}?", path.quote()) { Ok(()) } else { Err(Error::Skipped) diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index b1ef80fcc..c3afba730 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -236,7 +236,7 @@ fn test_cp_arg_interactive() { .pipe_in("N\n") .succeeds() .no_stdout() - .stderr_is("cp: overwrite 'b'? [y/N]:"); + .stderr_is("cp: overwrite 'b'? "); } #[test]