Permalink
Browse files

Added mask support to `Command#password()`

  • Loading branch information...
1 parent 3bbbb2a commit 0a2139695407822676685f14159fcb634ea03cb6 Tj Holowaychuk committed Aug 15, 2011
Showing with 26 additions and 6 deletions.
  1. +6 −3 examples/password
  2. +20 −3 lib/commander.js
View
@@ -8,8 +8,11 @@ var program = require('../');
program.password('Password: ', function(pass){
console.log('got "%s"', pass);
- program.prompt('Username: ', function(name){
- console.log('got "%s"', name.trim());
- process.stdin.destroy();
+ program.password('Password: ', '*', function(pass){
+ console.log('got "%s"', pass);
+ program.password('Password: ', '-', function(pass){
+ console.log('got "%s"', pass);
+ process.stdin.destroy();
+ });
});
});
View
@@ -719,7 +719,10 @@ Command.prototype.prompt = function(str, fn){
};
/**
- * Prompt for password with `str` and callback `fn(val)`.
+ * Prompt for password with `str`, `mask` char and callback `fn(val)`.
+ *
+ * The mask string defaults to '', aka no output is
+ * written while typing, you may want to use "*" etc.
*
* Examples:
*
@@ -728,15 +731,29 @@ Command.prototype.prompt = function(str, fn){
* process.stdin.destroy();
* });
*
+ * program.password('Password: ', '*', function(pass){
+ * console.log('got "%s"', pass);
+ * process.stdin.destroy();
+ * });
+ *
* @param {String} str
+ * @param {String} mask
* @param {Function} fn
* @api public
*/
-Command.prototype.password = function(str, fn){
+Command.prototype.password = function(str, mask, fn){
+ // default mask
+ if ('function' == typeof mask) {
+ fn = mask;
+ mask = '';
+ }
+
tty.setRawMode(true);
var buf = '';
process.stdout.write(str);
+
+ // keypress
process.stdin.on('keypress', function(c, key){
if ('enter' == key.name) {
console.log();
@@ -751,7 +768,7 @@ Command.prototype.password = function(str, fn){
process.exit();
}
- process.stdout.write('*');
+ process.stdout.write(mask);
buf += c;
}).resume();
};

0 comments on commit 0a21396

Please sign in to comment.