Skip to content

Commit

Permalink
perf: swap chalk for colorette (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgebucaran authored and mattallty committed Oct 7, 2018
1 parent fba6d4a commit d2fc842
Show file tree
Hide file tree
Showing 16 changed files with 761 additions and 741 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"unix"
],
"no-console": 0,
"no-control-regex": 0,
"quotes": 0,
"semi": [0, "never"]
}
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,8 @@ Define an auto-completion handler for the latest argument or option added to the
## Credits

Caporal is strongly inspired by [commander.js](https:/tj/commander.js) and [Symfony Console](http://symfony.com/doc/current/components/console.html).
Caporal make use of the following npm packages:
* [chalk](https://www.npmjs.com/package/chalk) for colors
Caporal makes use of the following npm packages:
* [colorette](https://www.npmjs.com/package/colorette) for colors
* [cli-table2](https://www.npmjs.com/package/cli-table2) for cli tables
* [fast-levenshtein](https://www.npmjs.com/package/fast-levenshtein) for suggestions
* [tabtab](https://www.npmjs.com/package/tabtab) for auto-completion
Expand Down
10 changes: 5 additions & 5 deletions lib/colorful.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"use strict";

const chalk = require('chalk');
const c = require('colorette');

exports.colorize = (text) => {
return text.replace(/<([a-z0-9-_.]+)>/gi, (match) => {
return chalk.blue(match);
return c.blue(match);
}).replace(/<command>/gi, (match) => {
return chalk.magenta(match);
return c.magenta(match);
}).replace(/\[([a-z0-9-_.]+)\]/gi, (match) => {
return chalk.yellow(match);
return c.yellow(match);
}).replace(/ --?([a-z0-9-_.]+)/gi, (match) => {
return chalk.green(match);
return c.green(match);
});
};
6 changes: 3 additions & 3 deletions lib/error/base-error.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use strict";

const chalk = require('chalk');
const c = require('colorette');

class BaseError extends Error {
constructor(message, meta, program) {
const spaces = " ".repeat(3);
const msg = spaces + chalk.red("Error: " + message) + "\n" + spaces +
`Type ${chalk.bold(program.bin() + ' --help')} for help.\n`;
const msg = spaces + c.red("Error: " + message) + "\n" + spaces +
`Type ${c.bold(program.bin() + ' --help')} for help.\n`;
super(msg);
this.name = this.constructor.name;
this.originalMessage = message;
Expand Down
4 changes: 2 additions & 2 deletions lib/error/invalid-argument-value.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use strict";

const BaseError = require('./base-error');
const chalk = require('chalk');
const c = require('colorette');

class InvalidArgumentValueError extends BaseError {

constructor(arg, value, command, originalError, program) {
let msg = `Invalid value '${value}' for argument ${chalk.italic(arg)}.\n ${originalError.meta.originalError}`;
let msg = `Invalid value '${value}' for argument ${c.italic(arg)}.\n ${originalError.meta.originalError}`;
super(msg, {arg, command, value}, program);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/error/invalid-option-value.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use strict";

const BaseError = require('./base-error');
const chalk = require('chalk');
const getDashedOption = require('../utils').getDashedOption;
const c = require('colorette');

class InvalidOptionValueError extends BaseError {
constructor(option, value, command, originalError, program) {
let msg = `Invalid value '${value}' for option ${chalk.italic(getDashedOption(option))}.\n ${originalError.meta.originalError}`;
let msg = `Invalid value '${value}' for option ${c.italic(getDashedOption(option))}.\n ${originalError.meta.originalError}`;

super(msg, {option, command, originalError}, program);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/error/missing-option.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use strict";

const BaseError = require('./base-error');
const chalk = require('chalk');
const getDashedOption = require('../utils').getDashedOption;
const c = require('colorette');

class MissingOptionError extends BaseError {
constructor(option, command, program) {
let msg = `Missing option ${chalk.italic(getDashedOption(option))}.`;
let msg = `Missing option ${c.italic(getDashedOption(option))}.`;
super(msg, {option, command}, program);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/error/unknown-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const BaseError = require('./base-error');
const getSuggestions = require('../suggest').getSuggestions;
const getBoldDiffString = require('../suggest').getBoldDiffString;
const getDashedOption = require('../utils').getDashedOption;
const chalk = require('chalk');
const c = require('colorette');

class UnknownOptionError extends BaseError {
constructor(option, command, program) {
const suggestions = getSuggestions(option, command._getLongOptions());
let msg = `Unknown option ${chalk.italic(getDashedOption(option))}.`;
let msg = `Unknown option ${c.italic(getDashedOption(option))}.`;
if (suggestions.length) {
msg += ' Did you mean ' + suggestions.map(
s => '--' + getBoldDiffString(option, s)
Expand Down
28 changes: 14 additions & 14 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const merge = require('lodash.merge');
const Table = require('cli-table2');
const chalk = require('chalk');
const colorize = require('./colorful').colorize;
const c = require('colorette')

/**
* @private
Expand Down Expand Up @@ -41,7 +41,7 @@ class Help {

const description = this._program.description() ? '- ' + this._program.description() : '';
let help = `
${chalk.cyan(this._program.name() || this._program.bin())} ${chalk.dim(this._program.version())} ${description}
${c.cyan(this._program.name() || this._program.bin())} ${c.dim(this._program.version())} ${description}
${this._getUsage(command)}`;

Expand All @@ -63,17 +63,17 @@ class Help {
.filter((c) => c.name() !== '')
.forEach(cmd => {
commandTable.push(
[chalk.magenta(cmd.getSynopsis()), cmd.description()]
[c.magenta(cmd.getSynopsis()), cmd.description()]
);
});
commandTable.push([chalk.magenta('help <command>'), 'Display help for a specific command']);
return chalk.bold('COMMANDS') + "\n\n" + colorize(commandTable.toString());
commandTable.push([c.magenta('help <command>'), 'Display help for a specific command']);
return c.bold('COMMANDS') + "\n\n" + colorize(commandTable.toString());
}

_getGlobalOptions() {
const optionsTable = this._getSimpleTable();
this._getPredefinedOptions().forEach(o => optionsTable.push(o));
return chalk.bold('GLOBAL OPTIONS') + "\n\n" + colorize(optionsTable.toString());
return c.bold('GLOBAL OPTIONS') + "\n\n" + colorize(optionsTable.toString());
}

_getCommandHelp(cmd) {
Expand All @@ -84,23 +84,23 @@ class Help {
help += this._renderHelp(cmd._name);

if (args.length) {
help += `\n\n ${chalk.bold('ARGUMENTS')}\n\n`;
help += `\n\n ${c.bold('ARGUMENTS')}\n\n`;
const argsTable = this._getSimpleTable();
args.forEach(a => {
const def = a.hasDefault() ? 'default: ' + JSON.stringify(a.default()) : '';
const req = a.isRequired() ? chalk.bold('required') : chalk.grey('optional');
argsTable.push([a.synopsis(), a.description(), req, chalk.grey(def)])
const req = a.isRequired() ? c.bold('required') : c.gray('optional');
argsTable.push([a.synopsis(), a.description(), req, c.gray(def)])
});
help += argsTable.toString();
}

if (options.length) {
help += `\n\n ${chalk.bold('OPTIONS')}\n\n`;
help += `\n\n ${c.bold('OPTIONS')}\n\n`;
const optionsTable = this._getSimpleTable();
options.forEach(a => {
const def = a.hasDefault() ? 'default: ' + JSON.stringify(a.default()) : '';
const req = a.isRequired() ? chalk.bold('required') : chalk.grey('optional');
optionsTable.push([a.synopsis(), a.description(), req, chalk.grey(def)])
const req = a.isRequired() ? c.bold('required') : c.gray('optional');
optionsTable.push([a.synopsis(), a.description(), req, c.gray(def)])
});
help += optionsTable.toString();
}
Expand All @@ -109,7 +109,7 @@ class Help {
}

_getUsage(cmd) {
let help = `${chalk.bold('USAGE')}\n\n ${chalk.italic(this._program.name() || this._program.bin())} `;
let help = `${c.bold('USAGE')}\n\n ${c.italic(this._program.name() || this._program.bin())} `;
if (cmd) {
help += colorize(this._getCommandHelp(cmd));
} else {
Expand Down Expand Up @@ -146,7 +146,7 @@ class Help {
const help = args[0];
const options = args[1];
if (options.name) {
rendered += "\n\n " + chalk.bold(options.name);
rendered += "\n\n " + c.bold(options.name);
}
const parsedHelp = options.indent ? " " + help.split ( '\n' ).join ( '\n ' ) : help;
rendered += "\n\n" + parsedHelp;
Expand Down
5 changes: 2 additions & 3 deletions lib/suggest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const levenshtein = require('fast-levenshtein');
const chalk = require('chalk');
const c = require('colorette');

/**
*
Expand All @@ -21,9 +21,8 @@ exports.getSuggestions = function getSuggestions(input, possibilities) {
exports.getBoldDiffString = (from, to) => {
return to.split('').map((char, index) => {
if (char != from.charAt(index)) {
return chalk.bold(char);
return c.bold(char);
}
return char;
}).join('')
};

Loading

0 comments on commit d2fc842

Please sign in to comment.