Skip to content

Commit

Permalink
fix: simplify option help output, fix default value display
Browse files Browse the repository at this point in the history
  • Loading branch information
chenasraf committed Jan 29, 2024
1 parent cd83a34 commit b3075a0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 37 deletions.
56 changes: 20 additions & 36 deletions src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,46 +423,30 @@ function generateHelpTable<T extends GenerateTableCommandConfig | GenerateTableO
const nameStyle = (name: string) => 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('')
Expand Down
9 changes: 8 additions & 1 deletion src/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,18 @@ const main = massarg<A>({
.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:
Expand Down

0 comments on commit b3075a0

Please sign in to comment.