Skip to content

repl: improve .help #8519

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
17 changes: 11 additions & 6 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1257,12 +1257,17 @@ function defineDefaultCommands(repl) {
});

repl.defineCommand('help', {
help: 'Show repl options',
help: 'Print this help message',
action: function() {
var self = this;
Object.keys(this.commands).sort().forEach(function(name) {
var cmd = self.commands[name];
self.outputStream.write(name + '\t' + (cmd.help || '') + '\n');
const names = Object.keys(this.commands).sort();
const longestNameLength = names.reduce((max, name) => {
return Math.max(max, name.length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@silverwind (max, name) => Math.max(max, name.length) ? is it against style guide?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not, but it won't fit on 80 chars so I went with the longer form.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not named function? Feel free to ignore it

const maxFun = (max, name) => Math.max(max, name.length);
const longestNameLength = names.reduce(maxFun, 0);

or

const nameLengthList = names.map((n) => n.length);
const longestNameLength = nameLengthList.reduce(Math.max, 0);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, I find that these reduce readability as compared to what I have now.

}, 0);
names.forEach((name) => {
const cmd = this.commands[name];
const spaces = ' '.repeat(longestNameLength - name.length + 3);
const line = '.' + name + (cmd.help ? spaces + cmd.help : '') + '\n';
this.outputStream.write(line);
});
this.displayPrompt();
}
Expand Down Expand Up @@ -1308,7 +1313,7 @@ function defineDefaultCommands(repl) {
});

repl.defineCommand('editor', {
help: 'Entering editor mode (^D to finish, ^C to cancel)',
help: 'Enter editor mode',
action() {
if (!this.terminal) return;
this.editorMode = true;
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-repl-definecommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ r.defineCommand('say2', function() {
});

inputStream.write('.help\n');
assert(/\nsay1\thelp for say1\n/.test(output), 'help for say1 not present');
assert(/\nsay2\t\n/.test(output), 'help for say2 not present');
assert(/\n.say1 help for say1\n/.test(output), 'help for say1 not present');
assert(/\n.say2\n/.test(output), 'help for say2 not present');
inputStream.write('.say1 node developer\n');
assert(/> hello node developer/.test(output), 'say1 outputted incorrectly');
inputStream.write('.say2 node developer\n');
Expand Down