我想隐藏密码输入。我在stackoverflow中看到许多答案,但如果按Backspace键,则无法验证值。条件返回false。
我尝试了几种解决方案来覆盖该函数,但是如果按退格键,则会出现缓冲区问题,我看到了不可见的字符\b。
\b
我按:“ A”,退格键,“ B”,我的缓冲区中有:“ \ u0041 \ u0008 \ u0042”(toString()=’A \ bB’),而不是“ B”。
我有 :
var readline = require('readline'); var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question("password : ", function(password) { console.log("Your password : " + password); });
覆盖应用程序的readline接口的_writeToOutput:https : //github.com/nodejs/node/blob/v9.5.0/lib/readline.js#L291
要隐藏密码输入,可以使用:
按下触摸时此解决方案具有动画:
password : [-=] password : [=-]
代码 :
var readline = require('readline'); var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.stdoutMuted = true; rl.query = "Password : "; rl.question(rl.query, function(password) { console.log('\nPassword is ' + password); rl.close(); }); rl._writeToOutput = function _writeToOutput(stringToWrite) { if (rl.stdoutMuted) rl.output.write("\x1B[2K\x1B[200D"+rl.query+"["+((rl.line.length%2==1)?"=-":"-=")+"]"); else rl.output.write(stringToWrite); };
此序列“ \ x1B [2K \ x1BD”使用两个转义序列:
要了解更多信息,请阅读:http : //ascii-table.com/ansi-escape-sequences-vt-100.php
var readline = require('readline'); var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.stdoutMuted = true; rl.question('Password: ', function(password) { console.log('\nPassword is ' + password); rl.close(); }); rl._writeToOutput = function _writeToOutput(stringToWrite) { if (rl.stdoutMuted) rl.output.write("*"); else rl.output.write(stringToWrite); };
rl.history = rl.history.slice(1);