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

标签 javascript node.js terminal command-line-interface

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

最佳答案

Serverline module

enter image description 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

关于javascript - Node.js 中的固定位置命令提示符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12672193/

相关文章:

Javascript:单击提交按钮时如何将隐藏的输入标记附加到表单中?

linux - 捕获过去的终端消息/ session 文本

ruby - 如何在 Ruby 中获取终端窗口的宽度

javascript - 具有 "computed"URL 的 anchor

javascript - 展开维基百科类别树的所有类别

javascript - 如何在 node.js 上用\r\n 替换字符串?

javascript - 上传和显示图像 - ReactJs、Node.js

linux - 尝试运行 make 命令时出错

JavaScript 正则表达式 - 尝试匹配 ( 5', 78' , 8 0')(90' +2') 中的所有数字,前面不带 +

node.js - 使用单个图像读取/流进行多个调整大小