Skip to content

Commit

Permalink
Replace chalk with ansi-colors
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Dec 12, 2021
1 parent 21e2702 commit 2c9e06d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## next

- Allowed args after and between options
- Replaced `chalk` with `ansi-colors`

## 3.0.0-beta.1

Expand Down
31 changes: 9 additions & 22 deletions lib/help.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
const colors = require('ansi-colors');

const MAX_LINE_WIDTH = process.stdout.columns || 200;
const MIN_OFFSET = 20;
const reAstral = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
const ansiRegex = /\x1B\[([0-9]{1,3}(;[0-9]{1,3})*)?[m|K]/g;
const byName = (a, b) => a.name > b.name || -(a.name < b.name);
let chalk;

function initChalk() {
if (!chalk) {
const ChalkInstance = require('chalk').Instance;

chalk = new ChalkInstance({
level: Number(process.stdout.isTTY)
});
}

return chalk;
}

function stringLength(str) {
return str
Expand Down Expand Up @@ -82,7 +71,7 @@ function commandsHelp(command) {

const lines = command.getCommands().sort(byName).map(({ name, meta, params }) => ({
description: meta.description,
name: chalk.green(name) + args(params, chalk.gray)
name: colors.green(name) + args(params, colors.gray)
}));

return [
Expand All @@ -104,8 +93,8 @@ function optionsHelp(command) {
const lines = options.map(({ short, long, params, description }) => ({
description,
name: [
short ? chalk.yellow(short) + ', ' : shortPlaceholder,
chalk.yellow(long),
short ? colors.yellow(short) + ', ' : shortPlaceholder,
colors.yellow(long),
args(params)
].join('')
}));
Expand All @@ -127,19 +116,17 @@ function optionsHelp(command) {
* @api private
*/
module.exports = function getCommandHelp(command, commandPath) {
initChalk();

commandPath = Array.isArray(commandPath) && commandPath.length
? commandPath.concat(command.name).join(' ')
: command.name;

return [
(command.meta.description ? command.meta.description + '\n\n' : '') +
'Usage:\n\n' +
' ' + chalk.cyan(commandPath) +
args(command.params, chalk.magenta) +
(command.options.size !== 0 ? ' [' + chalk.yellow('options') + ']' : '') +
(command.commands.size !== 0 ? ' [' + chalk.green('command') + ']' : ''),
' ' + colors.cyan(commandPath) +
args(command.params, colors.magenta) +
(command.options.size !== 0 ? ' [' + colors.yellow('options') + ']' : '') +
(command.commands.size !== 0 ? ' [' + colors.green('command') + ']' : ''),
commandsHelp(command) +
optionsHelp(command)
].join('\n');
Expand Down
30 changes: 21 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"node": ">=8.0.0"
},
"dependencies": {
"chalk": "^4.1.2"
"ansi-colors": "^4.1.1"
},
"devDependencies": {
"c8": "^7.10.0",
Expand Down

0 comments on commit 2c9e06d

Please sign in to comment.