Skip to content

Commit 39fbb5a

Browse files
committed
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: #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]>
1 parent 5a17139 commit 39fbb5a

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

lib/repl.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,12 +1257,17 @@ function defineDefaultCommands(repl) {
12571257
});
12581258

12591259
repl.defineCommand('help', {
1260-
help: 'Show repl options',
1260+
help: 'Print this help message',
12611261
action: function() {
1262-
var self = this;
1263-
Object.keys(this.commands).sort().forEach(function(name) {
1264-
var cmd = self.commands[name];
1265-
self.outputStream.write(name + '\t' + (cmd.help || '') + '\n');
1262+
const names = Object.keys(this.commands).sort();
1263+
const longestNameLength = names.reduce((max, name) => {
1264+
return Math.max(max, name.length);
1265+
}, 0);
1266+
names.forEach((name) => {
1267+
const cmd = this.commands[name];
1268+
const spaces = ' '.repeat(longestNameLength - name.length + 3);
1269+
const line = '.' + name + (cmd.help ? spaces + cmd.help : '') + '\n';
1270+
this.outputStream.write(line);
12661271
});
12671272
this.displayPrompt();
12681273
}
@@ -1308,7 +1313,7 @@ function defineDefaultCommands(repl) {
13081313
});
13091314

13101315
repl.defineCommand('editor', {
1311-
help: 'Entering editor mode (^D to finish, ^C to cancel)',
1316+
help: 'Enter editor mode',
13121317
action() {
13131318
if (!this.terminal) return;
13141319
this.editorMode = true;

test/parallel/test-repl-definecommand.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ r.defineCommand('say2', function() {
3535
});
3636

3737
inputStream.write('.help\n');
38-
assert(/\nsay1\thelp for say1\n/.test(output), 'help for say1 not present');
39-
assert(/\nsay2\t\n/.test(output), 'help for say2 not present');
38+
assert(/\n.say1 help for say1\n/.test(output), 'help for say1 not present');
39+
assert(/\n.say2\n/.test(output), 'help for say2 not present');
4040
inputStream.write('.say1 node developer\n');
4141
assert(/> hello node developer/.test(output), 'say1 outputted incorrectly');
4242
inputStream.write('.say2 node developer\n');

0 commit comments

Comments
 (0)