diff --git a/src/bin/coreutils.rs b/src/bin/coreutils.rs index 59634849a6b..383115baa82 100644 --- a/src/bin/coreutils.rs +++ b/src/bin/coreutils.rs @@ -17,31 +17,40 @@ const VERSION: &str = env!("CARGO_PKG_VERSION"); include!(concat!(env!("OUT_DIR"), "/uutils_map.rs")); fn usage(utils: &UtilityMap, name: &str) { - println!("{name} {VERSION} (multi-call binary)\n"); - println!("Usage: {name} [function [arguments...]]"); - println!(" {name} --list"); - println!(); + let display_list = utils.keys().copied().join(", "); + let width = cmp::min(textwrap::termwidth(), 100) - 8; // (opinion/heuristic) max 100 chars wide with 4 character side indentions + let indent_list = textwrap::indent(&textwrap::fill(&display_list, width), " "); #[cfg(feature = "feat_common_core")] + let common_core_string = " +Functions: + '' [arguments...] + +"; + #[cfg(not(feature = "feat_common_core"))] + let common_core_string = ""; + let s = format!( + "{name} {VERSION} (multi-call binary) + +Usage: {name} [function [arguments...]] + {name} --list + +{common_core_string}Options: + --list lists all defined functions, one per row + +Currently defined functions: + +{indent_list}" + ); + if let Err(e) = writeln!(io::stdout(), "{s}") + && e.kind() != io::ErrorKind::BrokenPipe { - println!("Functions:"); - println!(" '' [arguments...]"); - println!(); + let _ = writeln!(io::stderr(), "coreutils: {e}"); + process::exit(1); } - println!("Options:"); - println!(" --list lists all defined functions, one per row\n"); - println!("Currently defined functions:\n"); - let display_list = utils.keys().copied().join(", "); - let width = cmp::min(textwrap::termwidth(), 100) - 4 * 2; // (opinion/heuristic) max 100 chars wide with 4 character side indentions - println!( - "{}", - textwrap::indent(&textwrap::fill(&display_list, width), " ") - ); } #[allow(clippy::cognitive_complexity)] fn main() { - uucore::panic::mute_sigpipe_panic(); - let utils = util_map(); let mut args = uucore::args_os(); @@ -81,14 +90,24 @@ fn main() { usage(&utils, binary_as_util); process::exit(0); } - let utils: Vec<_> = utils.keys().collect(); - for util in utils { - println!("{util}"); + let mut out = io::stdout().lock(); + for util in utils.keys() { + if let Err(e) = writeln!(out, "{util}") + && e.kind() != io::ErrorKind::BrokenPipe + { + let _ = writeln!(io::stderr(), "coreutils: {e}"); + process::exit(1); + } } process::exit(0); } "--version" | "-V" => { - println!("{binary_as_util} {VERSION} (multi-call binary)"); + if let Err(e) = writeln!(io::stdout(), "coreutils {VERSION} (multi-call binary)") + && e.kind() != io::ErrorKind::BrokenPipe + { + let _ = writeln!(io::stderr(), "coreutils: {e}"); + process::exit(1); + } process::exit(0); } // Not a special command: fallthrough to calling a util