From 3d2b24f4d9e0859dbf6ad2eec3f750372ede5767 Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Mon, 26 Feb 2024 18:32:55 +0000 Subject: [PATCH] Make human size units optional --- src/main.rs | 55 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8153443..3a95786 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,7 +38,7 @@ pub mod db; use compression::CompressionType; use compression::program::CompressionEngineProgram; -use humansize::{format_size, BINARY}; +use humansize::BINARY; use is_terminal::IsTerminal; @@ -139,6 +139,10 @@ struct OptionsArgs { #[arg(help("A comma separated list of columns to display with --list"))] list_format: String, + #[arg(short('h'), long)] + #[arg(help("Display file sizes with units"))] + human_readable: bool, + #[arg(short, long, action = clap::ArgAction::Count, conflicts_with("quiet"))] #[arg(help("Increase message verbosity, can be given more than once"))] verbose: u8, @@ -552,15 +556,15 @@ fn mode_list(cmd: &mut Command, args: Args, ids: &mut Vec, tags: &Vec Cell::new_align( - truncate_column(&item.id.unwrap_or(0).to_string(), column_width), + &string_column(item.id.unwrap_or(0).to_string(), column_width), Alignment::RIGHT), ColumnType::Time => Cell::new( - truncate_column(&item.ts.with_timezone(&Local).format("%F %T").to_string(), column_width)), + &string_column(item.ts.with_timezone(&Local).format("%F %T").to_string(), column_width)), ColumnType::Size => match item.size { Some(size) => Cell::new_align( - truncate_column(format_size(size as u64, BINARY).as_str(), column_width), + &size_column(size as u64, args.options.human_readable, column_width), Alignment::RIGHT), - None => match item_path.metadata() { + None => match item_path.metadata() { Ok(_) => Cell::new_align("Unknown", Alignment::RIGHT) .with_style(Attr::ForegroundColor(color::YELLOW)) .with_style(Attr::Bold), @@ -569,19 +573,19 @@ fn mode_list(cmd: &mut Command, args: Args, ids: &mut Vec, tags: &Vec Cell::new(truncate_column(&item.compression, column_width)), + ColumnType::Compression => Cell::new(&string_column(item.compression.to_string(), column_width)), ColumnType::FileSize => match item_path.metadata() { Ok(metadata) => Cell::new_align( - truncate_column(format_size(metadata.len(), BINARY).as_str(), column_width), + &size_column(metadata.len() as u64, args.options.human_readable, column_width), Alignment::RIGHT), Err(_) => Cell::new_align("Missing", Alignment::RIGHT) .with_style(Attr::ForegroundColor(color::RED)).with_style(Attr::Bold) }, - ColumnType::FilePath => Cell::new(truncate_column(&item_path.clone().into_os_string().into_string().unwrap(), column_width)), - ColumnType::Tags => Cell::new(truncate_column(tags.join(" ").as_str(), column_width)), + ColumnType::FilePath => Cell::new(&string_column(item_path.clone().into_os_string().into_string().unwrap(), column_width)), + ColumnType::Tags => Cell::new(&string_column(tags.join(" "), column_width)), ColumnType::Meta => match meta_name { Some(meta_name) => match meta.get(meta_name) { - Some(meta_value) => Cell::new(truncate_column(meta_value, column_width)), + Some(meta_value) => Cell::new(&string_column(meta_value.to_string(), column_width)), None => Cell::new("") }, None => Cell::new("") @@ -710,7 +714,7 @@ fn mode_info(cmd: &mut Command, args: Args, ids: &mut Vec, tags: &mut Vec Cell::new(format_size(size as u64, BINARY).as_str()), + Some(size) => Cell::new(format_size(size as u64, args.options.human_readable).as_str()), None => Cell::new("Missing").with_style(Attr::ForegroundColor(color::RED)).with_style(Attr::Bold) }; @@ -727,7 +731,7 @@ fn mode_info(cmd: &mut Command, args: Args, ids: &mut Vec, tags: &mut Vec Cell::new(format_size(metadata.len(), BINARY).as_str()), + Ok(metadata) => Cell::new(format_size(metadata.len(), args.options.human_readable).as_str()), Err(_) => Cell::new("Missing").with_style(Attr::ForegroundColor(color::RED)).with_style(Attr::Bold) }; @@ -883,13 +887,24 @@ fn get_meta_from_env() -> HashMap { meta_env } -fn truncate_column(s: &str, column_width: usize) -> &str { - if column_width > 0 { - match s.char_indices().nth(column_width) { - None => s, - Some((idx, _)) => &s[..idx], - } - } else { - s +fn format_size(size: u64, human_readable: bool) -> String { + match human_readable { + true => humansize::format_size(size, BINARY), + false => size.to_string() } } + +fn string_column(s: String, column_width: usize) -> String { + if column_width > 0 { + match s.char_indices().nth(column_width) { + None => s.to_string(), + Some((idx, _)) => s[..idx].to_string(), + } + } else { + s.to_string() + } +} + +fn size_column(size: u64, human_readable: bool, column_width: usize) -> String { + string_column(format_size(size, human_readable), column_width) +}