在Node.js中固定位置命令提示符

有没有办法让一个命令提示符(只是一个question提示,或类似的东西)固定到terminal的底部,并使用Node.jslogging上面的输出。

一个非常糟糕的例子:

 Your favourite food is sushi. Your favourite food is chicken. Your favourite food is okra. --> What is your favourite food? 

所以基本上,我希望用户总是能够input,并在提示符之上input回显。

 interface.question("What is your favourite food?", function(answer) { // output the answer above where this prompt was // and somehow keep this same prompt where is is }); 

我希望使用这个特定的应用程序是一个简单的IRC客户端,我有一个地方供用户input,并输出上面输出的所有输出(用户键入的内容以及其他input的内容)用户正在键入。 下图中的线是虚构的。

 ---------------------------------------------------------------------- | | | | | | | | | | | | | | | Stuff | | Stuff | | Stuff | ---------------------------------------------------------------------- | --> The user can type here | ---------------------------------------------------------------------- 

       

网上收集的解决方案 "在Node.js中固定位置命令提示符"

请参阅https://asciinema.org/a/10583 ,是控制台logging。

终端屏幕

如果提示符有多行(多于4行),或者提示符是中间屏幕并且有多行,则此脚本具有显示问题。

 process.stdout.write("\x1Bc"); var readline = require('readline'), util = require('util'); var rl = readline.createInterface({ input: process.stdin, output: process.stdout, completer: completer }); rl.setPrompt("> ", 2); rl.on("line", function(line) { if (line == "help") { console.log("no command.") } rl.prompt(); }); rl.on('close', function() { return process.exit(1); }); rl.on("SIGINT", function() { rl.clearLine(); rl.question("Confirm exit : ", function(answer) { return (answer.match(/^o(ui)?$/i) || answer.match(/^y(es)?$/i)) ? process.exit(1) : rl.output.write("> "); }); }); rl.prompt(); var fu = function(type, args) { var t = Math.ceil((rl.line.length + 3) / process.stdout.columns); var text = util.format.apply(console, args); rl.output.write("\n\x1B[" + t + "A\x1B[0J"); rl.output.write(text + "\n"); rl.output.write(Array(t).join("\n\x1B[E")); rl._refreshLine(); }; console.log = function() { fu("log", arguments); }; console.warn = function() { fu("warn", arguments); }; console.info = function() { fu("info", arguments); }; console.error = function() { fu("error", arguments); }; console.log(">> Readline : Ok."); function completer(line) { var completions = ["help", "command1", "command2", "login", "check", "ping"]; var hits = completions.filter(function(c) { return c.indexOf(line) == 0; }); if (hits.length == 1) { return [hits, line]; } else { console.log("Suggest :"); var list = "", l = 0, c = "", t = hits.length ? hits : completions; for (var i = 0; i < t.length; i++) { c = t[i].replace(/(\s*)$/g, "") if (list != "") { list += ", "; } if (((list + c).length + 4 - l) > process.stdout.columns) { list += "\n"; l = list.length; } list += c; } console.log(list + "\n"); return [hits, line]; } } //----------------------------- function main() { var ___i=0; setInterval(function () { var num = function () { return Math.floor(Math.random() * 255) + 1; }; console.log(num()+"."+num()+"."+num()+" user connected."); }, 1000); //your code. } setTimeout(function () { main(); }, 10); 

编辑:

修复问题节点-v 6.2.0:

将最后的cmd.output.writereplace为:

 cmd.output.write(Array(t).join("\r\x1B[1E")); 

那么我使用查询器来解决这类问题。它已经解决了所有这些问题。它很容易使用,而且您有不同种类的propmttypes。

这里有一个小例子来形成doc:

 var inquirer = require('inquirer'); inquirer.prompt([/* Pass your questions in here */]).then(function (answers) { // Use user feedback for... whatever!! }); 

只需使用readline核心模块:

 var readline = require('readline'); var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question("What do you think of node.js? ", function(answer) { console.log("Thank you for your valuable feedback:", answer); rl.close(); }); 

这将解决你的问题:

 var readline = require('readline'); var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); var fav_foods = []; var ask_question = function() { rl.question("What is your favourite food? ", function(answer) { fav_foods.push(answer) fav_foods.forEach(function (element) { console.log("Your favourite food is " + element) }) ask_question() }); } ask_question() 

这是一个简单的解决scheme(用macos上的节点v6.4.0testing):

 const readline = require('readline'); const rl = readline.createInterface(process.stdin, process.stdout); // Logs a message keeping prompt on last line function log(message) { readline.cursorTo(process.stdout, 0); console.log(message); rl.prompt(true); } // Testing the solution rl.question('Enter something...:', userInput => { log('You typed ' + userInput); rl.close(); }); log('this should appear above the prompt'); setTimeout( () => log('this should appear above the prompt'), 1000 );