From 43092ebfa218dc1dfa4081d12fe0b615a9794643 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 1 Sep 2018 00:24:48 +0200 Subject: [PATCH] cli: more flexible width when printing `--help` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/22637 Reviewed-By: Michaƫl Zasso Reviewed-By: Ruben Bridgewater Reviewed-By: Trivikram Kamat Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- lib/internal/print_help.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/internal/print_help.js b/lib/internal/print_help.js index 9d14671973402e..00c8330daaaa14 100644 --- a/lib/internal/print_help.js +++ b/lib/internal/print_help.js @@ -69,6 +69,7 @@ function getArgDescription(type) { function format({ options, aliases = new Map(), firstColumn, secondColumn }) { let text = ''; + let maxFirstColumnUsed = 0; for (const [ name, { helpText, type, value } @@ -106,6 +107,7 @@ function format({ options, aliases = new Map(), firstColumn, secondColumn }) { } text += displayName; + maxFirstColumnUsed = Math.max(maxFirstColumnUsed, displayName.length); if (displayName.length >= firstColumn) text += '\n' + ' '.repeat(firstColumn); else @@ -115,16 +117,26 @@ function format({ options, aliases = new Map(), firstColumn, secondColumn }) { firstColumn).trimLeft() + '\n'; } + if (maxFirstColumnUsed < firstColumn - 4) { + // If we have more than 4 blank gap spaces, reduce first column width. + return format({ + options, + aliases, + firstColumn: maxFirstColumnUsed + 2, + secondColumn + }); + } + return text; } function print(stream) { const { options, aliases } = getOptions(); - // TODO(addaleax): Allow a bit of expansion depending on `stream.columns` - // if it is set. - const firstColumn = 28; - const secondColumn = 40; + // Use 75 % of the available width, and at least 70 characters. + const width = Math.max(70, (stream.columns || 0) * 0.75); + const firstColumn = Math.floor(width * 0.4); + const secondColumn = Math.floor(width * 0.57); options.set('-', { helpText: 'script read from stdin (default; ' + 'interactive mode if a tty)' });