有没有办法将命令提示符(只是 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
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()
旧版本:
关于javascript - Node.js 中的固定位置命令提示符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12672193/