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.
This commit is contained in:
Zhaofeng Li 2022-01-03 10:37:03 -08:00
parent d2762757f0
commit f234e16e80
7 changed files with 49 additions and 96 deletions

30
manual/colorized-help.nix Normal file
View file

@ -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 '<pre><div class="hljs">'
TERM=xterm-256color CLICOLOR_FORCE=1 ${fullCommand} --help | ansi2html -p
echo '</div></pre>'
)>>$out
'';
in runCommand "colmena-colorized-help" {
nativeBuildInputs = [ colmena ansi2html ];
} (''
ansi2html -H > $out
'' + concatStringsSep "\n" (map renderHelp subcommands))

View file

@ -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 ''
<!doctype html>
<html>
@ -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

View file

@ -1,4 +1,4 @@
# Command Line Arguments
# Command Line Options
<!-- UNSTABLE_BEGIN -->
You are currently reading **the unstable version** of the Colmena Manual, built against the tip of [the development branch](https://github.com/zhaofengli/colmena).