refactor: merge --si and --display-kb

add new option: --output-format this controls how the output is
summarised and takes:

 nothing = default behaviour
 si = SI units (same as old --si flag)
 b = bytes
 kb = kb
 kib = si kb
 mb = mb
 mib = si mb
....etc
This commit is contained in:
andy.boot
2024-02-21 22:57:33 +00:00
parent e9bacdf875
commit ebb3b8cceb
12 changed files with 120 additions and 140 deletions
+30 -25
View File
@@ -19,14 +19,13 @@ pub struct Config {
pub skip_total: Option<bool>,
pub screen_reader: Option<bool>,
pub ignore_hidden: Option<bool>,
pub iso: Option<bool>,
pub output_format: Option<String>,
pub min_size: Option<String>,
pub only_dir: Option<bool>,
pub only_file: Option<bool>,
pub disable_progress: Option<bool>,
pub depth: Option<usize>,
pub bars_on_right: Option<bool>,
pub display_kb: Option<bool>,
pub stack_size: Option<usize>,
}
@@ -57,8 +56,16 @@ impl Config {
pub fn get_no_bars(&self, options: &ArgMatches) -> bool {
Some(true) == self.no_bars || options.get_flag("no_bars")
}
pub fn get_iso(&self, options: &ArgMatches) -> bool {
Some(true) == self.iso || options.get_flag("iso")
pub fn get_output_format(&self, options: &ArgMatches) -> String {
let out_fmt = options.get_one::<String>("output_format");
(match out_fmt {
None => match &self.output_format {
None => "".to_string(),
Some(x) => x.to_string(),
},
Some(x) => x.into(),
})
.to_lowercase()
}
pub fn get_skip_total(&self, options: &ArgMatches) -> bool {
Some(true) == self.skip_total || options.get_flag("skip_total")
@@ -73,17 +80,17 @@ impl Config {
self.depth.unwrap_or(usize::MAX)
}
pub fn get_min_size(&self, options: &ArgMatches, iso: bool) -> Option<usize> {
pub fn get_min_size(&self, options: &ArgMatches, output_format: &str) -> Option<usize> {
let size_from_param = options.get_one::<String>("min_size");
self._get_min_size(size_from_param, iso)
self._get_min_size(size_from_param, output_format)
}
fn _get_min_size(&self, min_size: Option<&String>, iso: bool) -> Option<usize> {
let size_from_param = min_size.and_then(|a| convert_min_size(a, iso));
fn _get_min_size(&self, min_size: Option<&String>, output_format: &str) -> Option<usize> {
let size_from_param = min_size.and_then(|a| convert_min_size(a, output_format));
if size_from_param.is_none() {
self.min_size
.as_ref()
.and_then(|a| convert_min_size(a.as_ref(), iso))
.and_then(|a| convert_min_size(a.as_ref(), output_format))
} else {
size_from_param
}
@@ -105,12 +112,9 @@ impl Config {
from_cmd_line.copied()
}
}
pub fn get_display_kb(&self, options: &ArgMatches) -> bool {
Some(true) == self.display_kb || options.get_flag("display_kb")
}
}
fn convert_min_size(input: &str, iso: bool) -> Option<usize> {
fn convert_min_size(input: &str, output_format: &str) -> Option<usize> {
let chars_as_vec: Vec<char> = input.chars().collect();
match chars_as_vec.split_last() {
Some((last, start)) => {
@@ -120,7 +124,8 @@ fn convert_min_size(input: &str, iso: bool) -> Option<usize> {
if Some(*u) == last.to_uppercase().next() {
return match starts.parse::<usize>() {
Ok(pure) => {
let num: usize = if iso { 1000 } else { 1024 };
//fix
let num: usize = if output_format.eq("si") { 1000 } else { 1024 };
let marker = pure * num.pow((i + 1) as u32);
Some(marker)
}
@@ -173,13 +178,13 @@ mod tests {
#[test]
fn test_conversion() {
assert_eq!(convert_min_size("55", false), Some(55));
assert_eq!(convert_min_size("12344321", false), Some(12344321));
assert_eq!(convert_min_size("95RUBBISH", false), None);
assert_eq!(convert_min_size("10K", false), Some(10 * 1024));
assert_eq!(convert_min_size("10M", false), Some(10 * 1024usize.pow(2)));
assert_eq!(convert_min_size("10M", true), Some(10 * 1000usize.pow(2)));
assert_eq!(convert_min_size("2G", false), Some(2 * 1024usize.pow(3)));
assert_eq!(convert_min_size("55", ""), Some(55));
assert_eq!(convert_min_size("12344321", ""), Some(12344321));
assert_eq!(convert_min_size("95RUBBISH", ""), None);
assert_eq!(convert_min_size("10K", ""), Some(10 * 1024));
assert_eq!(convert_min_size("10M", ""), Some(10 * 1024usize.pow(2)));
assert_eq!(convert_min_size("10M", "si"), Some(10 * 1000usize.pow(2)));
assert_eq!(convert_min_size("2G", ""), Some(2 * 1024usize.pow(3)));
}
#[test]
@@ -188,11 +193,11 @@ mod tests {
min_size: Some("1K".to_owned()),
..Default::default()
};
assert_eq!(c._get_min_size(None, false), Some(1024));
assert_eq!(c._get_min_size(Some(&"2K".into()), false), Some(2048));
assert_eq!(c._get_min_size(None, ""), Some(1024));
assert_eq!(c._get_min_size(Some(&"2K".into()), ""), Some(2048));
assert_eq!(c._get_min_size(None, true), Some(1000));
assert_eq!(c._get_min_size(Some(&"2K".into()), true), Some(2000));
assert_eq!(c._get_min_size(None, "si"), Some(1000));
assert_eq!(c._get_min_size(Some(&"2K".into()), "si"), Some(2000));
}
#[test]