Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: be more flexible about width when printing --help #22637

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/internal/print_help.js
Original file line number Diff line number Diff line change
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