diff --git a/index.js b/index.js index a53f6000e..9f89d0535 100644 --- a/index.js +++ b/index.js @@ -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'); }; @@ -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'); @@ -1353,19 +1351,18 @@ 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 @@ -1373,12 +1370,14 @@ function wrap(str, width, 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); }