Skip to content

Commit

Permalink
repl: improve .help message
Browse files Browse the repository at this point in the history
- Added dots to printed commands.
- Use spaces instead of tabs so there's no misalignment on terminals
  with a tab size other than 4.
- Improved the help text for .editor and .help.
- Automatically indent command help based on the longest command.

PR-URL: nodejs#8519
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Prince John Wesley <[email protected]>
Reviewed-By: Ilkka Myller <[email protected]>
  • Loading branch information
silverwind committed Sep 16, 2016
1 parent 5a17139 commit 39fbb5a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
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);
}, 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

0 comments on commit 39fbb5a

Please sign in to comment.