Skip to content

Commit 43092eb

Browse files
addaleaxtargos
authored andcommitted
cli: more flexible width when printing --help
PR-URL: #22637 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent dbb8f37 commit 43092eb

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

lib/internal/print_help.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ function getArgDescription(type) {
6969

7070
function format({ options, aliases = new Map(), firstColumn, secondColumn }) {
7171
let text = '';
72+
let maxFirstColumnUsed = 0;
7273

7374
for (const [
7475
name, { helpText, type, value }
@@ -106,6 +107,7 @@ function format({ options, aliases = new Map(), firstColumn, secondColumn }) {
106107
}
107108

108109
text += displayName;
110+
maxFirstColumnUsed = Math.max(maxFirstColumnUsed, displayName.length);
109111
if (displayName.length >= firstColumn)
110112
text += '\n' + ' '.repeat(firstColumn);
111113
else
@@ -115,16 +117,26 @@ function format({ options, aliases = new Map(), firstColumn, secondColumn }) {
115117
firstColumn).trimLeft() + '\n';
116118
}
117119

120+
if (maxFirstColumnUsed < firstColumn - 4) {
121+
// If we have more than 4 blank gap spaces, reduce first column width.
122+
return format({
123+
options,
124+
aliases,
125+
firstColumn: maxFirstColumnUsed + 2,
126+
secondColumn
127+
});
128+
}
129+
118130
return text;
119131
}
120132

121133
function print(stream) {
122134
const { options, aliases } = getOptions();
123135

124-
// TODO(addaleax): Allow a bit of expansion depending on `stream.columns`
125-
// if it is set.
126-
const firstColumn = 28;
127-
const secondColumn = 40;
136+
// Use 75 % of the available width, and at least 70 characters.
137+
const width = Math.max(70, (stream.columns || 0) * 0.75);
138+
const firstColumn = Math.floor(width * 0.4);
139+
const secondColumn = Math.floor(width * 0.57);
128140

129141
options.set('-', { helpText: 'script read from stdin (default; ' +
130142
'interactive mode if a tty)' });

0 commit comments

Comments
 (0)