Changed long help for --unit, moved file length into Args to correct interaction with blocks as unit

main
nick 2024-07-21 20:22:17 -04:00
parent 0c157927b8
commit 0ca5d93248
1 changed files with 15 additions and 7 deletions

View File

@ -5,6 +5,7 @@ use rayon::ThreadPoolBuilder;
use crate::unit::Unit; use crate::unit::Unit;
use std::fs::{symlink_metadata, Metadata}; use std::fs::{symlink_metadata, Metadata};
use std::os::unix::fs::MetadataExt;
use std::path::{Component, Path}; use std::path::{Component, Path};
use std::slice::Iter; use std::slice::Iter;
use std::iter::once_with; use std::iter::once_with;
@ -31,7 +32,7 @@ pub struct Args {
#[arg( #[arg(
short, long, short, long,
help = "minimize output", help = "minimize output",
long_help = "print nothing but the total size for all directories, without a newline. Also supresses all error messages", long_help = "print nothing but the total size for all directories, without a newline or units. Also supresses all error messages",
conflicts_with = "total", conflicts_with = "total",
default_value_t = false, default_value_t = false,
)] )]
@ -72,7 +73,7 @@ pub struct Args {
#[arg( #[arg(
short, long, short, long,
help = "unit to print in", help = "unit to print in",
long_help = "printing unit (case insensitive): b = bytes, kb = kilobytes, ki = kibibytes, gb = gigabytes, gi = gibibytes, tb = terabytes, ti = tibibytes", long_help = "printing unit (case insensitive): b = bytes, kb = kilobytes, ki = kibibytes, gb = gigabytes, gi = gibibytes, tb = terabytes, ti = tibibytes, blk/blks/blck/blcks/block/blocks = file system blocks",
value_parser = Unit::parse, value_parser = Unit::parse,
default_value_t = Unit::Byte, default_value_t = Unit::Byte,
conflicts_with_all = ["base_two","si"], conflicts_with_all = ["base_two","si"],
@ -129,7 +130,7 @@ pub struct Args {
help = "items to summate", help = "items to summate",
action = ArgAction::Append, action = ArgAction::Append,
)] )]
path: Vec<String>, paths: Vec<String>,
} }
impl Args { impl Args {
/// utility method to chuck default values on the end. /// utility method to chuck default values on the end.
@ -146,8 +147,8 @@ impl Args {
self.exclude_print.clear(); self.exclude_print.clear();
} }
if self.path.is_empty() { if self.paths.is_empty() {
self.path = vec![ ".".to_owned() ]; self.paths = vec![ ".".to_owned() ];
} }
ThreadPoolBuilder::new() ThreadPoolBuilder::new()
@ -158,6 +159,13 @@ impl Args {
self self
} }
pub fn len(&self, meta: &Metadata) -> u64 {
match self.unit {
Unit::Blocks => meta.blocks(),
_ => meta.len(),
}
}
pub fn should_exclude(&self, path: &Path, file: &Metadata) -> bool { pub fn should_exclude(&self, path: &Path, file: &Metadata) -> bool {
if !self.follow_links if !self.follow_links
&& file.is_symlink() && file.is_symlink()
@ -202,7 +210,7 @@ impl Args {
} }
pub fn paths(&self) -> Iter<'_, String> { pub fn paths(&self) -> Iter<'_, String> {
self.path.iter() self.paths.iter()
} }
} }
@ -228,7 +236,7 @@ fn any_pattern_matches_any_component(patterns: &[Pattern], path: &Path) -> bool
continue continue
}; };
let Some(s) = cmp.to_str() else { let Some(s) = cmp.to_str() else {
// this is a code smell // TODO: this is a code smell
// I don't believe it, but I can't think // I don't believe it, but I can't think
// of anything worthwhile to do when // of anything worthwhile to do when
// you can't get a usable &str // you can't get a usable &str