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

2024-02-13

有没有办法让命令提示符(只是一个question http://nodejs.org/api/readline.html#readline_rl_question_query_callback提示符或类似的东西)固定在终端的底部,并使用它来记录其上方的输出Node.js http://nodejs.org/.

一个非常糟糕的例子:

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

服务器线模块 https://www.npmjs.com/package/serverline

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 https://stackoverflow.com/posts/24519813/revisions

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Node.js 中固定位置命令提示符 的相关文章

随机推荐