tweak: Align file sizes at left to make output clearer (#792)

This commit is contained in:
Talison Fabio 2025-04-18 12:24:02 -03:00 committed by João Marcos
parent ab5dd00b86
commit 4961a2c478
8 changed files with 67 additions and 30 deletions

View File

@ -48,6 +48,7 @@ Categories Used:
- Make `--format` more forgiving with the formatting of the provided format [\#519](https://github.com/ouch-org/ouch/pull/519) ([marcospb19](https://github.com/marcospb19)) - Make `--format` more forgiving with the formatting of the provided format [\#519](https://github.com/ouch-org/ouch/pull/519) ([marcospb19](https://github.com/marcospb19))
- Use buffered writer for list output [\#764](https://github.com/ouch-org/ouch/pull/764) ([killercup](https://github.com/killercup)) - Use buffered writer for list output [\#764](https://github.com/ouch-org/ouch/pull/764) ([killercup](https://github.com/killercup))
- Disable smart unpack when `--dir` flag is provided in decompress command [\#782](https://github.com/ouch-org/ouch/pull/782) ([talis-fb](https://github.com/talis-fb)) - Disable smart unpack when `--dir` flag is provided in decompress command [\#782](https://github.com/ouch-org/ouch/pull/782) ([talis-fb](https://github.com/talis-fb))
- Align file sizes at left for each extracted file to make output clearer [\#792](https://github.com/ouch-org/ouch/pull/792) ([talis-fb](https://github.com/talis-fb))
## [0.5.1](https://github.com/ouch-org/ouch/compare/0.5.0...0.5.1) ## [0.5.1](https://github.com/ouch-org/ouch/compare/0.5.0...0.5.1)

View File

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

View File

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

View File

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

View File

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

View File

@ -105,11 +105,11 @@ impl Bytes {
impl std::fmt::Display for Bytes { impl std::fmt::Display for Bytes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let &Self(num) = self; let num = self.0;
debug_assert!(num >= 0.0); debug_assert!(num >= 0.0);
if num < 1_f64 { if num < 1_f64 {
return write!(f, "{} B", num); return write!(f, "{:>6.2} B", num);
} }
let delimiter = 1000_f64; let delimiter = 1000_f64;
@ -117,9 +117,9 @@ impl std::fmt::Display for Bytes {
write!( write!(
f, f,
"{:.2} {}B", "{:>6.2} {:>2}B",
num / delimiter.powi(exponent), num / delimiter.powi(exponent),
Bytes::UNIT_PREFIXES[exponent as usize] Bytes::UNIT_PREFIXES[exponent as usize],
) )
} }
} }
@ -138,33 +138,33 @@ mod tests {
let mb = kb * 1000; let mb = kb * 1000;
let gb = mb * 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!(" 1.00 B", format_bytes(b));
assert_eq!("999.00 B", format_bytes(b * 999)); assert_eq!("999.00 B", format_bytes(b * 999));
assert_eq!("12.00 MiB", format_bytes(mb * 12)); assert_eq!(" 12.00 MiB", format_bytes(mb * 12));
assert_eq!("123.00 MiB", format_bytes(mb * 123)); assert_eq!("123.00 MiB", format_bytes(mb * 123));
assert_eq!("5.50 MiB", format_bytes(mb * 5 + kb * 500)); assert_eq!(" 5.50 MiB", format_bytes(mb * 5 + kb * 500));
assert_eq!("7.54 GiB", format_bytes(gb * 7 + 540 * mb)); assert_eq!(" 7.54 GiB", format_bytes(gb * 7 + 540 * mb));
assert_eq!("1.20 TiB", format_bytes(gb * 1200)); assert_eq!(" 1.20 TiB", format_bytes(gb * 1200));
// bytes // bytes
assert_eq!("234.00 B", format_bytes(234)); assert_eq!("234.00 B", format_bytes(234));
assert_eq!("999.00 B", format_bytes(999)); assert_eq!("999.00 B", format_bytes(999));
// kilobytes // kilobytes
assert_eq!("2.23 kiB", format_bytes(2234)); assert_eq!(" 2.23 kiB", format_bytes(2234));
assert_eq!("62.50 kiB", format_bytes(62500)); assert_eq!(" 62.50 kiB", format_bytes(62500));
assert_eq!("329.99 kiB", format_bytes(329990)); assert_eq!("329.99 kiB", format_bytes(329990));
// megabytes // megabytes
assert_eq!("2.75 MiB", format_bytes(2750000)); assert_eq!(" 2.75 MiB", format_bytes(2750000));
assert_eq!("55.00 MiB", format_bytes(55000000)); assert_eq!(" 55.00 MiB", format_bytes(55000000));
assert_eq!("987.65 MiB", format_bytes(987654321)); assert_eq!("987.65 MiB", format_bytes(987654321));
// gigabytes // gigabytes
assert_eq!("5.28 GiB", format_bytes(5280000000)); assert_eq!(" 5.28 GiB", format_bytes(5280000000));
assert_eq!("95.20 GiB", format_bytes(95200000000)); assert_eq!(" 95.20 GiB", format_bytes(95200000000));
assert_eq!("302.00 GiB", format_bytes(302000000000)); assert_eq!("302.00 GiB", format_bytes(302000000000));
assert_eq!("302.99 GiB", format_bytes(302990000000)); assert_eq!("302.99 GiB", format_bytes(302990000000));
// Weird aproximation cases: // Weird aproximation cases:
assert_eq!("999.90 GiB", format_bytes(999900000000)); assert_eq!("999.90 GiB", format_bytes(999900000000));
assert_eq!("1.00 TiB", format_bytes(999990000000)); assert_eq!(" 1.00 TiB", format_bytes(999990000000));
} }
} }

View File

@ -0,0 +1,13 @@
---
source: tests/ui.rs
expression: stdout_lines
---
{
"",
"[INFO] Files unpacked: 4",
"[INFO] Successfully decompressed archive in <TMP_DIR>/outputs",
"[INFO] extracted ( 0.00 B) \"outputs/inputs\"",
"[INFO] extracted ( 0.00 B) \"outputs/inputs/input\"",
"[INFO] extracted ( 0.00 B) \"outputs/inputs/input2\"",
"[INFO] extracted ( 0.00 B) \"outputs/inputs/input3\"",
}

View File

@ -6,7 +6,7 @@
#[macro_use] #[macro_use]
mod utils; mod utils;
use std::{ffi::OsStr, io, path::Path, process::Output}; use std::{collections::BTreeSet, ffi::OsStr, io, path::Path, process::Output};
use insta::assert_snapshot as ui; use insta::assert_snapshot as ui;
use regex::Regex; use regex::Regex;
@ -142,6 +142,29 @@ fn ui_test_ok_decompress() {
ui!(run_ouch("ouch decompress output.zst", dir)); ui!(run_ouch("ouch decompress output.zst", dir));
} }
#[cfg(target_os = "linux")]
#[test]
fn ui_test_ok_decompress_multiple_files() {
let (_dropper, dir) = testdir().unwrap();
let inputs_dir = dir.join("inputs");
std::fs::create_dir(&inputs_dir).unwrap();
let outputs_dir = dir.join("outputs");
std::fs::create_dir(&outputs_dir).unwrap();
// prepare
create_files_in(&inputs_dir, &["input", "input2", "input3"]);
let compress_command = format!("ouch compress {} output.tar.zst", inputs_dir.to_str().unwrap());
run_ouch(&compress_command, dir);
let decompress_command = format!("ouch decompress output.tar.zst --dir {}", outputs_dir.to_str().unwrap());
let stdout = run_ouch(&decompress_command, dir);
let stdout_lines = stdout.split('\n').collect::<BTreeSet<_>>();
insta::assert_debug_snapshot!(stdout_lines);
}
#[test] #[test]
fn ui_test_usage_help_flag() { fn ui_test_usage_help_flag() {
insta::with_settings!({filters => vec![ insta::with_settings!({filters => vec![