From b3075a03b39939ebe3c313c21d214f0d8d23c718 Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Tue, 30 Jan 2024 01:18:23 +0200 Subject: [PATCH] fix: simplify option help output, fix default value display --- src/help.ts | 56 ++++++++++++++++++--------------------------------- src/sample.ts | 9 ++++++++- 2 files changed, 28 insertions(+), 37 deletions(-) diff --git a/src/help.ts b/src/help.ts index 3aba4c6..bb909dc 100644 --- a/src/help.ts +++ b/src/help.ts @@ -423,46 +423,30 @@ function generateHelpTable format(name, config.nameStyle) const descStyle = (desc: string) => format(desc, config.descriptionStyle) const table = rows.map((row) => { - const nameLines = row.name.split('\n').map((l) => nameStyle(l).padEnd(maxNameLength! + 2)) - let description = descStyle(row.description) - if (displayDefaultValue && row.defaultValue != null) { - description += ` ${format(`(default: ${row.defaultValue})`, config.defaultValueStyle)}` - } - const firstNameLine = nameStyle(stripStyle(nameLines[0]).padEnd(maxNameLength! + 2)) - const length = - stripStyle(firstNameLine).padEnd(maxNameLength! + 2).length + stripStyle(description).length - if (length <= lineLength) { - let line = `${firstNameLine}${description}` - if (nameLines.length > 1) { - line += `\n${nameLines.slice(1).join('\n')}` - } - if (!compact) { - return `${line}\n` - } - return line - } + const nameLines = row.name.split('\n').map((l) => nameStyle(l.padEnd(maxNameLength! + 2))) + const descLines = wrap(row.description, lineLength - maxNameLength!) + .split('\n') + .map(descStyle) + const max = Math.max(nameLines.length, descLines.length) const subRows: string[] = [] - const words = description.split(' ') - let currentRow = firstNameLine - let rowCount = 0 - for (const word of words) { - if (stripStyle(currentRow).length + stripStyle(word).length + 1 > lineLength) { - subRows.push(currentRow) - rowCount++ - if (rowCount > 0 && rowCount < nameLines.length) { - currentRow = nameStyle(stripStyle(nameLines[rowCount]).padEnd(maxNameLength! + 2)) - currentRow += format('', { ...config.descriptionStyle, reset: false }) - } else { - currentRow = ' '.repeat(maxNameLength! + 2) - } - } - currentRow += `${word} ` + for (let i = 0; i < max - 1; i++) { + subRows.push(`${(nameLines[i] ?? ' ').padEnd(maxNameLength! + 2)}${descLines[i] ?? ''}`) } - if (rowCount > 0 && rowCount < nameLines.length) { - currentRow += ` ${nameLines[rowCount].padEnd(maxNameLength! + 2)}` + const defaultText = + displayDefaultValue && row.defaultValue != null + ? format(`(default: ${row.defaultValue})`, config.defaultValueStyle) + : '' + if (subRows.length + 1 + stripStyle(defaultText).length <= lineLength) { + subRows.push( + `${(nameLines[max - 1] ?? ' ').padEnd(maxNameLength! + 2)}${descLines[max - 1] ?? ''}${defaultText}`, + ) + } else { + subRows.push( + `${(nameLines[max - 1] ?? ' ').padEnd(maxNameLength! + 2)}${descLines[max - 1] ?? ''}`, + ) + subRows.push(defaultText.padStart(maxNameLength! + 2)) } - subRows.push(currentRow) if (!compact) { subRows.push('') diff --git a/src/sample.ts b/src/sample.ts index bfa1503..307b422 100644 --- a/src/sample.ts +++ b/src/sample.ts @@ -109,11 +109,18 @@ const main = massarg({ .flag({ name: 'bool2', description: - 'Ad consequat eiusmod officia aliqua. Eiusmod officia aliqua amet et laboris. Aliqua amet et laboris officia proident. Et, laboris officia proident minim duis officia. Proident minim, duis officia.', + 'Ad consequat eiusmod officia aliqua. Eiusmod officia aliqua amet et laboris. Aliqua amet et laboris officia proident. Et, laboris officia proident minim duis officia. Proident minim, duis officia. Non commodo ...', aliases: ['e'], negatable: true, defaultValue: false, }) + .flag({ + name: 'bool3', + description: 'Ad consequat eiusmod officia aliqua. Eiusmod officia aliqua ', + aliases: ['e22'], + negatable: true, + defaultValue: false, + }) .option({ name: 'string', description: