mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +00:00
Merge pull request #17 from vrmiguel/byte-formatting
Add pretty-printing for bytes
This commit is contained in:
commit
e080de860a
30
src/bytes.rs
Normal file
30
src/bytes.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
use std::cmp;
|
||||||
|
|
||||||
|
const UNITS: [&str; 4] = ["B", "kB", "MB", "GB"];
|
||||||
|
|
||||||
|
pub struct Bytes {
|
||||||
|
bytes: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Bytes {
|
||||||
|
pub fn new(bytes: u64) -> Self {
|
||||||
|
Self {
|
||||||
|
bytes: bytes as f64,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for Bytes {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let num = self.bytes;
|
||||||
|
debug_assert!(num >= 0.0);
|
||||||
|
if num < 1_f64 {
|
||||||
|
return write!(f, "{} B", num);
|
||||||
|
}
|
||||||
|
let delimiter = 1000_f64;
|
||||||
|
let exponent = cmp::min((num.ln() / 6.90775).floor() as i32, 4);
|
||||||
|
|
||||||
|
write!(f, "{:.2} ", num / delimiter.powi(exponent))?;
|
||||||
|
write!(f, "{}", UNITS[exponent as usize])
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
||||||
|
@ -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)
|
||||||
|
@ -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)
|
||||||
|
@ -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)?;
|
||||||
|
@ -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)
|
||||||
|
@ -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)?;
|
||||||
|
@ -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)?;
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ use std::{
|
|||||||
|
|
||||||
use CompressionFormat::*;
|
use CompressionFormat::*;
|
||||||
|
|
||||||
use crate::{debug, utils::to_utf};
|
use crate::utils::to_utf;
|
||||||
|
|
||||||
/// Represents the extension of a file, but only really caring about
|
/// Represents the extension of a file, but only really caring about
|
||||||
/// compression formats (and .tar).
|
/// compression formats (and .tar).
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
mod bytes;
|
||||||
mod cli;
|
mod cli;
|
||||||
mod compressors;
|
mod compressors;
|
||||||
mod decompressors;
|
mod decompressors;
|
||||||
|
73
src/test.rs
73
src/test.rs
@ -26,7 +26,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod argparsing {
|
||||||
use super::{make_dummy_files};
|
use super::{make_dummy_files};
|
||||||
use crate::cli;
|
use crate::cli;
|
||||||
use crate::cli::Command;
|
use crate::cli::Command;
|
||||||
@ -103,6 +103,77 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod byte_pretty_printing {
|
||||||
|
use crate::bytes::Bytes;
|
||||||
|
#[test]
|
||||||
|
fn bytes () {
|
||||||
|
assert_eq!(
|
||||||
|
&format!("{}", Bytes::new(234)),
|
||||||
|
"234.00 B"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
&format!("{}", Bytes::new(999)),
|
||||||
|
"999.00 B"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn kilobytes () {
|
||||||
|
assert_eq!(
|
||||||
|
&format!("{}", Bytes::new(2234)),
|
||||||
|
"2.23 kB"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
&format!("{}", Bytes::new(62500)),
|
||||||
|
"62.50 kB"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
&format!("{}", Bytes::new(329990)),
|
||||||
|
"329.99 kB"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn megabytes () {
|
||||||
|
assert_eq!(
|
||||||
|
&format!("{}", Bytes::new(2750000)),
|
||||||
|
"2.75 MB"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
&format!("{}", Bytes::new(55000000)),
|
||||||
|
"55.00 MB"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
&format!("{}", Bytes::new(987654321)),
|
||||||
|
"987.65 MB"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn gigabytes () {
|
||||||
|
assert_eq!(
|
||||||
|
&format!("{}", Bytes::new(5280000000)),
|
||||||
|
"5.28 GB"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
&format!("{}", Bytes::new(95200000000)),
|
||||||
|
"95.20 GB"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
&format!("{}", Bytes::new(302000000000)),
|
||||||
|
"302.00 GB"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// #[cfg(test)]
|
// #[cfg(test)]
|
||||||
// mod cli {
|
// mod cli {
|
||||||
// use super::*;
|
// use super::*;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user