Skip to content
Myr

color-help-replacement.mjs

This file

Path

examples/color-help-replacement.mjs

113 lines · 3.1 KiB · JavaScript

Last changed by

More Help documentation (#2282)John Geebefore adoptiond355b52

History of this file

Take it

Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947

lines 1–11why, one step back
1import stripAnsi from 'strip-ansi';
2import wrapAnsi from 'wrap-ansi';3import {4 default as chalkStdOut,5 chalkStderr as chalkStdErr,6 supportsColor as supportsColorStdout,7 supportsColorStderr,8} from 'chalk';9import { Command, Help } from 'commander';1011// Replace default color and wrapping support with Chalk packages as an example of

More Help documentation (#2282)John Geebefore adoptiond355b52

line 12why, one step back
12// a deep replacement of layout and style support.

Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947

lines 13–49why, one step back
13
14// This example requires chalk and wrap-ansi and strip-ansi, and won't run15// from a clone of Commander repo without installing them first.16//17// For example using npm:18// npm install chalk wrap-ansi strip-ansi1920class MyHelp extends Help {21 constructor() {22 super();23 this.chalk = chalkStdOut;24 }2526 prepareContext(contextOptions) {27 super.prepareContext(contextOptions);28 if (contextOptions?.error) {29 this.chalk = chalkStdErr;30 }31 }3233 displayWidth(str) {34 return stripAnsi(str).length; // use imported package35 }3637 boxWrap(str, width) {38 return wrapAnsi(str, width, { hard: true }); // use imported package39 }4041 styleTitle(str) {42 return this.chalk.bold(str);43 }44 styleCommandText(str) {45 return this.chalk.cyan(str);46 }47 styleCommandDescription(str) {48 return this.chalk.magenta(str);49 }

Split styleItemDescription into option/subcommand/argument (#2283)John Geebefore adoption437e4e3

line 50why, one step back
50 styleDescriptionText(str) {

Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947

lines 51–113why, one step back
51 return this.chalk.italic(str);
52 }53 styleOptionText(str) {54 return this.chalk.green(str);55 }56 styleArgumentText(str) {57 return this.chalk.yellow(str);58 }59 styleSubcommandText(str) {60 return this.chalk.blue(str);61 }62}6364class MyCommand extends Command {65 createCommand(name) {66 return new MyCommand(name);67 }68 createHelp() {69 return Object.assign(new MyHelp(), this.configureHelp());70 }71}7273const program = new MyCommand();7475// Override the color detection to use Chalk's detection.76// Chalk overrides color support based on the `FORCE_COLOR` environment variable,77// and looks for --color and --no-color command-line options.78// See https://github.com/chalk/chalk?tab=readme-ov-file#supportscolor79//80// In general we want stripColor() to be consistent with displayWidth().81program.configureOutput({82 getOutHasColors: () => supportsColorStdout,83 getErrHasColors: () => supportsColorStderr,84 stripColor: (str) => stripAnsi(str),85});8687program.description('program description '.repeat(10));88program89 .option('-s', 'short description')90 .option('--long <number>', 'long description '.repeat(10))91 .option('--color', 'force color output') // implemented by chalk92 .option('--no-color', 'disable color output'); // implemented by chalk9394program.addHelpText('after', (context) => {95 const chalk = context.error ? chalkStdErr : chalkStdOut;96 return chalk.italic('\nThis is additional help text.');97});9899program.command('esses').description('sssss '.repeat(33));100101program102 .command('print')103 .description('print files')104 .argument('<files...>', 'files to queue for printing')105 .option('--double-sided', 'print on both sides');106107program.parse();108109// Try the following (after installing the required packages):110// node color-help-replacement.mjs --help111// node color-help-replacement.mjs --no-color help112// FORCE_COLOR=0 node color-help-replacement.mjs help113// node color-help-replacement.mjs help print