Skip to content

Commit

Permalink
feat(help): Add styling to help output
Browse files Browse the repository at this point in the history
Traditionally, cargo has disabled clap's styled help output.  My assumed
reason is that cargo mixes custom help output with auto-generated and
you couldn't previously make it all styled to match.  Clap 4.2 allowed
users to pass in strings styled using ANSI escape codes, allowing us to
pass in styled text that matches clap, unblocking this.  In clap
4.4.1, clap gained the ability for the user to override the style,
allowing us to choose the styling as we wish.

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.  That subdued style was
mostly chosen to be app theme neurtral (since we didn't have theming
support yet) and there were problems with our style and no one stepped
up to fix them (cargo has a style we can match to instead).

I decided to *not* style `Arg::help` messages because
- It might be distracting to have the descriptions lit up like a
  christmas tree
- It is a lot more work

The one exception I made was for `--list` since it is for a
psuedo-command (`...`) and I wanted to intentionally draw attention to
it.
  • Loading branch information
epage committed Aug 30, 2023
1 parent 99bf036 commit a460100
Show file tree
Hide file tree
Showing 38 changed files with 174 additions and 75 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ cargo-test-support = { path = "crates/cargo-test-support" }
cargo-util = { version = "0.2.6", path = "crates/cargo-util" }
cargo_metadata = "0.14.0"
clap = "4.4.1"
color-print = "0.3.4"
core-foundation = { version = "0.9.3", features = ["mac_os_10_7_support"] }
crates-io = { version = "0.39.0", path = "crates/crates-io" }
criterion = { version = "0.5.1", features = ["html_reports"] }
Expand Down Expand Up @@ -130,6 +131,7 @@ cargo-credential-libsecret.workspace = true
cargo-credential-macos-keychain.workspace = true
cargo-credential-wincred.workspace = true
cargo-util.workspace = true
color-print.workspace = true
clap = { workspace = true, features = ["wrap_help"] }
crates-io.workspace = true
curl = { workspace = true, features = ["http2"] }
Expand Down
69 changes: 40 additions & 29 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,53 +516,64 @@ pub fn cli() -> Command {
#[allow(clippy::disallowed_methods)]
let is_rustup = std::env::var_os("RUSTUP_HOME").is_some();
let usage = if is_rustup {
"cargo [+toolchain] [OPTIONS] [COMMAND]\n cargo [+toolchain] [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]..."
color_print::cstr!("<blue,bold>cargo</> <blue>[+toolchain] [OPTIONS] [COMMAND]</>\n <blue,bold>cargo</> <blue>[+toolchain] [OPTIONS]</> <blue,bold>-Zscript</> <blue><<MANIFEST_RS>> [ARGS]...</>")
} else {
"cargo [OPTIONS] [COMMAND]\n cargo [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]..."
color_print::cstr!("<blue,bold>cargo</> <blue>[OPTIONS] [COMMAND]</>\n <blue,bold>cargo</> <blue>[OPTIONS]</> <blue,bold>-Zscript</> <blue><<MANIFEST_RS>> [ARGS]...</>")
};

let styles = {
use clap::builder::styling::*;
Styles::styled()
.header(AnsiColor::Green.on_default() | Effects::BOLD)
.usage(AnsiColor::Green.on_default() | Effects::BOLD)
.literal(AnsiColor::Blue.on_default() | Effects::BOLD)
.placeholder(AnsiColor::Cyan.on_default())
.error(AnsiColor::Red.on_default())
.valid(AnsiColor::Blue.on_default() | Effects::BOLD)
.invalid(AnsiColor::Yellow.on_default())
};

Command::new("cargo")
// Subcommands all count their args' display order independently (from 0),
// which makes their args interspersed with global args. This puts global args last.
//
// We also want these to come before auto-generated `--help`
.next_display_order(800)
.allow_external_subcommands(true)
// Doesn't mix well with our list of common cargo commands. See clap-rs/clap#3108 for
// opening clap up to allow us to style our help template
.disable_colored_help(true)
.styles(styles)
// Provide a custom help subcommand for calling into man pages
.disable_help_subcommand(true)
.override_usage(usage)
.help_template(
.help_template(color_print::cstr!(
"\
Rust's package manager
Usage: {usage}
<green,bold>Usage:</> {usage}
Options:
<green,bold>Options:</>
{options}
Commands:
build, b Compile the current package
check, c Analyze the current package and report errors, but don't build object files
clean Remove the target directory
doc, d Build this package's and its dependencies' documentation
new Create a new cargo package
init Create a new cargo package in an existing directory
add Add dependencies to a manifest file
remove Remove dependencies from a manifest file
run, r Run a binary or example of the local package
test, t Run the tests
bench Run the benchmarks
update Update dependencies listed in Cargo.lock
search Search registry for crates
publish Package and upload this package to the registry
install Install a Rust binary. Default location is $HOME/.cargo/bin
uninstall Uninstall a Rust binary
... See all commands with --list
See 'cargo help <command>' for more information on a specific command.\n",
)
<green,bold>Commands:</>
<blue,bold>build</>, <blue,bold>b</> Compile the current package
<blue,bold>check</>, <blue,bold>c</> Analyze the current package and report errors, but don't build object files
<blue,bold>clean</> Remove the target directory
<blue,bold>doc</>, <blue,bold>d</> Build this package's and its dependencies' documentation
<blue,bold>new</> Create a new cargo package
<blue,bold>init</> Create a new cargo package in an existing directory
<blue,bold>add</> Add dependencies to a manifest file
<blue,bold>remove</> Remove dependencies from a manifest file
<blue,bold>run</>, <blue,bold>r</> Run a binary or example of the local package
<blue,bold>test</>, <blue,bold>t</> Run the tests
<blue,bold>bench</> Run the benchmarks
<blue,bold>update</> Update dependencies listed in Cargo.lock
<blue,bold>search</> Search registry for crates
<blue,bold>publish</> Package and upload this package to the registry
<blue,bold>install</> Install a Rust binary. Default location is $HOME/.cargo/bin
<blue,bold>uninstall</> Uninstall a Rust binary
<blue>...</> See all commands with <blue,bold>--list</>
See '<blue,bold>cargo help <<command>></>' for more information on a specific command.\n",
))
.arg(flag("version", "Print version info and exit").short('V'))
.arg(flag("list", "List installed commands"))
.arg(
Expand Down
12 changes: 6 additions & 6 deletions src/bin/cargo/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ pub fn cli() -> Command {
clap::Command::new("add")
.about("Add dependencies to a Cargo.toml manifest file")
.override_usage(
"\
cargo add [OPTIONS] <DEP>[@<VERSION>] ...
cargo add [OPTIONS] --path <PATH> ...
cargo add [OPTIONS] --git <URL> ..."
)
.after_help("Run `cargo help add` for more detailed information.\n")
color_print::cstr!("\
<blue,bold>cargo add</> <blue>[OPTIONS] <<DEP>>[@<<VERSION>>] ...</>
<blue,bold>cargo add</> <blue>[OPTIONS]</> <blue,bold>--path</> <blue><<PATH>> ...</>
<blue,bold>cargo add</> <blue>[OPTIONS]</> <blue,bold>--git</> <blue><<URL>> ...</>"
))
.after_help(color_print::cstr!("Run `<blue,bold>cargo help add</>` for more detailed information.\n"))
.group(clap::ArgGroup::new("selected").multiple(true).required(true))
.args([
clap::Arg::new("crates")
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ pub fn cli() -> Command {
.arg_unit_graph()
.arg_timings()
.arg_manifest_path()
.after_help("Run `cargo help bench` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help bench</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ pub fn cli() -> Command {
.arg_unit_graph()
.arg_timings()
.arg_manifest_path()
.after_help("Run `cargo help build` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help build</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ pub fn cli() -> Command {
.arg_unit_graph()
.arg_timings()
.arg_manifest_path()
.after_help("Run `cargo help check` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help check</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub fn cli() -> Command {
.arg_target_triple("Target triple to clean output for")
.arg_target_dir()
.arg_manifest_path()
.after_help("Run `cargo help clean` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help clean</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ pub fn cli() -> Command {
.arg_unit_graph()
.arg_timings()
.arg_manifest_path()
.after_help("Run `cargo help doc` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help doc</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ pub fn cli() -> Command {
.arg_quiet()
.arg_target_triple("Fetch dependencies for the target triple")
.arg_manifest_path()
.after_help("Run `cargo help fetch` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help fetch</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ pub fn cli() -> Command {
.arg_target_dir()
.arg_timings()
.arg_manifest_path()
.after_help("Run `cargo help fix` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help fix</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/generate_lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ pub fn cli() -> Command {
.about("Generate the lockfile for a package")
.arg_quiet()
.arg_manifest_path()
.after_help("Run `cargo help generate-lockfile` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help generate-lockfile</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ pub fn cli() -> Command {
.arg_new_opts()
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
.arg_quiet()
.after_help("Run `cargo help init` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help init</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ pub fn cli() -> Command {
.arg_target_triple("Build for the target triple")
.arg_target_dir()
.arg_timings()
.after_help("Run `cargo help install` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help install</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/locate_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ pub fn cli() -> Command {
)
.arg_quiet()
.arg_manifest_path()
.after_help("Run `cargo help locate-project` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help locate-project</>` for more detailed information.\n"
))
}

#[derive(Serialize)]
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub fn cli() -> Command {
.last(true),
)
.arg_quiet()
.after_help("Run `cargo help login` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help login</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/logout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ pub fn cli() -> Command {
.about("Remove an API token from the registry locally")
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
.arg_quiet()
.after_help("Run `cargo help logout` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help logout</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ pub fn cli() -> Command {
.arg_quiet()
.arg_features()
.arg_manifest_path()
.after_help("Run `cargo help metadata` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help metadata</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ pub fn cli() -> Command {
.arg_new_opts()
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
.arg_quiet()
.after_help("Run `cargo help new` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help new</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ pub fn cli() -> Command {
.arg(opt("token", "API token to use when authenticating").value_name("TOKEN"))
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
.arg_quiet()
.after_help("Run `cargo help owner` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help owner</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ pub fn cli() -> Command {
.arg_target_dir()
.arg_parallel()
.arg_manifest_path()
.after_help("Run `cargo help package` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help package</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/pkgid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ pub fn cli() -> Command {
.arg_quiet()
.arg_package("Argument to get the package ID specifier for")
.arg_manifest_path()
.after_help("Run `cargo help pkgid` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help pkgid</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ pub fn cli() -> Command {
.arg_target_triple("Build for the target triple")
.arg_target_dir()
.arg_manifest_path()
.after_help("Run `cargo help publish` for more detailed information.\n")
.after_help(color_print::cstr!(
"Run `<blue,bold>cargo help publish</>` for more detailed information.\n"
))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Expand Down
8 changes: 4 additions & 4 deletions src/bin/cargo/commands/read_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use crate::command_prelude::*;

pub fn cli() -> Command {
subcommand("read-manifest")
.about(
.about(color_print::cstr!(
"\
Print a JSON representation of a Cargo.toml manifest.
Deprecated, use `cargo metadata --no-deps` instead.\
",
)
Deprecated, use `<blue,bold>cargo metadata --no-deps</>` instead.\
"
))
.arg_quiet()
.arg_manifest_path()
}
Expand Down

0 comments on commit a460100

Please sign in to comment.