cli: more flexible width when printing --help

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Anna Henningsen 2018-09-01 00:24:48 +02:00 committed by Daniel Bevenius
parent 65f0323b07
commit 089c0d1025

View File

@ -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)' });