Rework wrap to have simpler usage pattern (#1395)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Gee · committed by GitHub · parent ed7f13e · Browse files · 3 files·+24 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+24 added−22 removed
- tests:+5 added−5 removed
- other:+19 added−17 removed
Provenance
- Commit
4555881- Tree
6575bfa- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
ed7f13e
3 files changed+24 added−22 removed
modified:tests/help.wrap.test.js+5 added-5 removed
33 unmodified lines3435363738383940414041424344454546474748495051113 unmodified linesExpand all33 unmodified lines expect(wrapped).toEqual(text); });
test('when text has line breaks then respect and indent', () => { const text = 'foo\nbar'; const text = 'term description\nanother line'; const helper = new commander.Help(); const wrapped = helper.wrap(text, 50, 3); expect(wrapped).toEqual('foo\n bar'); const wrapped = helper.wrap(text, 78, 5); expect(wrapped).toEqual('term description\n another line'); });
test('when text already formatted with line breaks and indent then do not touch', () => { const text = 'a '.repeat(25) + '\n ' + 'a '.repeat(25) + 'a'; const text = 'term a '.repeat(25) + '\n ' + 'a '.repeat(25) + 'a'; const helper = new commander.Help(); const wrapped = helper.wrap(text, 39, 0); const wrapped = helper.wrap(text, 78, 5); expect(wrapped).toEqual(text); });});
113 unmodified linesExpand allmodified:typings/index.d.ts+2 added-3 removed
110 unmodified lines111 /** Calculate the pad width from the maximum term length. */112 padWidth(cmd: Command, helper: Help): number;113 114 /**115- * Optionally wrap the given str to a max width of width characters per line116- * while indenting with indent spaces. Do not wrap if insufficient width or117- * string is manually formatted.115+ * Wrap the given string to width characters per line, with lines after the first indented.116+ * Do not wrap if insufficient room for wrapping, or string is manually formatted.117 */118 wrap(str: string, width: number, indent: number): string;119 120 /** Generate the built-in help text. */468 unmodified linesmodified:index.js+17 added-14 removed
243 unmodified lines244 formatHelp(cmd, helper) {245 const termWidth = helper.padWidth(cmd, helper);246 const columns = helper.columns || 80;247 const itemIndentWidth = 2;248- const itemSeparatorWidth = 2;249- // itemIndent term itemSeparator description250- const descriptionWidth = columns - termWidth - itemIndentWidth - itemSeparatorWidth;248+ const itemSeparatorWidth = 2; // between term and description249 function formatItem(term, description) {250 if (description) {253- return term.padEnd(termWidth + itemSeparatorWidth) + helper.wrap(description, descriptionWidth, termWidth + itemSeparatorWidth);251+ const fullText = `${term.padEnd(termWidth + itemSeparatorWidth)}${description}`;252+ return helper.wrap(fullText, columns - itemIndentWidth, termWidth + itemSeparatorWidth);253 }254 return term;255 };256 function formatList(textArray) {52 unmodified lines309 );310 };311 312 /**314- * Optionally wrap the given str to a max width of width characters per line315- * while indenting with indent spaces. Do not wrap if insufficient width or316- * string is manually formatted.313+ * Wrap the given string to width characters per line, with lines after the first indented.314+ * Do not wrap if insufficient room for wrapping, or string is manually formatted.315 *316 * @param {string} str317 * @param {number} width318 * @param {number} indent319 * @return {string}320+ *321 */322 323 wrap(str, width, indent) {324 // Detect manually wrapped and indented strings by searching for line breaks325 // followed by multiple spaces/tabs.326 if (str.match(/[\n]\s+/)) return str;328- // Do not wrap to narrow columns (or can end up with a word per line).329- const minWidth = 40;330- if (width < minWidth) return str;327+ // Do not wrap if not enough room for a wrapped column of text (as could end up with a word per line).328+ const columnWidth = width - indent;329+ const minColumnWidth = 40;330+ if (columnWidth < minColumnWidth) return str;331 332+ const leadingStr = str.substr(0, indent);333+ const columnText = str.substr(indent);334+335 const indentString = ' '.repeat(indent);333- const regex = new RegExp('.{1,' + (width - 1) + '}([\\s\u200B]|$)|[^\\s\u200B]+?([\\s\u200B]|$)', 'g');334- const lines = str.match(regex) || [];335- return lines.map((line, i) => {336+ const regex = new RegExp('.{1,' + (columnWidth - 1) + '}([\\s\u200B]|$)|[^\\s\u200B]+?([\\s\u200B]|$)', 'g');337+ const lines = columnText.match(regex) || [];338+ return leadingStr + lines.map((line, i) => {339 if (line.slice(-1) === '\n') {340 line = line.slice(0, line.length - 1);341 }339- return ((i > 0 && indent) ? indentString : '') + line.trimRight();342+ return ((i > 0) ? indentString : '') + line.trimRight();343 }).join('\n');344 }345 }346 1816 unmodified lines