Skip to content

Commit

Permalink
fix(cli): better command line helper text content and format
Browse files Browse the repository at this point in the history
Summary:
## Before
 {F1968819378}

## After
 {F1968819329}

Differential Revision: D66548359

fbshipit-source-id: b54289477f782dd64e003e3e7a808f6281f18728
  • Loading branch information
JacksonGL authored and facebook-github-bot committed Dec 3, 2024
1 parent 4f15d22 commit c2ba788
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/cli/src/commands/helper/HelperCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ export default class HelperCommand extends BaseCommand {
const cmd = docUtils.generateExampleCommand(name, example, {
descriptionAsBashComment: false,
});
let msg = `${indent}${cmd}`;
msg += `\n${indent}${desc}`;
let msg = docUtils.indentText(cmd, indent);
msg += `\n${docUtils.indentText(desc, indent)}`;

if (options.printOptions && cmdDoc.length > 0) {
const cmdDocBlock = alignTextInBlock(cmdDoc, {
Expand Down
66 changes: 66 additions & 0 deletions packages/cli/src/commands/helper/lib/DocUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,72 @@ function exampleFromCliOptionString(
return `memlab ${command} ${cliExample.trim()}`;
}

function getCLIWidth() {
return process.stdout.columns || 80; // Default to 80 if undefined
}

function indentText(description: string, indent: string): string {
const cliWidth = getCLIWidth();
const availableWidth = cliWidth - indent.length;
const lines: Array<string> = [];

// Split the description by \n to handle existing line breaks
const descriptionLines = description.split('\n');

descriptionLines.forEach(descLine => {
const words = descLine.split(/\s+/);
let line = '';
for (let i = 0; i < words.length; i++) {
const word = words[i];
if (line.length === 0) {
// Start a new line with the word
if (word.length > availableWidth) {
// The word itself is longer than the available width
// Split the word
let start = 0;
while (start < word.length) {
const part = word.substring(start, start + availableWidth);
lines.push(indent + part);
start += availableWidth;
}
line = '';
} else {
line = word;
}
} else {
const potentialLine = line + ' ' + word;
if (potentialLine.length <= availableWidth) {
line = potentialLine;
} else {
// Line is full, push it and start new line
lines.push(indent + line);
if (word.length > availableWidth) {
// The word itself is longer than the available width
// Split the word
let start = 0;
while (start < word.length) {
const part = word.substring(start, start + availableWidth);
lines.push(indent + part);
start += availableWidth;
}
line = '';
} else {
line = word;
}
}
}
}
// Push the last line if any
if (line.length > 0) {
lines.push(indent + line);
}
});

// Join all lines with \n
return lines.join('\n');
}

export default {
indentText,
generateExampleCommand,
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ export default class HeapAnalysisOutputOption extends BaseOption {
}

getDescription(): string {
return 'specify output format of the analysis (defaults to text)';
const options = this.getExampleValues()
.map(v => `'${v}'`)
.join(', ');
return (
'specify output format of the analysis ' +
`(available options: ${options}; defaults to 'text')`
);
}

getExampleValues(): string[] {
Expand Down
14 changes: 7 additions & 7 deletions website/docs/cli/CLI-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ memlab analyze collections-with-stale

**Options**:
* **`--snapshot`**: set file path of the heap snapshot under analysis
* **`--output`**: specify output format of the analysis (defaults to text)
* **`--output`**: specify output format of the analysis (available options: 'text', 'json'; defaults to 'text')
* **`--help`**, **`-h`**: print helper text
* **`--verbose`**, **`-v`**: show more details
* **`--sc`**: set to continuous test mode
Expand All @@ -232,7 +232,7 @@ memlab analyze detached-DOM

**Options**:
* **`--snapshot`**: set file path of the heap snapshot under analysis
* **`--output`**: specify output format of the analysis (defaults to text)
* **`--output`**: specify output format of the analysis (available options: 'text', 'json'; defaults to 'text')
* **`--help`**, **`-h`**: print helper text
* **`--verbose`**, **`-v`**: show more details
* **`--sc`**: set to continuous test mode
Expand All @@ -249,7 +249,7 @@ memlab analyze global-variable

**Options**:
* **`--snapshot`**: set file path of the heap snapshot under analysis
* **`--output`**: specify output format of the analysis (defaults to text)
* **`--output`**: specify output format of the analysis (available options: 'text', 'json'; defaults to 'text')
* **`--help`**, **`-h`**: print helper text
* **`--verbose`**, **`-v`**: show more details
* **`--sc`**: set to continuous test mode
Expand All @@ -267,7 +267,7 @@ memlab analyze object
**Options**:
* **`--snapshot`**: set file path of the heap snapshot under analysis
* **`--node-id`**: set heap node ID
* **`--output`**: specify output format of the analysis (defaults to text)
* **`--output`**: specify output format of the analysis (available options: 'text', 'json'; defaults to 'text')
* **`--help`**, **`-h`**: print helper text
* **`--verbose`**, **`-v`**: show more details
* **`--sc`**: set to continuous test mode
Expand All @@ -284,7 +284,7 @@ memlab analyze object-fanout

**Options**:
* **`--snapshot`**: set file path of the heap snapshot under analysis
* **`--output`**: specify output format of the analysis (defaults to text)
* **`--output`**: specify output format of the analysis (available options: 'text', 'json'; defaults to 'text')
* **`--help`**, **`-h`**: print helper text
* **`--verbose`**, **`-v`**: show more details
* **`--sc`**: set to continuous test mode
Expand Down Expand Up @@ -333,7 +333,7 @@ memlab analyze object-size

**Options**:
* **`--snapshot`**: set file path of the heap snapshot under analysis
* **`--output`**: specify output format of the analysis (defaults to text)
* **`--output`**: specify output format of the analysis (available options: 'text', 'json'; defaults to 'text')
* **`--help`**, **`-h`**: print helper text
* **`--verbose`**, **`-v`**: show more details
* **`--sc`**: set to continuous test mode
Expand Down Expand Up @@ -414,7 +414,7 @@ memlab analyze unmounted-fiber-node

**Options**:
* **`--snapshot`**: set file path of the heap snapshot under analysis
* **`--output`**: specify output format of the analysis (defaults to text)
* **`--output`**: specify output format of the analysis (available options: 'text', 'json'; defaults to 'text')
* **`--help`**, **`-h`**: print helper text
* **`--verbose`**, **`-v`**: show more details
* **`--sc`**: set to continuous test mode
Expand Down

0 comments on commit c2ba788

Please sign in to comment.