From c9d4eea4036d534e8d0785cf3dea70713ef795ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Fri, 13 Aug 2021 02:14:28 -0300 Subject: [PATCH] Minor renaming --- src/archive/tar.rs | 3 +-- src/archive/zip.rs | 47 +++++++++++++++++++++++----------------------- src/commands.rs | 3 +-- src/lib.rs | 5 +---- src/utils.rs | 10 +++++++--- 5 files changed, 33 insertions(+), 35 deletions(-) diff --git a/src/archive/tar.rs b/src/archive/tar.rs index 5cd750e..6eb9941 100644 --- a/src/archive/tar.rs +++ b/src/archive/tar.rs @@ -22,8 +22,7 @@ pub fn unpack_archive( let mut file = file?; let file_path = output_folder.join(file.path()?); - if file_path.exists() && !utils::permission_for_overwriting(&file_path, flags)? { - // The user does not want to overwrite the file + if file_path.exists() && !utils::user_wants_to_overwrite(&file_path, flags)? { continue; } diff --git a/src/archive/zip.rs b/src/archive/zip.rs index 1349c46..e04d96a 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -12,28 +12,6 @@ use crate::{ utils::{self, colors}, }; -#[cfg(unix)] -fn __unix_set_permissions(file_path: &Path, file: &ZipFile) { - use std::os::unix::fs::PermissionsExt; - - if let Some(mode) = file.unix_mode() { - fs::set_permissions(&file_path, fs::Permissions::from_mode(mode)).unwrap(); - } -} - -fn check_for_comments(file: &ZipFile) { - let comment = file.comment(); - if !comment.is_empty() { - println!( - "{}[INFO]{} Comment in {}: {}", - colors::yellow(), - colors::reset(), - file.name(), - comment - ); - } -} - pub fn unpack_archive( mut archive: ZipArchive, into: &Path, @@ -51,8 +29,7 @@ where }; let file_path = into.join(file_path); - if file_path.exists() && !utils::permission_for_overwriting(&file_path, flags)? { - // The user does not want to overwrite the file + if file_path.exists() && !utils::user_wants_to_overwrite(&file_path, flags)? { continue; } @@ -142,3 +119,25 @@ where let bytes = writer.finish()?; Ok(bytes) } + +fn check_for_comments(file: &ZipFile) { + let comment = file.comment(); + if !comment.is_empty() { + println!( + "{}[INFO]{} Comment in {}: {}", + colors::yellow(), + colors::reset(), + file.name(), + comment + ); + } +} + +#[cfg(unix)] +fn __unix_set_permissions(file_path: &Path, file: &ZipFile) { + use std::os::unix::fs::PermissionsExt; + + if let Some(mode) = file.unix_mode() { + fs::set_permissions(&file_path, fs::Permissions::from_mode(mode)).unwrap(); + } +} diff --git a/src/commands.rs b/src/commands.rs index b4f1a18..0d4e9e4 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -89,8 +89,7 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> { .display_and_crash(); } - if output_path.exists() && !utils::permission_for_overwriting(&output_path, flags)? { - // The user does not want to overwrite the file + if output_path.exists() && !utils::user_wants_to_overwrite(&output_path, flags)? { return Ok(()); } diff --git a/src/lib.rs b/src/lib.rs index a5881f0..02d4a9e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,14 +10,11 @@ mod error; mod extension; mod utils; -use dialogs::Confirmation; pub use error::{Error, Result}; pub const EXIT_FAILURE: i32 = 127; const VERSION: &str = "0.1.5"; -const OVERWRITE_CONFIRMATION: Confirmation = - Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE")); fn help_command() { use utils::colors::*; @@ -76,5 +73,5 @@ Visit https://github.com/vrmiguel/ouch for more usage examples.", #[inline] fn version_command() { use utils::colors::*; - println!("{green}ouch{reset} {}", crate::VERSION, green = green(), reset = reset(),); + println!("{green}ouch{reset} {}", crate::VERSION, green = green(), reset = reset()); } diff --git a/src/utils.rs b/src/utils.rs index 717b64e..fd05c03 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,7 +5,7 @@ use std::{ path::{Path, PathBuf}, }; -use crate::{oof, OVERWRITE_CONFIRMATION}; +use crate::{dialogs::Confirmation, oof}; pub fn create_dir_if_non_existent(path: &Path) -> crate::Result<()> { if !path.exists() { @@ -42,7 +42,7 @@ pub fn cd_into_same_dir_as(filename: &Path) -> crate::Result { Ok(previous_location) } -pub fn permission_for_overwriting(path: &Path, flags: &oof::Flags) -> crate::Result { +pub fn user_wants_to_overwrite(path: &Path, flags: &oof::Flags) -> crate::Result { match (flags.is_present("yes"), flags.is_present("no")) { (true, true) => { unreachable!( @@ -55,7 +55,11 @@ pub fn permission_for_overwriting(path: &Path, flags: &oof::Flags) -> crate::Res } let file_path_str = to_utf(path); - OVERWRITE_CONFIRMATION.ask(Some(&file_path_str)) + + const OVERWRITE_CONFIRMATION_QUESTION: Confirmation = + Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE")); + + OVERWRITE_CONFIRMATION_QUESTION.ask(Some(&file_path_str)) } pub fn to_utf(os_str: impl AsRef) -> String {