repl: improve .help message

- 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: https://github.com/nodejs/node/pull/8519
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com>
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
This commit is contained in:
Roman Reiss 2016-09-13 21:46:57 +02:00
parent 5a171398f4
commit 39fbb5adf5
No known key found for this signature in database
GPG Key ID: 2E62B41C93869443
2 changed files with 13 additions and 8 deletions

View File

@ -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();
}
@ -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;

View File

@ -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');