Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 41 additions & 22 deletions src/bin/coreutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,40 @@ const VERSION: &str = env!("CARGO_PKG_VERSION");
include!(concat!(env!("OUT_DIR"), "/uutils_map.rs"));

fn usage<T>(utils: &UtilityMap<T>, 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:
'<uutils>' [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!(" '<uutils>' [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();

Expand Down Expand Up @@ -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
Expand Down
Loading