17

有没有办法将命令提示符(只是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                                         |
----------------------------------------------------------------------
4

4 回答 4

16

服务器线模块

在此处输入图像描述

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

于 2014-07-01T21:51:27.540 回答
3

这是一个简单的解决方案(在 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
);
于 2016-10-25T18:10:53.913 回答
2

好吧,我正在使用查询器来解决这类问题。它已经解决了所有这些问题。它易于使用,并且您有不同的提示类型。

这是文档中的一个小示例:

var inquirer = require('inquirer');
inquirer.prompt([/* Pass your questions in here */]).then(function (answers) {
    // Use user feedback for... whatever!! 
});
于 2016-11-22T15:38:19.757 回答
-1

只需使用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()
于 2012-10-01T12:36:10.413 回答