node.js stdout clearline() 和 cursorTo() 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34570452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
node.js stdout clearline() and cursorTo() functions
提问by trogne
From a node.js tutorial, I see those two process.stdout functions :
从 node.js 教程中,我看到了这两个 process.stdout 函数:
process.stdout.clearLine();
process.stdout.cursorTo(0);
But I'm using a more recent node.js version (4.2.4), and those functions do not exist. I get process.stdout.clearLine is not a functionand process.stdout.cursorTo is not a function.
但是我使用的是较新的 node.js 版本 (4.2.4),并且这些功能不存在。我得到process.stdout.clearLine is not a function和process.stdout.cursorTo is not a function。
What is the equivalent of clearLine and cursorTo on node.js version 4.2.4 ?
node.js 版本 4.2.4 上 clearLine 和 cursorTo 的等价物是什么?
EDIT :
编辑 :
Those are not working either :
那些也不起作用:
process.readline.clearLine();
process.readline.cursorTo(0);
function writeWaitingPercent(p) {
process.readline.clearLine();
process.readline.cursorTo(0);
process.stdout.write(`waiting ... ${p}%`);
}
I get Cannot read property 'clearLine' of undefined
我得到 Cannot read property 'clearLine' of undefined
回答by trogne
This is the solution :
这是解决方案:
First, require readline :
首先,需要 readline :
var readline = require('readline');
Then, use cursorTo like this :
然后,像这样使用 cursorTo :
function writeWaitingPercent(p) {
//readline.clearLine(process.stdout);
readline.cursorTo(process.stdout, 0);
process.stdout.write(`waiting ... ${p}%`);
}
I've commented clearLine, since it's useless in my case (cursorTo move back the cursor to the start)
我评论了 clearLine,因为它在我的情况下没用(cursorTo 将光标移回开始)
回答by Imanou Petit
The Readline modulethat is part of Node.js now provides readline.cursorTo(stream, x, y), readline.moveCursor(stream, dx, dy)and readline.clearLine(stream, dir)methods.
作为Node.js 一部分的Readline 模块现在提供readline.cursorTo(stream, x, y),readline.moveCursor(stream, dx, dy)和readline.clearLine(stream, dir)方法。
With TypeScript, your code should look like this:
使用 TypeScript,您的代码应如下所示:
import * as readline from 'readline'
// import readline = require('readline') also works
/* ... */
function writeWaitingPercent(p: number) {
readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0, null)
let text = `waiting ... ${p}%`
process.stdout.write(text)
}
The previous code will transpile into the following Javascript (ES6) code:
前面的代码将转换为以下 Javascript (ES6) 代码:
const readline = require('readline');
/* ... */
function writeWaitingPercent(p) {
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0, null);
let text = `waiting ... ${p}%`;
process.stdout.write(text);
}
As an alternative, you can use the following code for Javascript (ES6):
作为替代方案,您可以将以下代码用于 Javascript (ES6):
const readline = require('readline');
/* ... */
function waitingPercent(p) {
readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0)
let text = `waiting ... ${p}%`
process.stdout.write(text)
}
回答by Maksim Shamihulau
if you see stdout exceptions like TypeError: process.stdout.clearLineis not a function in Debug Console window of Visual Studio Code (or Webstorm), run the app as external terminal application instead of internal console. The reason is that Debug Console window is not TTY (process.stdout.isTTYis false). Therefore update your launch configuration in launch.jsonwith "console": "externalTerminal"option.
如果TypeError: process.stdout.clearLine在 Visual Studio Code(或 Webstorm)的调试控制台窗口中看到 stdout 异常,如不是函数,请将应用程序作为外部终端应用程序而不是内部控制台运行。原因是调试控制台窗口不是 TTY(process.stdout.isTTY是假的)。因此,launch.json使用"console": "externalTerminal"选项更新您的启动配置。
回答by Rafael Teixeira
process.readline.cursorTo and process.readline.clearLine
process.readline.cursorTo 和 process.readline.clearLine

