mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
cp: use uucore prompt_yes instead of custom
This commit is contained in:
parent
6cdcfca573
commit
ed34264b95
2 changed files with 4 additions and 22 deletions
|
@ -18,7 +18,7 @@ use std::env;
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::fs::{self, File, OpenOptions};
|
use std::fs::{self, File, OpenOptions};
|
||||||
use std::io::{self, stderr, stdin, Write};
|
use std::io;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use std::os::unix::ffi::OsStrExt;
|
use std::os::unix::ffi::OsStrExt;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
|
@ -39,7 +39,7 @@ use uucore::error::{set_exit_code, UClapError, UError, UResult, UUsageError};
|
||||||
use uucore::fs::{
|
use uucore::fs::{
|
||||||
canonicalize, paths_refer_to_same_file, FileInformation, MissingHandling, ResolveMode,
|
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;
|
mod copydir;
|
||||||
use crate::copydir::copy_directory;
|
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<T> = Result<T, Error>;
|
pub type CopyResult<T> = Result<T, Error>;
|
||||||
pub type Source = PathBuf;
|
pub type Source = PathBuf;
|
||||||
pub type SourceSlice = Path;
|
pub type SourceSlice = Path;
|
||||||
|
@ -1085,7 +1067,7 @@ impl OverwriteMode {
|
||||||
match *self {
|
match *self {
|
||||||
Self::NoClobber => Err(Error::NotAllFilesCopied),
|
Self::NoClobber => Err(Error::NotAllFilesCopied),
|
||||||
Self::Interactive(_) => {
|
Self::Interactive(_) => {
|
||||||
if prompt_yes!("{}: overwrite {}? ", uucore::util_name(), path.quote()) {
|
if prompt_yes!("overwrite {}?", path.quote()) {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(Error::Skipped)
|
Err(Error::Skipped)
|
||||||
|
|
|
@ -236,7 +236,7 @@ fn test_cp_arg_interactive() {
|
||||||
.pipe_in("N\n")
|
.pipe_in("N\n")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.no_stdout()
|
.no_stdout()
|
||||||
.stderr_is("cp: overwrite 'b'? [y/N]:");
|
.stderr_is("cp: overwrite 'b'? ");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue