Skip to content

Commit 0fb78ce

Browse files
committed
feat(help): Add styling to help output
Traditionally, cargo has disabled clap's styled output. My assumed reason is that cargo mixes custom help output with auto-generated and you couldn't previously make it all styled. Clap 4.2 allowed users to pass in strings styled using ANSI escape codes, allowing us to pass in styled text that matches clap. In clap 4.4.1, clap gained the ability for the user to override the style. In this PR, I decided to use the new 4.4.1 feature to style clap's output to match the rest of cargo's output. Alternatively, we could use a more subdue style that clap uses by default.
1 parent 07acbac commit 0fb78ce

File tree

4 files changed

+64
-27
lines changed

4 files changed

+64
-27
lines changed

Cargo.lock

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ cargo-test-support = { path = "crates/cargo-test-support" }
3030
cargo-util = { version = "0.2.6", path = "crates/cargo-util" }
3131
cargo_metadata = "0.14.0"
3232
clap = "4.4.1"
33+
color-print = "0.3.4"
3334
core-foundation = { version = "0.9.3", features = ["mac_os_10_7_support"] }
3435
crates-io = { version = "0.39.0", path = "crates/crates-io" }
3536
criterion = { version = "0.5.1", features = ["html_reports"] }
@@ -130,6 +131,7 @@ cargo-credential-libsecret.workspace = true
130131
cargo-credential-macos-keychain.workspace = true
131132
cargo-credential-wincred.workspace = true
132133
cargo-util.workspace = true
134+
color-print.workspace = true
133135
clap = { workspace = true, features = ["wrap_help"] }
134136
crates-io.workspace = true
135137
curl = { workspace = true, features = ["http2"] }

src/bin/cargo/cli.rs

+37-26
Original file line numberDiff line numberDiff line change
@@ -520,48 +520,59 @@ pub fn cli() -> Command {
520520
} else {
521521
"cargo [OPTIONS] [COMMAND]\n cargo [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]..."
522522
};
523+
524+
let styles = {
525+
use clap::builder::styling::*;
526+
Styles::styled()
527+
.header(AnsiColor::Green.on_default() | Effects::BOLD)
528+
.usage(AnsiColor::Green.on_default() | Effects::BOLD)
529+
.literal(AnsiColor::Blue.on_default() | Effects::BOLD)
530+
.placeholder(AnsiColor::Cyan.on_default())
531+
.error(AnsiColor::Red.on_default())
532+
.valid(AnsiColor::Blue.on_default() | Effects::BOLD)
533+
.invalid(AnsiColor::Yellow.on_default())
534+
};
535+
523536
Command::new("cargo")
524537
// Subcommands all count their args' display order independently (from 0),
525538
// which makes their args interspersed with global args. This puts global args last.
526539
//
527540
// We also want these to come before auto-generated `--help`
528541
.next_display_order(800)
529542
.allow_external_subcommands(true)
530-
// Doesn't mix well with our list of common cargo commands. See clap-rs/clap#3108 for
531-
// opening clap up to allow us to style our help template
532-
.disable_colored_help(true)
543+
.styles(styles)
533544
// Provide a custom help subcommand for calling into man pages
534545
.disable_help_subcommand(true)
535546
.override_usage(usage)
536-
.help_template(
547+
.help_template(color_print::cstr!(
537548
"\
538549
Rust's package manager
539550
540-
Usage: {usage}
551+
<green,bold>Usage:</> <blue,bold>{usage}</>
541552
542-
Options:
553+
<green,bold>Options:</>
543554
{options}
544555
545-
Some common cargo commands are (see all commands with --list):
546-
build, b Compile the current package
547-
check, c Analyze the current package and report errors, but don't build object files
548-
clean Remove the target directory
549-
doc, d Build this package's and its dependencies' documentation
550-
new Create a new cargo package
551-
init Create a new cargo package in an existing directory
552-
add Add dependencies to a manifest file
553-
remove Remove dependencies from a manifest file
554-
run, r Run a binary or example of the local package
555-
test, t Run the tests
556-
bench Run the benchmarks
557-
update Update dependencies listed in Cargo.lock
558-
search Search registry for crates
559-
publish Package and upload this package to the registry
560-
install Install a Rust binary. Default location is $HOME/.cargo/bin
561-
uninstall Uninstall a Rust binary
562-
563-
See 'cargo help <command>' for more information on a specific command.\n",
564-
)
556+
Some common cargo commands are (see all commands with '<blue,bold>--list</>'):
557+
<blue,bold>build</>, <blue,bold>b</> Compile the current package
558+
<blue,bold>check</>, <blue,bold>c</> Analyze the current package and report errors, but don't build object files
559+
<blue,bold>clean</> Remove the target directory
560+
<blue,bold>doc</>, <blue,bold>d</> Build this package's and its dependencies' documentation
561+
<blue,bold>new</> Create a new cargo package
562+
<blue,bold>init</> Create a new cargo package in an existing directory
563+
<blue,bold>add</> Add dependencies to a manifest file
564+
<blue,bold>remove</> Remove dependencies from a manifest file
565+
<blue,bold>run</>, <blue,bold>r</> Run a binary or example of the local package
566+
<blue,bold>test</>, <blue,bold>t</> Run the tests
567+
<blue,bold>bench</> Run the benchmarks
568+
<blue,bold>update</> Update dependencies listed in Cargo.lock
569+
<blue,bold>search</> Search registry for crates
570+
<blue,bold>publish</> Package and upload this package to the registry
571+
<blue,bold>install</> Install a Rust binary. Default location is $HOME/.cargo/bin
572+
<blue,bold>uninstall</> Uninstall a Rust binary
573+
574+
See '<blue,bold>cargo help <<command>></>' for more information on a specific command.\n",
575+
))
565576
.arg(flag("version", "Print version info and exit").short('V'))
566577
.arg(flag("list", "List installed commands"))
567578
.arg(opt("explain", "Run `rustc --explain CODE`").value_name("CODE"))

src/bin/cargo/commands/run.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ pub fn cli() -> Command {
3838
.arg_manifest_path()
3939
.arg_unit_graph()
4040
.arg_timings()
41-
.after_help("Run `cargo help run` for more detailed information.\n")
41+
.after_help(color_print::cstr!(
42+
"Run `<blue,bold>cargo help run</>` for more detailed information.\n"
43+
))
4244
}
4345

4446
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {

0 commit comments

Comments
 (0)