检测按键和replace控制台中的行

我有一个Node.js脚本,看起来像这样:

var chalk = require("chalk"); var https = require("https"); var express = require("express"); var fs = require("fs") var app = express(); const readline = require("readline"); var keypress = require("keypress"); var process = require("process"); var emitter = require("emitter"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); console.log(' _______'); console.log(' / o \\ .------------------------.'); console.log(' | > | Welcome to chikin! |'); console.log(' \\_______/ | A npm package manager |'); console.log(" | | '------------------------'"); console.log(' ^ ^'); console.log('_____________________________________________\n'); console.log(chalk.bold.green('What would you like to do?') + ' Type ' + chalk.bold.blue('a') + ',' + chalk.bold.blue('b') + ', or ' + chalk.bold.blue('c')); console.log('_________________________________________'); console.log('a. ' + chalk.bold.blue('Install Packages')); console.log('b. ' + chalk.bold.blue('Update Packages')); console.log('c. ' + chalk.bold.blue('Uninstall Packages\n')); var du = ''; var valid = false; rl.question('?> ', function(answer) { var areValid = ['a', 'b', 'c', 'A', 'B', 'C', 'a.', 'b.', 'c.', 'A.', 'B.', 'C.']; if (answer in areValid) { valid = true; } }) 

此脚本试图检测使用是否希望A. 安装软件包 ,B. 更新软件包或C. 卸载软件包 。 使用rl.question()方法不起作用,因为当我运行它光标只是闪烁,不让我input任何东西。 所以,我想我可以通过按键来检测它,而不是使用提示。 虽然我不知道如何做到这一点。 我也认为它可以取代控制台的最后一行到最新的有效答案。 有没有办法做到这一点?

       

网上收集的解决方案 "检测按键和replace控制台中的行"

使用readline ,应该保持用户input状态始终处于locking状态(已paused

我希望这有助于,祝你好运:

 const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const LINE = ''; const LINE2 = '\n'; const PROMPT = '?> '; rl.setPrompt(PROMPT); // prevent user input by default: lock the state rl.pause(); var steps = { a: function() { console.log('You selected "Install Packages"', LINE); var trials = 0; ask('Enter package name(s) (separated with , ) you want to install:', function(answer) { var packages = answer.toLowerCase().split(/[^a-z_\-]/).filter(function(pkg) { return pkg.length; }); if (!packages.length) { if (++trials > 2) { console.log('Are you kidding me?'); process.exit(); } return false; // ask again } confirm('Packages below will be installed, are you confirm? ( y/Y or not )' + LINE2 + ' --> ' + packages.join(', '), function(answer) { console.log(LINE2); if (!answer) { steps.a(); // start this step again return; } console.log('You confirmed but', LINE); steps.c(1); }); }); }, b: function() { console.log('You selected "Update Packages"', LINE); console.log('Now, please enter the package name you want to update: '); input('acme-package', function(pkg) { if ('acme-package' !== pkg) { console.log('Really!?', LINE); process.exit(); } // else steps.c(1); }); }, c: function(internal) { if (!internal) { console.log('You selected "Uninstall Packages"', LINE); } console.log("Sorry, I don't know what to do now.", LINE, 'Program will exit.'); process.exit(); } }; /** * Test */ console.log('What would you like to do?', LINE2); console.log('a. Install Packages', LINE); console.log('b. Update Packages', LINE); console.log('c. Uninstall Packages', LINE2); ask('What would you like to do?', function(answer) { answer = answer.toLowerCase(); if (!steps[answer]) { return false; } steps[answer](); // first step: after this point you can handle other events in your 'steps' }); /** * Functions */ function confirm(message, callback) { console.log(message, LINE); // print confirmation message rl.pause(); // lock: ensure rl.question(PROMPT, function(answer) // unlock { rl.pause(); // lock again answer = answer.trim().toLowerCase(); if (!answer.length) { confirm(message, callback); // again } else { callback(/^y(e(s|p|ah?))?$/.test(answer)); // y|yes|yep|yea|yeah } }); } function ask(question, placeholder, callback) { if (typeof placeholder === 'function') { callback = placeholder; placeholder = null; } console.log(question, LINE); // print question message rl.pause(); // lock rl.question(PROMPT, function(answer) // unlock { rl.pause(); // lock again var response = callback(answer.trim()); if (response) { ask(typeof response === 'string' ? response : question, placeholder, callback); // again } }); if (placeholder) { rl.write(placeholder); } } function input(placeholder, callback) { if (typeof placeholder === 'function') { callback = placeholder; placeholder = null; } rl.pause(); // lock rl.question(LINE + PROMPT, function(answer) // unlock { rl.pause(); // lock again var answer = answer.trim(); if (!answer || callback(answer) === false) { input(callback); } }); if (placeholder) { rl.write(placeholder); } } process.on('exit', function() { rl.pause(); // always lock before 'console.log' console.log(LINE, 'bye!'); }); 

奖金:

 const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); var pr = '+'; setInterval(function() { rl.setPrompt(pr += '+'); rl.prompt(); rl.pause(); if (pr.length > 20) { process.exit(); } }, 100); 

第三方模块如何处理? 像这样或那样