Minor import and module exporting changes

This commit is contained in:
João M. Bezerra 2021-11-10 06:03:46 -03:00
parent 2d2a018cbe
commit 552096acf0
3 changed files with 12 additions and 13 deletions

View File

@ -22,7 +22,7 @@ use crate::{
info,
list::{self, ListOptions},
utils::{
self, concatenate_list_of_os_str, dir_is_empty, nice_directory_display, to_utf, try_infer,
self, concatenate_list_of_os_str, dir_is_empty, nice_directory_display, to_utf, try_infer_extension,
user_wants_to_continue_decompressing,
},
warning, Opts, QuestionPolicy, Subcommand,
@ -495,7 +495,7 @@ fn check_mime_type(
if format.is_empty() {
// File with no extension
// Try to detect it automatically and prompt the user about it
if let Some(detected_format) = try_infer(path) {
if let Some(detected_format) = try_infer_extension(path) {
info!("Detected file: `{}` extension as `{}`", path.display(), detected_format);
if user_wants_to_continue_decompressing(path, question_policy)? {
format.push(detected_format);
@ -503,7 +503,7 @@ fn check_mime_type(
return Ok(ControlFlow::Break(()));
}
}
} else if let Some(detected_format) = try_infer(path) {
} else if let Some(detected_format) = try_infer_extension(path) {
// File ending with extension
// Try to detect the extension and warn the user if it differs from the written one
let outer_ext = format.iter().next().unwrap();

View File

@ -4,6 +4,8 @@ use std::{
borrow::Cow,
env,
ffi::OsStr,
fs::ReadDir,
io::Read,
path::{Component, Path, PathBuf},
};
@ -13,7 +15,7 @@ use crate::{extension::Extension, info};
/// Checks given path points to an empty directory.
pub fn dir_is_empty(dir_path: &Path) -> bool {
let is_empty = |mut rd: std::fs::ReadDir| rd.next().is_none();
let is_empty = |mut rd: ReadDir| rd.next().is_none();
dir_path.read_dir().map(is_empty).unwrap_or_default()
}
@ -82,7 +84,7 @@ pub fn nice_directory_display(os_str: impl AsRef<OsStr>) -> Cow<'static, str> {
/// Try to detect the file extension by looking for known magic strings
/// Source: https://en.wikipedia.org/wiki/List_of_file_signatures
pub fn try_infer(path: &Path) -> Option<Extension> {
pub fn try_infer_extension(path: &Path) -> Option<Extension> {
fn is_zip(buf: &[u8]) -> bool {
buf.len() > 3
&& buf[0] == 0x50
@ -155,10 +157,12 @@ pub fn try_infer(path: &Path) -> Option<Extension> {
/// Module with a list of bright colors.
#[allow(dead_code)]
pub mod colors {
use std::env;
use once_cell::sync::Lazy;
static DISABLE_COLORED_TEXT: Lazy<bool> = Lazy::new(|| {
std::env::var_os("NO_COLOR").is_some() || atty::isnt(atty::Stream::Stdout) || atty::isnt(atty::Stream::Stderr)
env::var_os("NO_COLOR").is_some() || atty::isnt(atty::Stream::Stdout) || atty::isnt(atty::Stream::Stderr)
});
macro_rules! color {

View File

@ -5,10 +5,5 @@ mod fs;
mod question_policy;
pub use bytes::Bytes;
pub use fs::{
cd_into_same_dir_as, colors, concatenate_list_of_os_str, create_dir_if_non_existent, dir_is_empty,
nice_directory_display, strip_cur_dir, to_utf, try_infer,
};
pub use question_policy::{
create_or_ask_overwrite, user_wants_to_continue_decompressing, user_wants_to_overwrite, QuestionPolicy,
};
pub use fs::*;
pub use question_policy::*;