Skip to content
Open
Show file tree
Hide file tree
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
62 changes: 60 additions & 2 deletions src/uu/false/src/false.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,68 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use clap::{Arg, ArgAction, Command};
use std::{ffi::OsString, io::Write};
use std::{
ffi::OsString,
io::{IsTerminal, Write},
};
use uucore::error::{UResult, set_exit_code};

use uucore::translate;

/// Create a localized help template for true/false commands which starts with Usage:
/// This ensures the format matches GNU coreutils where Usage: appears first
pub fn true_false_help_template(util_name: &str) -> clap::builder::StyledStr {
// Determine if colors should be enabled - same logic as configure_localized_command
let colors_enabled = if std::env::var("NO_COLOR").is_ok() {
false
} else if std::env::var("CLICOLOR_FORCE").is_ok() || std::env::var("FORCE_COLOR").is_ok() {
true
} else {
IsTerminal::is_terminal(&std::io::stdout())
&& std::env::var("TERM").unwrap_or_default() != "dumb"
};

true_false_help_template_with_colors(util_name, colors_enabled)
}

/// Create a localized help template for true/false commands with explicit color control
pub fn true_false_help_template_with_colors(
util_name: &str,
colors_enabled: bool,
) -> clap::builder::StyledStr {
use std::fmt::Write;

// Ensure localization is initialized for this utility
let _ = uucore::locale::setup_localization(util_name);

// Get the localized "Usage" label
let usage_label = uucore::locale::translate!("common-usage");

// Create a styled template
let mut template = clap::builder::StyledStr::new();

// Add the basic template parts
write!(template, "{{before-help}}").unwrap();

// Add styled usage header (bold + underline like clap's default)
if colors_enabled {
write!(
template,
"\x1b[1m\x1b[4m{usage_label}:\x1b[0m {{usage}}\n\n"
)
.unwrap();
} else {
write!(template, "{usage_label}: {{usage}}\n\n").unwrap();
}

writeln!(template, "{{about-with-newline}}").unwrap();

// Add the rest
write!(template, "{{all-args}}{{after-help}}").unwrap();

template
}

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let mut command = uu_app();
Expand Down Expand Up @@ -47,7 +104,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
pub fn uu_app() -> Command {
Command::new(uucore::util_name())
.version(uucore::crate_version!())
.help_template(uucore::localized_help_template(uucore::util_name()))
.override_usage(uucore::util_name().to_string())
.help_template(true_false_help_template(uucore::util_name()))
.about(translate!("false-about"))
// We provide our own help and version options, to ensure maximum compatibility with GNU.
.disable_help_flag(true)
Expand Down
62 changes: 60 additions & 2 deletions src/uu/true/src/true.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,68 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use clap::{Arg, ArgAction, Command};
use std::{ffi::OsString, io::Write};
use std::{
ffi::OsString,
io::{IsTerminal, Write},
};
use uucore::error::{UResult, set_exit_code};

use uucore::translate;

/// Create a localized help template for true/false commands which starts with Usage:
/// This ensures the format matches GNU coreutils where Usage: appears first
pub fn true_false_help_template(util_name: &str) -> clap::builder::StyledStr {
// Determine if colors should be enabled - same logic as configure_localized_command
let colors_enabled = if std::env::var("NO_COLOR").is_ok() {
false
} else if std::env::var("CLICOLOR_FORCE").is_ok() || std::env::var("FORCE_COLOR").is_ok() {
true
} else {
IsTerminal::is_terminal(&std::io::stdout())
&& std::env::var("TERM").unwrap_or_default() != "dumb"
};

true_false_help_template_with_colors(util_name, colors_enabled)
}

/// Create a localized help template for true/false commands with explicit color control
pub fn true_false_help_template_with_colors(
util_name: &str,
colors_enabled: bool,
) -> clap::builder::StyledStr {
use std::fmt::Write;

// Ensure localization is initialized for this utility
let _ = uucore::locale::setup_localization(util_name);

// Get the localized "Usage" label
let usage_label = uucore::locale::translate!("common-usage");

// Create a styled template
let mut template = clap::builder::StyledStr::new();

// Add the basic template parts
write!(template, "{{before-help}}").unwrap();

// Add styled usage header (bold + underline like clap's default)
if colors_enabled {
write!(
template,
"\x1b[1m\x1b[4m{usage_label}:\x1b[0m {{usage}}\n\n"
)
.unwrap();
} else {
write!(template, "{usage_label}: {{usage}}\n\n").unwrap();
}

writeln!(template, "{{about-with-newline}}").unwrap();

// Add the rest
write!(template, "{{all-args}}{{after-help}}").unwrap();

template
}

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let mut command = uu_app();
Expand Down Expand Up @@ -42,7 +99,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
pub fn uu_app() -> Command {
Command::new(uucore::util_name())
.version(uucore::crate_version!())
.help_template(uucore::localized_help_template(uucore::util_name()))
.override_usage(uucore::util_name().to_string())
.help_template(true_false_help_template(uucore::util_name()))
.about(translate!("true-about"))
// We provide our own help and version options, to ensure maximum compatibility with GNU.
.disable_help_flag(true)
Expand Down
Loading