fix the line length after the first line; cleanup

This commit is contained in:
Jeff Williams 2013-12-20 16:08:29 -08:00
parent 405ebb5c3b
commit 2babc5b1c3

View File

@ -90,7 +90,7 @@ function findMaxLength(arr) {
return max;
}
function concatWithMax(items, maxLength) {
function concatWithMaxLength(items, maxLength) {
var result = '';
// to prevent endless loops, always use the first item, regardless of length
result += items.shift();
@ -106,17 +106,14 @@ function concatWithMax(items, maxLength) {
// | -f, --foo Very long description very long description very long |
// | description very long description. |
function formatHelpInfo(options) {
var MARGIN_LENGTH = 4;
var results = [];
var marginLength = 4;
var maxLength = process.stdout.columns;
var maxNameLength = findMaxLength(options.names);
var maxDescriptionLength = findMaxLength(options.descriptions);
var wrapDescriptionAt = maxLength - (marginLength * 3);
var wrapDescriptionFirstLineAt = wrapDescriptionAt - maxNameLength;
var wrapDescriptionAt = maxLength - (MARGIN_LENGTH * 3) - maxNameLength;
// build the string for each option
options.names.forEach(function(name, i) {
var result;
@ -124,19 +121,18 @@ function formatHelpInfo(options) {
var words;
// add a left margin to the name
result = padLeft(options.names[i], marginLength);
result = padLeft(options.names[i], MARGIN_LENGTH);
// and a right margin, with extra padding so the descriptions line up with one another
result = padRight(result, maxNameLength - options.names[i].length + marginLength);
result = padRight(result, maxNameLength - options.names[i].length + MARGIN_LENGTH);
// split the description on spaces
words = options.descriptions[i].split(' ');
// add as much of the description as we can fit on the first line
result += concatWithMax(words, wrapDescriptionFirstLineAt);
result += concatWithMaxLength(words, wrapDescriptionAt);
// if there's anything left, keep going until we've consumed the description
while (words.length) {
partialDescription = padding( maxNameLength + (marginLength * 2) );
partialDescription += concatWithMax(words, wrapDescriptionAt -
partialDescription.length);
partialDescription = padding( maxNameLength + (MARGIN_LENGTH * 2) );
partialDescription += concatWithMaxLength(words, wrapDescriptionAt);
result += '\n' + partialDescription;
}