From f234e16e80e99d19a9976de5dd1343b04e683669 Mon Sep 17 00:00:00 2001 From: Zhaofeng Li Date: Mon, 3 Jan 2022 10:37:03 -0800 Subject: [PATCH] manual: Fix colorized CLI help In clap 3.0, it's no longer possible to get `App::write_long_help` to output colorized text (it doesn't invoke the Colorizer at all). So let's move the generation outside of Rust. --- Cargo.lock | 31 ------------------ Cargo.toml | 1 - default.nix | 2 +- manual/colorized-help.nix | 30 +++++++++++++++++ manual/default.nix | 15 ++++++--- manual/src/reference/cli.md | 2 +- src/cli.rs | 64 ++++--------------------------------- 7 files changed, 49 insertions(+), 96 deletions(-) create mode 100644 manual/colorized-help.nix diff --git a/Cargo.lock b/Cargo.lock index c0c2e25..b79702a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,16 +11,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "ansi-to-html" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19ee82de0545b181a17cbdef44fce80ecaf394e001da7ea279008bf2e0944bee" -dependencies = [ - "regex", - "thiserror", -] - [[package]] name = "async-stream" version = "0.3.2" @@ -134,7 +124,6 @@ dependencies = [ name = "colmena" version = "0.3.0-pre" dependencies = [ - "ansi-to-html", "async-trait", "atty", "clap", @@ -835,26 +824,6 @@ version = "0.14.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" -[[package]] -name = "thiserror" -version = "1.0.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "tinyvec" version = "1.5.1" diff --git a/Cargo.toml b/Cargo.toml index 81c83ac..ccab493 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,6 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -ansi-to-html = "0.1.0" async-trait = "0.1.42" atty = "0.2" clap = "3.0.0" diff --git a/default.nix b/default.nix index 39db837..6e5de3f 100644 --- a/default.nix +++ b/default.nix @@ -17,7 +17,7 @@ in rustPlatform.buildRustPackage rec { src = lib.cleanSource ./.; }; - cargoSha256 = "sha256-FVHNtgOZCC2aC2ilt1UreDidhiAuunPOO3E9ejJzwmw="; + cargoSha256 = "sha256-rJnPo8xmasRNOYttDuG80QqfKyeEcl7gn85dvYgeOpw="; nativeBuildInputs = [ installShellFiles ]; diff --git a/manual/colorized-help.nix b/manual/colorized-help.nix new file mode 100644 index 0000000..a9fda85 --- /dev/null +++ b/manual/colorized-help.nix @@ -0,0 +1,30 @@ +{ runCommand, colmena, ansi2html }: + +with builtins; + +let + subcommands = [ + null + "apply" + "apply-local" + "build" + "upload-keys" + "eval" + "exec" + "nix-info" + ]; + renderHelp = subcommand: let + fullCommand = if subcommand == null then "colmena" else "colmena ${subcommand}"; + in '' + ( + echo '## `${fullCommand}`' + echo -n '
' + TERM=xterm-256color CLICOLOR_FORCE=1 ${fullCommand} --help | ansi2html -p + echo '
' + )>>$out + ''; +in runCommand "colmena-colorized-help" { + nativeBuildInputs = [ colmena ansi2html ]; +} ('' + ansi2html -H > $out +'' + concatStringsSep "\n" (map renderHelp subcommands)) diff --git a/manual/default.nix b/manual/default.nix index 90f4959..08de9fc 100644 --- a/manual/default.nix +++ b/manual/default.nix @@ -1,4 +1,4 @@ -{ lib, stdenv, nix-gitignore, mdbook, python3, writeScript +{ lib, stdenv, nix-gitignore, mdbook, python3, callPackage, writeScript , deploymentOptionsMd ? null , metaOptionsMd ? null , colmena ? null @@ -13,6 +13,12 @@ let apiVersion = builtins.concatStringsSep "." (lib.take 2 (lib.splitString "." version)); + colorizedHelp = let + help = callPackage ./colorized-help.nix { + inherit colmena; + }; + in if colmena != null then help else null; + redirectTemplate = lib.escapeShellArg '' @@ -28,7 +34,7 @@ let ''; in stdenv.mkDerivation { - inherit version deploymentOptionsMd metaOptionsMd; + inherit version deploymentOptionsMd metaOptionsMd colorizedHelp; pname = "colmena-manual" + (if unstable then "-unstable" else ""); @@ -48,9 +54,8 @@ in stdenv.mkDerivation { ''; buildPhase = '' - if [ -n "${toString colmena}" ]; then - echo "Generating CLI help text" - ${toString colmena}/bin/colmena gen-help-markdown >> src/reference/cli.md + if [[ -n "$colorizedHelp" ]]; then + cat "$colorizedHelp" >> src/reference/cli.md else echo "Error: No colmena executable passed to the builder" >> src/reference/cli.md fi diff --git a/manual/src/reference/cli.md b/manual/src/reference/cli.md index 3f6726b..b4ce911 100644 --- a/manual/src/reference/cli.md +++ b/manual/src/reference/cli.md @@ -1,4 +1,4 @@ -# Command Line Arguments +# Command Line Options You are currently reading **the unstable version** of the Colmena Manual, built against the tip of [the development branch](https://github.com/zhaofengli/colmena). diff --git a/src/cli.rs b/src/cli.rs index c6d8364..177a9d3 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -140,10 +140,6 @@ It's also possible to specify the preference using environment variables. See
- let mut commands = vec![ - build_cli(false), - command::apply::subcommand(), - command::apply_local::subcommand(), - command::build::subcommand(), - command::upload_keys::subcommand(), - command::eval::subcommand(), - command::exec::subcommand(), - command::nix_info::subcommand(), - ]; - - for command in commands.drain(..) { - let full_command = match command.get_name() { - "Colmena" => "colmena".to_string(), - sub => format!("colmena {}", sub), - }; - - let mut command = { - let c = command - .color(ColorChoice::Always); - - if full_command != "colmena" { - c.bin_name(&full_command) - } else { - c - } - }; - - println!("## `{}`", full_command); - print!("
"); - - let help_message = { - let mut bytes = Vec::new(); - command.write_long_help(&mut bytes).unwrap(); - String::from_utf8(bytes).unwrap() - }; - - let help_html = ansi_to_html::convert(&help_message, true, true) - .expect("Could not convert terminal output to HTML"); - - print!("{}", help_html); - println!("
"); - } -} - fn set_color_pref(cli: &str) { if cli != "auto" { clicolors_control::set_colors_enabled(cli == "always");