Skip to content
Myr

Added mask support to `Command#password()`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 3bbbb2a · Browse files · 2 files+26 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+26 added−6 removed

  • other:+26 added−6 removed

2 files+26 added−6 removed

modified:examples/password+6 added-3 removed
6 unmodified lines
7var program = require('../');89program.password('Password: ', function(pass){10 console.log('got "%s"', pass);11 program.prompt('Username: ', function(name){12 console.log('got "%s"', name.trim());13 process.stdin.destroy();11 program.password('Password: ', '*', function(pass){12 console.log('got "%s"', pass);13 program.password('Password: ', '-', function(pass){14 console.log('got "%s"', pass);15 process.stdin.destroy();16 });17 });18});
modified:lib/commander.js+20 added-3 removed
717 unmodified lines
718 this.promptMultiLine(str, fn);719};720721/**722 * Prompt for password with `str` and callback `fn(val)`.722 * Prompt for password with `str`, `mask` char and callback `fn(val)`.723 *724 * The mask string defaults to '', aka no output is725 * written while typing, you may want to use "*" etc.726 *727 * Examples:728 *729 * program.password('Password: ', function(pass){730 * console.log('got "%s"', pass);731 * process.stdin.destroy();732 * });733 *734 * program.password('Password: ', '*', function(pass){735 * console.log('got "%s"', pass);736 * process.stdin.destroy();737 * });738 *739 * @param {String} str740 * @param {String} mask741 * @param {Function} fn742 * @api public743 */744736Command.prototype.password = function(str, fn){745Command.prototype.password = function(str, mask, fn){746 // default mask747 if ('function' == typeof mask) {748 fn = mask;749 mask = '';750 }751752 tty.setRawMode(true);753 var buf = '';754 process.stdout.write(str);755756 // keypress757 process.stdin.on('keypress', function(c, key){758 if ('enter' == key.name) {759 console.log();760 process.stdin.removeAllListeners('keypress');
6 unmodified lines
767 console.log('%s', buf);768 process.exit();769 }770754 process.stdout.write('*');771 process.stdout.write(mask);772 buf += c;773 }).resume();774};775
90 unmodified lines