-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
color-help-replacement.mjs
113 lines (97 loc) · 3.13 KB
/
color-help-replacement.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import stripAnsi from 'strip-ansi';
import wrapAnsi from 'wrap-ansi';
import {
default as chalkStdOut,
chalkStderr as chalkStdErr,
supportsColor as supportsColorStdout,
supportsColorStderr,
} from 'chalk';
import { Command, Help } from 'commander';
// Replace default color and wrapping support with Chalk packages as an example of
// a deep replacement of layout and style support.
// This example requires chalk and wrap-ansi and strip-ansi, and won't run
// from a clone of Commander repo without installing them first.
//
// For example using npm:
// npm install chalk wrap-ansi strip-ansi
class MyHelp extends Help {
constructor() {
super();
this.chalk = chalkStdOut;
}
prepareContext(contextOptions) {
super.prepareContext(contextOptions);
if (contextOptions?.error) {
this.chalk = chalkStdErr;
}
}
displayWidth(str) {
return stripAnsi(str).length; // use imported package
}
boxWrap(str, width) {
return wrapAnsi(str, width, { hard: true }); // use imported package
}
styleTitle(str) {
return this.chalk.bold(str);
}
styleCommandText(str) {
return this.chalk.cyan(str);
}
styleCommandDescription(str) {
return this.chalk.magenta(str);
}
styleDescriptionText(str) {
return this.chalk.italic(str);
}
styleOptionText(str) {
return this.chalk.green(str);
}
styleArgumentText(str) {
return this.chalk.yellow(str);
}
styleSubcommandText(str) {
return this.chalk.blue(str);
}
}
class MyCommand extends Command {
createCommand(name) {
return new MyCommand(name);
}
createHelp() {
return Object.assign(new MyHelp(), this.configureHelp());
}
}
const program = new MyCommand();
// Override the color detection to use Chalk's detection.
// Chalk overrides color support based on the `FORCE_COLOR` environment variable,
// and looks for --color and --no-color command-line options.
// See https://github.com/chalk/chalk?tab=readme-ov-file#supportscolor
//
// In general we want stripColor() to be consistent with displayWidth().
program.configureOutput({
getOutHasColors: () => supportsColorStdout,
getErrHasColors: () => supportsColorStderr,
stripColor: (str) => stripAnsi(str),
});
program.description('program description '.repeat(10));
program
.option('-s', 'short description')
.option('--long <number>', 'long description '.repeat(10))
.option('--color', 'force color output') // implemented by chalk
.option('--no-color', 'disable color output'); // implemented by chalk
program.addHelpText('after', (context) => {
const chalk = context.error ? chalkStdErr : chalkStdOut;
return chalk.italic('\nThis is additional help text.');
});
program.command('esses').description('sssss '.repeat(33));
program
.command('print')
.description('print files')
.argument('<files...>', 'files to queue for printing')
.option('--double-sided', 'print on both sides');
program.parse();
// Try the following (after installing the required packages):
// node color-help-replacement.mjs --help
// node color-help-replacement.mjs --no-color help
// FORCE_COLOR=0 node color-help-replacement.mjs help
// node color-help-replacement.mjs help print