From 39fbb5adf54f2d4e700b92abc70516ebc09b8fd7 Mon Sep 17 00:00:00 2001 From: Roman Reiss Date: Tue, 13 Sep 2016 21:46:57 +0200 Subject: [PATCH] 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 Reviewed-By: James M Snell Reviewed-By: Prince John Wesley Reviewed-By: Ilkka Myller --- lib/repl.js | 17 +++++++++++------ test/parallel/test-repl-definecommand.js | 4 ++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index e93b7102665..9b3b197f955 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -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; diff --git a/test/parallel/test-repl-definecommand.js b/test/parallel/test-repl-definecommand.js index c0e1b3269a3..6c0c75bca8d 100644 --- a/test/parallel/test-repl-definecommand.js +++ b/test/parallel/test-repl-definecommand.js @@ -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');