-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a8d0b4
commit 101e64d
Showing
7 changed files
with
217 additions
and
24 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use super::{ | ||
ByteDisplay, | ||
DuMetric, | ||
units::PrefixKind, | ||
}; | ||
use std::{ | ||
fmt::{self, Display}, | ||
fs::Metadata, | ||
}; | ||
|
||
pub struct Size { | ||
pub value: u64, | ||
pub prefix_kind: PrefixKind, | ||
pub human_readable: bool, | ||
} | ||
|
||
impl DuMetric for Size {} | ||
|
||
impl Size { | ||
pub fn new(metadata: &Metadata, prefix_kind: PrefixKind, human_readable: bool) -> Self { | ||
let value = metadata.len(); | ||
|
||
Self { | ||
value, | ||
prefix_kind, | ||
human_readable, | ||
} | ||
} | ||
} | ||
|
||
impl ByteDisplay for Size { | ||
fn human_readable(&self) -> bool { | ||
self.human_readable | ||
} | ||
|
||
fn prefix_kind(&self) -> PrefixKind { | ||
self.prefix_kind | ||
} | ||
|
||
fn value(&self) -> u64 { | ||
self.value | ||
} | ||
} | ||
|
||
impl Display for Size { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
<Self as ByteDisplay>::fmt(self, f) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_logical_size() -> std::io::Result<()> { | ||
use std::fs; | ||
|
||
let md = fs::metadata("./tests/data/nemesis.txt")?; | ||
|
||
let logical_size = Size::new(&md, PrefixKind::Bin, false); | ||
|
||
let display = format!("{logical_size}"); | ||
|
||
assert_eq!(logical_size.value, 161); | ||
assert_eq!(display, "161 B"); | ||
|
||
assert_eq!( | ||
format!( | ||
"{}", | ||
Size { | ||
value: 1_024, | ||
prefix_kind: PrefixKind::Bin, | ||
human_readable: true | ||
} | ||
), | ||
"1.00 KiB" | ||
); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,53 @@ | ||
use std::fmt; | ||
use units::{BinPrefix, PrefixKind, SiPrefix, UnitPrefix}; | ||
|
||
/// Binary and SI prefixes | ||
pub mod units; | ||
|
||
/// Rules to display disk usage for individual files | ||
/// Rules to display disk usage for individual files. | ||
pub mod file_size; | ||
|
||
pub mod block; | ||
|
||
pub mod logical; | ||
|
||
pub mod physical; | ||
|
||
pub trait DuMetric {} | ||
|
||
pub trait ByteDisplay { | ||
fn prefix_kind(&self) -> PrefixKind; | ||
|
||
fn value(&self) -> u64; | ||
|
||
fn human_readable(&self) -> bool; | ||
|
||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let value = self.value() as f64; | ||
|
||
match self.prefix_kind() { | ||
PrefixKind::Si => { | ||
let unit = SiPrefix::from(value); | ||
let base_value = unit.base_value(); | ||
|
||
if !self.human_readable() || matches!(unit, SiPrefix::Base) { | ||
write!(f, "{} {unit}", self.value()) | ||
} else { | ||
let size = value / (base_value as f64); | ||
write!(f, "{size:.2} {unit}") | ||
} | ||
} | ||
PrefixKind::Bin => { | ||
let unit = BinPrefix::from(value); | ||
let base_value = unit.base_value(); | ||
|
||
if !self.human_readable() || matches!(unit, BinPrefix::Base) { | ||
write!(f, "{} {unit}", self.value()) | ||
} else { | ||
let size = value / (base_value as f64); | ||
write!(f, "{size:.2} {unit}") | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use filesize::PathExt; | ||
use super::{ | ||
units::PrefixKind, | ||
ByteDisplay, | ||
DuMetric, | ||
}; | ||
use std::{ | ||
path::Path, | ||
fmt::{self, Display}, | ||
fs::Metadata, | ||
}; | ||
|
||
pub struct Size { | ||
pub value: u64, | ||
pub prefix_kind: PrefixKind, | ||
pub human_readable: bool, | ||
} | ||
|
||
impl DuMetric for Size {} | ||
|
||
impl Size { | ||
pub fn new(path: &Path, metadata: &Metadata, prefix_kind: PrefixKind, human_readable: bool) -> Self { | ||
let value = path.size_on_disk_fast(metadata).unwrap_or(0); | ||
|
||
Self { | ||
value, | ||
prefix_kind, | ||
human_readable, | ||
} | ||
} | ||
} | ||
|
||
impl ByteDisplay for Size { | ||
fn human_readable(&self) -> bool { | ||
self.human_readable | ||
} | ||
|
||
fn prefix_kind(&self) -> PrefixKind { | ||
self.prefix_kind | ||
} | ||
|
||
fn value(&self) -> u64 { | ||
self.value | ||
} | ||
} | ||
|
||
impl Display for Size { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
<Self as ByteDisplay>::fmt(self, f) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_physical_size() -> std::io::Result<()> { | ||
assert_eq!( | ||
format!( | ||
"{}", | ||
Size { | ||
value: 1_024, | ||
prefix_kind: PrefixKind::Bin, | ||
human_readable: true | ||
} | ||
), | ||
"1.00 KiB" | ||
); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters