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 }) { function format({ options, aliases = new Map(), firstColumn, secondColumn }) {
let text = ''; let text = '';
let maxFirstColumnUsed = 0;
for (const [ for (const [
name, { helpText, type, value } name, { helpText, type, value }
@ -106,6 +107,7 @@ function format({ options, aliases = new Map(), firstColumn, secondColumn }) {
} }
text += displayName; text += displayName;
maxFirstColumnUsed = Math.max(maxFirstColumnUsed, displayName.length);
if (displayName.length >= firstColumn) if (displayName.length >= firstColumn)
text += '\n' + ' '.repeat(firstColumn); text += '\n' + ' '.repeat(firstColumn);
else else
@ -115,16 +117,26 @@ function format({ options, aliases = new Map(), firstColumn, secondColumn }) {
firstColumn).trimLeft() + '\n'; 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; return text;
} }
function print(stream) { function print(stream) {
const { options, aliases } = getOptions(); const { options, aliases } = getOptions();
// TODO(addaleax): Allow a bit of expansion depending on `stream.columns` // Use 75 % of the available width, and at least 70 characters.
// if it is set. const width = Math.max(70, (stream.columns || 0) * 0.75);
const firstColumn = 28; const firstColumn = Math.floor(width * 0.4);
const secondColumn = 40; const secondColumn = Math.floor(width * 0.57);
options.set('-', { helpText: 'script read from stdin (default; ' + options.set('-', { helpText: 'script read from stdin (default; ' +
'interactive mode if a tty)' }); 'interactive mode if a tty)' });