Skip to content
Myr

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

3 files+24 added−22 removed

modified:tests/help.wrap.test.js+5 added-5 removed
modified:typings/index.d.ts+2 added-3 removed
110 unmodified lines
111 /** Calculate the pad width from the maximum term length. */112 padWidth(cmd: Command, helper: Help): number;113114 /**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;119120 /** Generate the built-in help text. */
468 unmodified lines
modified:index.js+17 added-14 removed
243 unmodified lines
244 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 lines
309 );310 };311312 /**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 */322323 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;331332 const leadingStr = str.substr(0, indent);333 const columnText = str.substr(indent);334335 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