refactor: pretty-print bytes

This commit is contained in:
Vinícius R. Miguel 2021-04-06 02:49:03 -03:00
parent 368a776b70
commit 1b9faab96a
8 changed files with 22 additions and 18 deletions

View File

@ -2,7 +2,7 @@ use std::cmp;
const UNITS: [&str; 4] = ["B", "kB", "MB", "GB"]; const UNITS: [&str; 4] = ["B", "kB", "MB", "GB"];
struct Bytes { pub struct Bytes {
bytes: f64, bytes: f64,
} }

View File

@ -5,6 +5,7 @@ use colored::Colorize;
use super::{Compressor, Entry}; use super::{Compressor, Entry};
use crate::{ use crate::{
extension::CompressionFormat, extension::CompressionFormat,
bytes::Bytes,
file::File, file::File,
utils::{check_for_multiple_files, ensure_exists}, utils::{check_for_multiple_files, ensure_exists},
}; };
@ -22,10 +23,10 @@ impl BzipCompressor {
}; };
println!( println!(
"{}: compressed {:?} into memory ({} bytes)", "{}: compressed {:?} into memory ({})",
"info".yellow(), "info".yellow(),
&path, &path,
contents.len() Bytes::new(contents.len() as u64)
); );
Ok(contents) Ok(contents)

View File

@ -6,6 +6,7 @@ use super::{Compressor, Entry};
use crate::{ use crate::{
extension::CompressionFormat, extension::CompressionFormat,
file::File, file::File,
bytes::Bytes,
utils::{check_for_multiple_files, ensure_exists}, utils::{check_for_multiple_files, ensure_exists},
}; };
@ -27,10 +28,10 @@ impl GzipCompressor {
}; };
println!( println!(
"{}: compressed {:?} into memory ({} bytes)", "{}: compressed {:?} into memory ({})",
"info".yellow(), "info".yellow(),
&path, &path,
bytes.len() Bytes::new(bytes.len() as u64)
); );
Ok(bytes) Ok(bytes)

View File

@ -6,6 +6,7 @@ use super::{Compressor, Entry};
use crate::{ use crate::{
extension::CompressionFormat, extension::CompressionFormat,
file::File, file::File,
bytes::Bytes,
utils::{check_for_multiple_files, ensure_exists}, utils::{check_for_multiple_files, ensure_exists},
}; };
@ -27,10 +28,10 @@ impl LzmaCompressor {
}; };
println!( println!(
"{}: compressed {:?} into memory ({} bytes)", "{}: compressed {:?} into memory ({})",
"info".yellow(), "info".yellow(),
&path, &path,
bytes.len() Bytes::new(bytes.len() as u64)
); );
Ok(bytes) Ok(bytes)

View File

@ -8,7 +8,7 @@ use colored::Colorize;
use tar::{self, Archive}; use tar::{self, Archive};
use super::decompressor::{DecompressionResult, Decompressor}; use super::decompressor::{DecompressionResult, Decompressor};
use crate::{dialogs::Confirmation, file::File, utils}; use crate::{dialogs::Confirmation, file::File, bytes::Bytes, utils};
#[derive(Debug)] #[derive(Debug)]
pub struct TarDecompressor {} pub struct TarDecompressor {}
@ -45,10 +45,10 @@ impl TarDecompressor {
file.unpack_in(into)?; file.unpack_in(into)?;
println!( println!(
"{}: {:?} extracted. ({} bytes)", "{}: {:?} extracted. ({})",
"info".yellow(), "info".yellow(),
into.join(file.path()?), into.join(file.path()?),
file.size() Bytes::new(file.size())
); );
let file_path = fs::canonicalize(file_path)?; let file_path = fs::canonicalize(file_path)?;

View File

@ -7,7 +7,7 @@ use colored::Colorize;
use super::decompressor::{DecompressionResult, Decompressor}; use super::decompressor::{DecompressionResult, Decompressor};
use crate::utils; use crate::utils;
// use niffler; use crate::bytes::Bytes;
use crate::{extension::CompressionFormat, file::File}; use crate::{extension::CompressionFormat, file::File};
struct DecompressorToMemory {} struct DecompressorToMemory {}
@ -37,10 +37,10 @@ impl DecompressorToMemory {
let bytes_read = reader.read_to_end(&mut buffer)?; let bytes_read = reader.read_to_end(&mut buffer)?;
println!( println!(
"{}: {:?} extracted into memory ({} bytes).", "{}: {:?} extracted into memory ({}).",
"info".yellow(), "info".yellow(),
path, path,
bytes_read Bytes::new(bytes_read as u64)
); );
Ok(buffer) Ok(buffer)

View File

@ -8,7 +8,7 @@ use colored::Colorize;
use zip::{self, read::ZipFile, ZipArchive}; use zip::{self, read::ZipFile, ZipArchive};
use super::decompressor::{DecompressionResult, Decompressor}; use super::decompressor::{DecompressionResult, Decompressor};
use crate::{dialogs::Confirmation, file::File, utils}; use crate::{dialogs::Confirmation, file::File, bytes::Bytes, utils};
#[cfg(unix)] #[cfg(unix)]
fn __unix_set_permissions(file_path: &Path, file: &ZipFile) { fn __unix_set_permissions(file_path: &Path, file: &ZipFile) {
@ -73,10 +73,10 @@ impl ZipDecompressor {
} }
} }
println!( println!(
"{}: \"{}\" extracted. ({} bytes)", "{}: \"{}\" extracted. ({})",
"info".yellow(), "info".yellow(),
file_path.display(), file_path.display(),
file.size() Bytes::new(file.size())
); );
let mut output_file = fs::File::create(&file_path)?; let mut output_file = fs::File::create(&file_path)?;

View File

@ -7,6 +7,7 @@ use std::{
use colored::Colorize; use colored::Colorize;
use crate::{ use crate::{
bytes::Bytes,
cli::{VERSION, Command}, cli::{VERSION, Command},
compressors::{ compressors::{
Entry, Compressor, BzipCompressor, GzipCompressor, LzmaCompressor, TarCompressor, Entry, Compressor, BzipCompressor, GzipCompressor, LzmaCompressor, TarCompressor,
@ -199,10 +200,10 @@ impl Evaluator {
}; };
println!( println!(
"{}: writing to {:?}. ({} bytes)", "{}: writing to {:?}. ({})",
"info".yellow(), "info".yellow(),
output_path, output_path,
bytes.len() Bytes::new(bytes.len() as u64)
); );
fs::write(output_path, bytes)?; fs::write(output_path, bytes)?;