1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

mv: use uucore::prompt_yes over custom function

This commit is contained in:
Terts Diepraam 2022-11-16 15:16:03 +01:00
parent 7bb0e8f849
commit 91df2b1709
2 changed files with 6 additions and 19 deletions

View file

@ -15,7 +15,7 @@ use clap::{crate_version, error::ErrorKind, Arg, ArgAction, ArgMatches, Command}
use std::env; use std::env;
use std::ffi::OsString; use std::ffi::OsString;
use std::fs; use std::fs;
use std::io::{self, stdin}; use std::io;
#[cfg(unix)] #[cfg(unix)]
use std::os::unix; use std::os::unix;
#[cfg(windows)] #[cfg(windows)]
@ -24,7 +24,7 @@ use std::path::{Path, PathBuf};
use uucore::backup_control::{self, BackupMode}; use uucore::backup_control::{self, BackupMode};
use uucore::display::Quotable; use uucore::display::Quotable;
use uucore::error::{FromIo, UError, UResult, USimpleError, UUsageError}; use uucore::error::{FromIo, UError, UResult, USimpleError, UUsageError};
use uucore::{format_usage, show, show_if_err}; use uucore::{format_usage, prompt_yes, show, show_if_err};
use fs_extra::dir::{move_dir, CopyOptions as DirCopyOptions}; use fs_extra::dir::{move_dir, CopyOptions as DirCopyOptions};
@ -282,8 +282,7 @@ fn exec(files: &[OsString], b: &Behavior) -> UResult<()> {
match b.overwrite { match b.overwrite {
OverwriteMode::NoClobber => return Ok(()), OverwriteMode::NoClobber => return Ok(()),
OverwriteMode::Interactive => { OverwriteMode::Interactive => {
println!("{}: overwrite {}? ", uucore::util_name(), target.quote()); if !prompt_yes!("overwrite {}? ", target.quote()) {
if !read_yes() {
return Ok(()); return Ok(());
} }
} }
@ -377,8 +376,7 @@ fn rename(from: &Path, to: &Path, b: &Behavior) -> io::Result<()> {
match b.overwrite { match b.overwrite {
OverwriteMode::NoClobber => return Ok(()), OverwriteMode::NoClobber => return Ok(()),
OverwriteMode::Interactive => { OverwriteMode::Interactive => {
println!("{}: overwrite {}? ", uucore::util_name(), to.quote()); if !prompt_yes!("overwrite {}?", to.quote()) {
if !read_yes() {
return Ok(()); return Ok(());
} }
} }
@ -494,17 +492,6 @@ fn rename_symlink_fallback(from: &Path, to: &Path) -> io::Result<()> {
Ok(()) Ok(())
} }
fn read_yes() -> bool {
let mut s = String::new();
match stdin().read_line(&mut s) {
Ok(_) => match s.chars().next() {
Some(x) => x == 'y' || x == 'Y',
_ => false,
},
_ => false,
}
}
fn is_empty_dir(path: &Path) -> bool { fn is_empty_dir(path: &Path) -> bool {
match fs::read_dir(path) { match fs::read_dir(path) {
Ok(contents) => contents.peekable().peek().is_none(), Ok(contents) => contents.peekable().peek().is_none(),

View file

@ -166,7 +166,7 @@ fn test_mv_interactive() {
.arg(file_b) .arg(file_b)
.pipe_in("n") .pipe_in("n")
.succeeds() .succeeds()
.no_stderr(); .no_stdout();
assert!(at.file_exists(file_a)); assert!(at.file_exists(file_a));
assert!(at.file_exists(file_b)); assert!(at.file_exists(file_b));
@ -178,7 +178,7 @@ fn test_mv_interactive() {
.arg(file_b) .arg(file_b)
.pipe_in("Yesh") // spell-checker:disable-line .pipe_in("Yesh") // spell-checker:disable-line
.succeeds() .succeeds()
.no_stderr(); .no_stdout();
assert!(!at.file_exists(file_a)); assert!(!at.file_exists(file_a));
assert!(at.file_exists(file_b)); assert!(at.file_exists(file_b));