Skip to content
Myr

Wrap command description in help (#1804)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Wrap command description. Replace trimRight by trimEnd. * Preserve empty lines when wrapping * Simplify line end handling * Translate Windows line endings to keep things sane

authored by John Gee · committed by GitHub · parent 471ec40 · Browse files · 2 files+59 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+59 added−14 removed

  • tests:+45 added−3 removed
  • other:+14 added−11 removed

2 files+59 added−14 removed

modified:lib/help.js+14 added-11 removed
320 unmodified lines
321322 // Description323 const commandDescription = helper.commandDescription(cmd);324 if (commandDescription.length > 0) {325 output = output.concat([commandDescription, '']);325 output = output.concat([helper.wrap(commandDescription, helpWidth, 0), '']);326 }327328 // Arguments329 const argumentList = helper.visibleArguments(cmd).map((argument) => {
50 unmodified lines
380 *381 */382383 wrap(str, width, indent, minColumnWidth = 40) {384 // Detect manually wrapped and indented strings by searching for line breaks385 // followed by multiple spaces/tabs.386 if (str.match(/[\n]\s+/)) return str;384 // Full \s characters, minus the linefeeds.385 const indents = ' \\f\\t\\v\u00a0\u1680\u2000-\u200a\u202f\u205f\u3000\ufeff';386 // Detect manually wrapped and indented strings by searching for line break followed by spaces.387 const manualIndent = new RegExp(`[\\n][${indents}]+`);388 if (str.match(manualIndent)) return str;389 // Do not wrap if not enough room for a wrapped column of text (as could end up with a word per line).390 const columnWidth = width - indent;391 if (columnWidth < minColumnWidth) return str;392393 const leadingStr = str.slice(0, indent);392 const columnText = str.slice(indent);393394 const columnText = str.slice(indent).replace('\r\n', '\n');395 const indentString = ' '.repeat(indent);395 const regex = new RegExp('.{1,' + (columnWidth - 1) + '}([\\s\u200B]|$)|[^\\s\u200B]+?([\\s\u200B]|$)', 'g');396 const zeroWidthSpace = '\u200B';397 const breaks = `\\s${zeroWidthSpace}`;398 // Match line end (so empty lines don't collapse),399 // or as much text as will fit in column, or excess text up to first break.400 const regex = new RegExp(`\n|.{1,${columnWidth - 1}}([${breaks}]|$)|[^${breaks}]+?([${breaks}]|$)`, 'g');401 const lines = columnText.match(regex) || [];402 return leadingStr + lines.map((line, i) => {398 if (line.slice(-1) === '\n') {399 line = line.slice(0, line.length - 1);400 }401 return ((i > 0) ? indentString : '') + line.trimRight();403 if (line === '\n') return ''; // preserve empty lines404 return ((i > 0) ? indentString : '') + line.trimEnd();405 }).join('\n');406 }407}408
1 unmodified line
modified:tests/help.wrap.test.js+45 added-3 removed