Skip to content

Commit

Permalink
Code tidy from review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Sep 21, 2019
1 parent 725ebd1 commit 8d7d080
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions index.js
Expand Up @@ -1155,14 +1155,13 @@ Command.prototype.optionHelp = function() {

var columns = process.stdout.columns || 80;
var descriptionWidth = columns - width - 4;
var minWidth = 40;

// Append the help information
return this.options.map(function(option) {
const fullDesc = option.description +
((!option.negate && option.defaultValue !== undefined) ? ' (default: ' + JSON.stringify(option.defaultValue) + ')' : '');
return pad(option.flags, width) + ' ' + optionalWrap(fullDesc, descriptionWidth, width + 2, minWidth);
}).concat([pad(this._helpFlags, width) + ' ' + optionalWrap(this._helpDescription, descriptionWidth, width + 2, minWidth)])
return pad(option.flags, width) + ' ' + optionalWrap(fullDesc, descriptionWidth, width + 2);
}).concat([pad(this._helpFlags, width) + ' ' + optionalWrap(this._helpDescription, descriptionWidth, width + 2)])
.join('\n');
};

Expand All @@ -1181,13 +1180,12 @@ Command.prototype.commandHelp = function() {

var columns = process.stdout.columns || 80;
var descriptionWidth = columns - width - 4;
var minWidth = 40;

return [
'Commands:',
commands.map(function(cmd) {
var desc = cmd[1] ? ' ' + cmd[1] : '';
return (desc ? pad(cmd[0], width) : cmd[0]) + optionalWrap(desc, descriptionWidth, width + 2, minWidth);
return (desc ? pad(cmd[0], width) : cmd[0]) + optionalWrap(desc, descriptionWidth, width + 2);
}).join('\n').replace(/^/gm, ' '),
''
].join('\n');
Expand Down Expand Up @@ -1353,32 +1351,33 @@ function pad(str, width) {
function wrap(str, width, indent) {
var regex = new RegExp('.{1,' + (width - 1) + '}([\\s\u200B]|$)|[^\\s\u200B]+?([\\s\u200B]|$)', 'g');
var lines = str.match(regex) || [];
var result = lines.map(function(line, i) {
return lines.map(function(line, i) {
if (line.slice(-1) === '\n') {
line = line.slice(0, line.length - 1);
}
return ((i > 0 && indent) ? Array(indent + 1).join(' ') : '') + line;
}).join('\n');
return result;
}

/**
* Optionally wrap the given str to a max width of width characters per line
* while indenting with indent spaces. Do not wrap the text when the minWidth
* is not hit.
* while indenting with indent spaces. Do not wrap if insufficient width or
* string is manually formatted.
*
* @param {String} str
* @param {Number} width
* @param {Number} indent
* @return {String}
* @api private
*/
function optionalWrap(str, width, indent, minWidth) {
// detected manually wrapped and indented strings by searching for line breaks
// followed by multiple spaces/tabs
function optionalWrap(str, width, indent) {
// Detect manually wrapped and indented strings by searching for line breaks
// followed by multiple spaces/tabs.
if (str.match(/[\n]\s+/)) return str;
// check if minimum width is reached
// Do not wrap to narrow columns (or can end up with a word per line).
const minWidth = 40;
if (width < minWidth) return str;

return wrap(str, width, indent);
}

Expand Down

0 comments on commit 8d7d080

Please sign in to comment.