Skip to content

Commit dc2ffde

Browse files
committed
Update true/false help msg to start with Usage:
1 parent b5bbabc commit dc2ffde

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

src/uu/false/src/false.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4747
pub fn uu_app() -> Command {
4848
Command::new(uucore::util_name())
4949
.version(uucore::crate_version!())
50-
.help_template(uucore::localized_help_template(uucore::util_name()))
50+
.override_usage(uucore::util_name().to_string())
51+
.help_template(uucore::true_false_help_template(uucore::util_name()))
5152
.about(translate!("false-about"))
5253
// We provide our own help and version options, to ensure maximum compatibility with GNU.
5354
.disable_help_flag(true)

src/uu/true/src/true.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4242
pub fn uu_app() -> Command {
4343
Command::new(uucore::util_name())
4444
.version(uucore::crate_version!())
45-
.help_template(uucore::localized_help_template(uucore::util_name()))
45+
.override_usage(uucore::util_name().to_string())
46+
.help_template(uucore::true_false_help_template(uucore::util_name()))
4647
.about(translate!("true-about"))
4748
// We provide our own help and version options, to ensure maximum compatibility with GNU.
4849
.disable_help_flag(true)

src/uucore/src/lib/lib.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,62 @@ pub fn localized_help_template_with_colors(
321321
template
322322
}
323323

324+
/// Create a localized help template for true/false commands which starts with Usage:
325+
/// This ensures the format matches GNU coreutils where Usage: appears first
326+
pub fn true_false_help_template(util_name: &str) -> clap::builder::StyledStr {
327+
use std::io::IsTerminal;
328+
329+
// Determine if colors should be enabled - same logic as configure_localized_command
330+
let colors_enabled = if std::env::var("NO_COLOR").is_ok() {
331+
false
332+
} else if std::env::var("CLICOLOR_FORCE").is_ok() || std::env::var("FORCE_COLOR").is_ok() {
333+
true
334+
} else {
335+
IsTerminal::is_terminal(&std::io::stdout())
336+
&& std::env::var("TERM").unwrap_or_default() != "dumb"
337+
};
338+
339+
true_false_help_template_with_colors(util_name, colors_enabled)
340+
}
341+
342+
/// Create a localized help template for true/false commands with explicit color control
343+
pub fn true_false_help_template_with_colors(
344+
util_name: &str,
345+
colors_enabled: bool,
346+
) -> clap::builder::StyledStr {
347+
use std::fmt::Write;
348+
349+
// Ensure localization is initialized for this utility
350+
let _ = crate::locale::setup_localization(util_name);
351+
352+
// Get the localized "Usage" label
353+
let usage_label = crate::locale::translate!("common-usage");
354+
355+
// Create a styled template
356+
let mut template = clap::builder::StyledStr::new();
357+
358+
// Add the basic template parts
359+
write!(template, "{{before-help}}").unwrap();
360+
361+
// Add styled usage header (bold + underline like clap's default)
362+
if colors_enabled {
363+
write!(
364+
template,
365+
"\x1b[1m\x1b[4m{usage_label}:\x1b[0m {{usage}}\n\n"
366+
)
367+
.unwrap();
368+
} else {
369+
write!(template, "{usage_label}: {{usage}}\n\n").unwrap();
370+
}
371+
372+
writeln!(template, "{{about-with-newline}}").unwrap();
373+
374+
// Add the rest
375+
write!(template, "{{all-args}}{{after-help}}").unwrap();
376+
377+
template
378+
}
379+
324380
/// Used to check if the utility is the second argument.
325381
/// Used to check if we were called as a multicall binary (`coreutils <utility>`)
326382
pub fn get_utility_is_second_arg() -> bool {

0 commit comments

Comments
 (0)