From e2dbfbd817932b0dcfbb419666875472943ee82f Mon Sep 17 00:00:00 2001 From: June Kim Date: Sun, 10 May 2026 08:30:41 -0700 Subject: [PATCH] Add --print-path flag to display path in output Addresses issue #41 by adding an optional --print-path flag that appends the path to the output, matching the desired behavior: diskus --print-path /tmp 57872384 /tmp Implementation details: - Uses tab separator for machine-parseable output - Rejects multiple paths with --print-path to avoid ambiguity - Preserves existing output format when flag is not used - Works correctly with paths containing spaces The flag is restricted to single-path usage. For multiple paths, users should call diskus separately for each path. --- src/main.rs | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index a514ab4..6bd00a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use num_format::{Locale, ToFormattedString}; use diskus::{CountType, Directories, DiskUsage, DiskUsageResult, Error}; -fn print_result(result: &DiskUsageResult, size_format: FormatSizeOptions, verbose: bool) { +fn print_result(result: &DiskUsageResult, size_format: FormatSizeOptions, verbose: bool, path_to_print: Option<&PathBuf>) { if verbose { for err in result.errors() { match err { @@ -32,14 +32,21 @@ fn print_result(result: &DiskUsageResult, size_format: FormatSizeOptions, verbos } let size = result.ignore_errors().size_in_bytes(); + let path_str = if let Some(path) = path_to_print { + format!("\t{}", path.display()) + } else { + String::new() + }; + if stdout().is_terminal() { println!( - "{} ({:} bytes)", + "{} ({:} bytes){}", format_size(size, size_format), - size.to_formatted_string(&Locale::en) + size.to_formatted_string(&Locale::en), + path_str ); } else { - println!("{}", size); + println!("{}{}", size, path_str); } } @@ -74,6 +81,12 @@ fn main() { .action(ArgAction::SetTrue) .help("Do not hide filesystem errors"), ) + .arg( + Arg::new("print-path") + .long("print-path") + .action(ArgAction::SetTrue) + .help("Print the path in the output"), + ) .arg( Arg::new("directories") .long("directories") @@ -119,6 +132,13 @@ fn main() { }; let verbose = matches.get_flag("verbose"); + let print_path = matches.get_flag("print-path"); + + // Reject --print-path with multiple paths + if print_path && paths.len() > 1 { + eprintln!("error: --print-path cannot be used with multiple paths"); + std::process::exit(1); + } let directories = match matches.get_one::("directories").map(|s| s.as_str()) { Some("included") => Directories::Included, @@ -126,6 +146,12 @@ fn main() { _ => Directories::Auto, }; + let path_to_print = if print_path { + paths.first().cloned() + } else { + None + }; + let mut disk_usage = DiskUsage::new(paths) .count_type(count_type) .directories(directories); @@ -133,5 +159,5 @@ fn main() { disk_usage = disk_usage.num_workers(n); } let result = disk_usage.count(); - print_result(&result, size_format, verbose); + print_result(&result, size_format, verbose, path_to_print.as_ref()); }