mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +00:00
Add Bytes and impl Display for Bytes
This commit is contained in:
parent
d3de94dfca
commit
368a776b70
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"];
|
||||||
|
|
||||||
|
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])
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user