From cc05dc142d1c57afd10cb08d84f99a25bd49678c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 23 Jan 2024 19:56:50 +0100 Subject: [PATCH] chore: make deno --help more colorful (#21962) This commit adds coloring to "--help" flag. Before: https://github.com/denoland/deno/assets/13602871/23753f95-3289-45bc-8a1e-842d0ccd92df After: https://github.com/denoland/deno/assets/13602871/91b035cc-f199-4cf4-a77c-f65b290b5e19 --- Cargo.lock | 22 +++++++++++++ cli/Cargo.toml | 1 + cli/args/flags.rs | 79 ++++++++++++++++++++++++++++++++--------------- 3 files changed, 77 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4c3440e8c2..e46621346f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -634,6 +634,27 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "color-print" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a858372ff14bab9b1b30ea504f2a4bc534582aee3e42ba2d41d2a7baba63d5d" +dependencies = [ + "color-print-proc-macro", +] + +[[package]] +name = "color-print-proc-macro" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57e37866456a721d0a404439a1adae37a31be4e0055590d053dfe6981e05003f" +dependencies = [ + "nom 7.1.3", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "color_quant" version = "1.1.0" @@ -955,6 +976,7 @@ dependencies = [ "clap", "clap_complete", "clap_complete_fig", + "color-print", "console_static_text", "dashmap", "data-encoding", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 1a74469eab..a9a93a6fc0 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -79,6 +79,7 @@ chrono.workspace = true clap = { version = "=4.4.17", features = ["env", "string"] } clap_complete = "=4.4.7" clap_complete_fig = "=4.4.2" +color-print = "0.3.5" console_static_text.workspace = true dashmap = "5.5.3" data-encoding.workspace = true diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 707e282560..8bb29e72d4 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -1,5 +1,6 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +use clap::builder::styling::AnsiColor; use clap::builder::FalseyValueParser; use clap::value_parser; use clap::Arg; @@ -806,61 +807,82 @@ impl Flags { } } -static ENV_VARIABLES_HELP: &str = r#"ENVIRONMENT VARIABLES: - DENO_AUTH_TOKENS A semi-colon separated list of bearer tokens and +static ENV_VARIABLES_HELP: &str = color_print::cstr!( + r#"Environment variables: + DENO_AUTH_TOKENS A semi-colon separated list of bearer tokens and hostnames to use when fetching remote modules from private repositories (e.g. "abcde12345@deno.land;54321edcba@github.com") - DENO_TLS_CA_STORE Comma-separated list of order dependent certificate + + DENO_TLS_CA_STORE Comma-separated list of order dependent certificate stores. Possible values: "system", "mozilla". Defaults to "mozilla". - DENO_CERT Load certificate authority from PEM encoded file - DENO_DIR Set the cache directory - DENO_INSTALL_ROOT Set deno install's output directory + + DENO_CERT Load certificate authority from PEM encoded file + + DENO_DIR Set the cache directory + + DENO_INSTALL_ROOT Set deno install's output directory (defaults to $HOME/.deno/bin) - DENO_REPL_HISTORY Set REPL history file path + + DENO_REPL_HISTORY Set REPL history file path History file is disabled when the value is empty (defaults to $DENO_DIR/deno_history.txt) - DENO_NO_PACKAGE_JSON Disables auto-resolution of package.json - DENO_NO_PROMPT Set to disable permission prompts on access + + DENO_NO_PACKAGE_JSON Disables auto-resolution of package.json + + DENO_NO_PROMPT Set to disable permission prompts on access (alternative to passing --no-prompt on invocation) - DENO_NO_UPDATE_CHECK Set to disable checking if a newer Deno version is + + DENO_NO_UPDATE_CHECK Set to disable checking if a newer Deno version is available - DENO_V8_FLAGS Set V8 command line options - DENO_WEBGPU_TRACE Directory to use for wgpu traces - DENO_JOBS Number of parallel workers used for the --parallel + + DENO_V8_FLAGS Set V8 command line options + + DENO_WEBGPU_TRACE Directory to use for wgpu traces + + DENO_JOBS Number of parallel workers used for the --parallel flag with the test subcommand. Defaults to number of available CPUs. - HTTP_PROXY Proxy address for HTTP requests + + HTTP_PROXY Proxy address for HTTP requests (module downloads, fetch) - HTTPS_PROXY Proxy address for HTTPS requests + + HTTPS_PROXY Proxy address for HTTPS requests (module downloads, fetch) - NPM_CONFIG_REGISTRY URL to use for the npm registry. - NO_COLOR Set to disable color - NO_PROXY Comma-separated list of hosts which do not use a proxy - (module downloads, fetch)"#; + + NPM_CONFIG_REGISTRY URL to use for the npm registry. + + NO_COLOR Set to disable color + + NO_PROXY Comma-separated list of hosts which do not use a proxy + (module downloads, fetch)"# +); static DENO_HELP: &str = concat!( - "A modern JavaScript and TypeScript runtime + color_print::cstr!("A modern JavaScript and TypeScript runtime"), + " Docs: https://deno.land/manual@v", env!("CARGO_PKG_VERSION"), - " + color_print::cstr!( + " Modules: https://deno.land/std/ https://deno.land/x/ Bugs: https://github.com/denoland/deno/issues To start the REPL: - deno + deno To execute a script: - deno run https://examples.deno.land/hello-world.ts + deno run https://examples.deno.land/hello-world.ts To evaluate code in the shell: - deno eval \"console.log(30933 + 404)\" + deno eval \"console.log(30933 + 404)\" " + ) ); /// Main entry point for parsing deno's command line flags. @@ -971,7 +993,14 @@ fn clap_root() -> Command { let mut cmd = Command::new("deno") .bin_name("deno") - .color(ColorChoice::Never) + .styles( + clap::builder::Styles::styled() + .header(AnsiColor::Yellow.on_default()) + .usage(AnsiColor::White.on_default()) + .literal(AnsiColor::Green.on_default()) + .placeholder(AnsiColor::Green.on_default()) + ) + .color(ColorChoice::Auto) .max_term_width(80) .version(crate::version::deno()) .long_version(long_version)