Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Word, line, and block count #172

Merged
merged 8 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/context/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ pub struct ColumnProperties {
impl From<&Context> for ColumnProperties {
fn from(ctx: &Context) -> Self {
let unit_width = match ctx.unit {
PrefixKind::Si => 2,
PrefixKind::Bin => 3,
PrefixKind::Bin if ctx.human => 3,
PrefixKind::Si if ctx.human => 2,
_ => 1,
};

Self {
Expand Down
191 changes: 0 additions & 191 deletions src/disk_usage/file_size.rs

This file was deleted.

22 changes: 22 additions & 0 deletions src/disk_usage/file_size/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::{
fmt::{self, Display},
fs::Metadata,
os::unix::fs::MetadataExt,
};

#[derive(Default)]
pub struct Metric {
pub value: u64,
}

impl Metric {
pub fn init(md: &Metadata) -> Self {
Self { value: md.blocks() }
}
}

impl Display for Metric {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
<u64 as Display>::fmt(&self.value, f)
}
}
172 changes: 172 additions & 0 deletions src/disk_usage/file_size/byte.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
use super::super::units::{BinPrefix, PrefixKind, SiPrefix, UnitPrefix};
use filesize::PathExt;
use std::{
fmt::{self, Display},
fs::Metadata,
path::Path,
};

/// Concerned with measuring file size in bytes, whether logical or physical determined by `kind`.
/// Binary or SI units used for reporting determined by `prefix_kind`.
pub struct Metric {
pub value: u64,
pub human_readable: bool,
#[allow(dead_code)]
kind: MetricKind,
prefix_kind: PrefixKind,
}

/// Represents the appropriate method in which to compute bytes. `Logical` represent the total amount
/// of bytes in a file; `Physical` represents how many bytes are actually used to store the file on
/// disk.
pub enum MetricKind {
Logical,
Physical,
}

impl Metric {
/// Initializes a [Metric] that stores the total amount of bytes in a file.
pub fn init_logical(
metadata: &Metadata,
prefix_kind: PrefixKind,
human_readable: bool,
) -> Self {
let value = metadata.len();
let kind = MetricKind::Logical;

Self {
value,
human_readable,
kind,
prefix_kind,
}
}

/// Initializes an empty [Metric] used to represent the total amount of bytes of a file.
pub const fn init_empty_logical(human_readable: bool, prefix_kind: PrefixKind) -> Self {
Self {
value: 0,
human_readable,
kind: MetricKind::Logical,
prefix_kind,
}
}

/// Initializes an empty [Metric] used to represent the total disk space of a file in bytes.
pub const fn init_empty_physical(human_readable: bool, prefix_kind: PrefixKind) -> Self {
Self {
value: 0,
human_readable,
kind: MetricKind::Physical,
prefix_kind,
}
}

/// Initializes a [Metric] that stores the total amount of bytes used to store a file on disk.
pub fn init_physical(
path: &Path,
metadata: &Metadata,
prefix_kind: PrefixKind,
human_readable: bool,
) -> Self {
let value = path.size_on_disk_fast(metadata).unwrap_or(metadata.len());
let kind = MetricKind::Physical;

Self {
value,
human_readable,
kind,
prefix_kind,
}
}
}

impl Display for Metric {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let value = self.value as f64;

match self.prefix_kind {
PrefixKind::Si => {
if !self.human_readable {
return write!(f, "{} {}", self.value, SiPrefix::Base);
}

let unit = SiPrefix::from(self.value);

if matches!(unit, SiPrefix::Base) {
write!(f, "{} {unit}", self.value)
} else {
let base_value = unit.base_value();
let size = value / (base_value as f64);
write!(f, "{size:.2} {unit}")
}
}
PrefixKind::Bin => {
if !self.human_readable {
return write!(f, "{} {}", self.value, BinPrefix::Base);
}

let unit = BinPrefix::from(self.value);

if matches!(unit, BinPrefix::Base) {
write!(f, "{} {unit}", self.value)
} else {
let base_value = unit.base_value();
let size = value / (base_value as f64);
write!(f, "{size:.2} {unit}")
}
}
}
}
}

#[test]
fn test_metric() {
let metric = Metric {
value: 100,
kind: MetricKind::Logical,
human_readable: false,
prefix_kind: PrefixKind::Bin,
};
assert_eq!(format!("{}", metric), "100 B");

let metric = Metric {
value: 1000,
kind: MetricKind::Logical,
human_readable: true,
prefix_kind: PrefixKind::Si,
};
assert_eq!(format!("{}", metric), "1.00 KB");

let metric = Metric {
value: 1000,
kind: MetricKind::Logical,
human_readable: true,
prefix_kind: PrefixKind::Bin,
};
assert_eq!(format!("{}", metric), "1000 B");

let metric = Metric {
value: 1024,
kind: MetricKind::Logical,
human_readable: true,
prefix_kind: PrefixKind::Bin,
};
assert_eq!(format!("{}", metric), "1.00 KiB");

let metric = Metric {
value: 2_u64.pow(20),
kind: MetricKind::Logical,
human_readable: true,
prefix_kind: PrefixKind::Bin,
};
assert_eq!(format!("{}", metric), "1.00 MiB");

let metric = Metric {
value: 123454,
kind: MetricKind::Logical,
human_readable: false,
prefix_kind: PrefixKind::Bin,
};
assert_eq!(format!("{}", metric), "123454 B");
}
Loading