tweak: align file sizes and "extracted" to improve output

This commit is contained in:
Talison Fabio 2025-04-14 11:38:54 -03:00
parent 21e7fdf3d6
commit 343355f5f6
5 changed files with 38 additions and 30 deletions

View File

@ -7,7 +7,7 @@ use unrar::Archive;
use crate::{
error::{Error, Result},
list::FileInArchive,
utils::logger::info,
utils::{logger::info, Bytes},
};
/// Unpacks the archive given by `archive_path` into the folder given by `output_folder`.
@ -33,9 +33,9 @@ pub fn unpack_archive(
archive = if entry.is_file() {
if !quiet {
info(format!(
"{} extracted. ({})",
"extracted ({}) {}",
Bytes::new(entry.unpacked_size),
entry.filename.display(),
entry.unpacked_size
));
}
unpacked += 1;

View File

@ -127,9 +127,9 @@ where
} else {
if !quiet {
info(format!(
"{:?} extracted. ({})",
"extracted ({}) {:?}",
Bytes::new(entry.size()),
file_path.display(),
Bytes::new(entry.size())
));
}

View File

@ -39,9 +39,9 @@ pub fn unpack_archive(reader: Box<dyn Read>, output_folder: &Path, quiet: bool)
// and so on
if !quiet {
info(format!(
"{:?} extracted. ({})",
utils::strip_cur_dir(&output_folder.join(file.path()?)),
"extracted ({}) {:?}",
Bytes::new(file.size()),
utils::strip_cur_dir(&output_folder.join(file.path()?)),
));
files_unpacked += 1;

View File

@ -79,9 +79,9 @@ where
// same reason is in _is_dir: long, often not needed text
if !quiet {
info(format!(
"{:?} extracted. ({})",
"extracted ({}) {:?}",
Bytes::new(file.size()),
file_path.display(),
Bytes::new(file.size())
));
}

View File

@ -105,21 +105,29 @@ impl Bytes {
impl std::fmt::Display for Bytes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let &Self(num) = self;
let num = self.0;
debug_assert!(num >= 0.0);
if num < 1_f64 {
return write!(f, "{} B", num);
let num_pretty = format!("{:.2}", num);
return write!(
f,
"{:>6} B",
num_pretty
);
}
let delimiter = 1000_f64;
let exponent = cmp::min((num.ln() / 6.90775).floor() as i32, 4);
let num_pretty = format!("{:.2}", (num / delimiter.powi(exponent)));
let unit_pretty = format!("{}B", Bytes::UNIT_PREFIXES[exponent as usize]);
write!(
f,
"{:.2} {}B",
num / delimiter.powi(exponent),
Bytes::UNIT_PREFIXES[exponent as usize]
"{:>6} {:>3}",
num_pretty,
unit_pretty,
)
}
}
@ -138,7 +146,7 @@ mod tests {
let mb = kb * 1000;
let gb = mb * 1000;
assert_eq!("0 B", format_bytes(0)); // This is weird
assert_eq!(" 0.00 B", format_bytes(0)); // This is weird
assert_eq!(" 1.00 B", format_bytes(b));
assert_eq!("999.00 B", format_bytes(b * 999));
assert_eq!(" 12.00 MiB", format_bytes(mb * 12));