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
Provenance
- Commit
186f0c8- Tree
e637f3e- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
471ec40
2 files changed+59 added−14 removed
modified:lib/help.js+14 added-11 removed
320 unmodified lines321 322 // 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 }327 328 // Arguments329 const argumentList = helper.visibleArguments(cmd).map((argument) => {50 unmodified lines380 *381 */382 383 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;392 393 const leadingStr = str.slice(0, indent);392- const columnText = str.slice(indent);393-394+ 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 linemodified:tests/help.wrap.test.js+45 added-3 removed
39 unmodified lines4041424344444546474849505152535455565758596061626364656667686970717273747541 unmodified lines11711811912010012112212312412536 unmodified lines16216316416514516616716816917017 unmodified lines18818919019119219319419519619719819920020120220320420520620720820921021121221339 unmodified lines const wrapped = helper.wrap(text, 39, 0); expect(wrapped).toEqual(text); });
test('when text has line breaks then respect and indent', () => { test('when text has line break then respect and indent', () => { const text = 'term description\nanother line'; const helper = new commander.Help(); const wrapped = helper.wrap(text, 78, 5); expect(wrapped).toEqual('term description\n another line'); });
test('when text has consecutive line breaks then respect and indent', () => { const text = 'term description\n\nanother line'; const helper = new commander.Help(); const wrapped = helper.wrap(text, 78, 5); expect(wrapped).toEqual('term description\n\n another line'); });
test('when text has Windows line break then respect and indent', () => { const text = 'term description\r\nanother line'; const helper = new commander.Help(); const wrapped = helper.wrap(text, 78, 5); expect(wrapped).toEqual('term description\n another line'); });
test('when text has Windows consecutive line breaks then respect and indent', () => { const text = 'term description\r\n\r\nanother line'; const helper = new commander.Help(); const wrapped = helper.wrap(text, 78, 5); expect(wrapped).toEqual('term description\n\n another line'); });
test('when text already formatted with line breaks and indent then do not touch', () => { const text = 'term a '.repeat(25) + '\n ' + 'a '.repeat(25) + 'a'; const helper = new commander.Help(); const wrapped = helper.wrap(text, 78, 5);41 unmodified lines
expect(program.helpInformation()).toBe(expectedOutput); });
test('when long command description then wrap and indent', () => { test('when long subcommand description then wrap and indent', () => { const program = new commander.Command(); program .configureHelp({ helpWidth: 80 }) .option('-x --extra-long-option-switch', 'x')36 unmodified lines
expect(program.helpInformation()).toBe(expectedOutput); });
test('when option descripton preformatted then only add small indent', () => { test('when option description pre-formatted then only add small indent', () => { // #396: leave custom format alone, apart from space-space indent const optionSpec = '-t, --time <HH:MM>'; const program = new commander.Command(); program17 unmodified lines`;
expect(program.helpInformation()).toBe(expectedOutput); });
test('when command description long then wrapped', () => { const program = new commander.Command(); program .configureHelp({ helpWidth: 80 }) .description(`Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor labore eu Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor labore euAfter line break Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor labore eu Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor labore eu`); const expectedOutput = `Usage: [options]
Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor laboreeu Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco temporlabore euAfter line break Do fugiat eiusmod ipsum laboris excepteur pariatur sintullamco tempor labore eu Do fugiat eiusmod ipsum laboris excepteur pariatursint ullamco tempor labore eu
Options: -h, --help display help for command`; expect(program.helpInformation()).toBe(expectedOutput); });});