Skip to content

Commit

Permalink
cli: more flexible width when printing --help
Browse files Browse the repository at this point in the history
PR-URL: #22637
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
addaleax authored and targos committed Sep 5, 2018
1 parent dbb8f37 commit 43092eb
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/internal/print_help.js
Expand Up @@ -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 }
Expand Down Expand Up @@ -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
Expand All @@ -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)' });
Expand Down

0 comments on commit 43092eb

Please sign in to comment.