繁体   English   中英

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

[英]Fixed position command prompt in Node.js

有没有办法将命令提示符(只是一个question提示或类似的东西)固定到终端的底部,并使用Node.js记录它上面的输出。

一个非常糟糕的例子:

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

基本上,我希望用户始终能够键入,并在输入上方回显输入。

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客户端,我有一个供用户键入的位置,并且输出所有输出(用户键入的内容,以及其他人也键入的内容)用户正在打字。 下图中的线是虚构的。

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

服务器模块

在此输入图像描述

process.stdout.write("\x1Bc")
console.log(Array(process.stdout.rows + 1).join('\n'));

const myRL = require("serverline")

myRL.init()
myRL.getRL().question("What is your favourite food? ", function(answer) {
  console.log(`Your favourite food is ${answer}.`)
});

function main() {
  let i = 0
  setInterval(function() {
    const num = () => Math.floor(Math.random() * 255) + 1
    i++
    console.log(i + " " + num() + "." + num() + "." + num() + " user connected.")
  }, 700)
}
main()

旧版 :

https://stackoverflow.com/posts/24519813/revisions

好吧,我正在使用询问者来解决这类问题..它已经解决了所有这些问题。它很容易使用,你有不同类型的propmt类型。

这里有一个litle示例来形成doc:

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

这是一个简单的解决方案(在macos上使用node v6.4.0进行测试):

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
);

只需使用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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM