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', { repl.defineCommand('help', {
help: 'Show repl options', help: 'Print this help message',
action: function() { action: function() {
var self = this; const names = Object.keys(this.commands).sort();
Object.keys(this.commands).sort().forEach(function(name) { const longestNameLength = names.reduce((max, name) => {
var cmd = self.commands[name]; return Math.max(max, name.length);
self.outputStream.write(name + '\t' + (cmd.help || '') + '\n'); }, 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(); this.displayPrompt();
} }
@ -1308,7 +1313,7 @@ function defineDefaultCommands(repl) {
}); });
repl.defineCommand('editor', { repl.defineCommand('editor', {
help: 'Entering editor mode (^D to finish, ^C to cancel)', help: 'Enter editor mode',
action() { action() {
if (!this.terminal) return; if (!this.terminal) return;
this.editorMode = true; this.editorMode = true;

View File

@ -35,8 +35,8 @@ r.defineCommand('say2', function() {
}); });
inputStream.write('.help\n'); inputStream.write('.help\n');
assert(/\nsay1\thelp for say1\n/.test(output), 'help for say1 not present'); assert(/\n.say1 help for say1\n/.test(output), 'help for say1 not present');
assert(/\nsay2\t\n/.test(output), 'help for say2 not present'); assert(/\n.say2\n/.test(output), 'help for say2 not present');
inputStream.write('.say1 node developer\n'); inputStream.write('.say1 node developer\n');
assert(/> hello node developer/.test(output), 'say1 outputted incorrectly'); assert(/> hello node developer/.test(output), 'say1 outputted incorrectly');
inputStream.write('.say2 node developer\n'); inputStream.write('.say2 node developer\n');