Node.js nodejs中cursorTo的用法是什么?
我读了一个nodejs文件,遇到了一个cursorTo方法,我不理解它。请有人解释一下Node.js nodejs中cursorTo的用法是什么?,node.js,Node.js,我读了一个nodejs文件,遇到了一个cursorTo方法,我不理解它。请有人解释一下 function refreshConsole () { if (Settings.properties.preventMessageFlicker) { readline.cursorTo(process.stdout, 0, 0); } else { process.stdout.write('\033c'); } } 如果您看到的Nodejs文档
function refreshConsole () {
if (Settings.properties.preventMessageFlicker) {
readline.cursorTo(process.stdout, 0, 0);
} else {
process.stdout.write('\033c');
}
}
如果您看到的Nodejs文档,您将看到以下解释: readline.cursorTo(流,x,y)
- 流<可写>
- x
- y
test.js
文件,并复制粘贴到其中的代码下面。使用节点test.js在控制台中执行
process.stdin.resume();
process.stdin.setEncoding('utf8');
console.log('This is interactive console for cursorTo explanation');
process.stdin.on('data', function (data) {
// This is when only x value is given as input
for(i = 0; i< 10; i++){
console.log('here x = '+ i + ' and y = 0' );
require('readline').cursorTo(process.stdout, i);
}
// This is when x and y values are given as input
// for(i = 0; i< 10; i++){
// console.log('here x = '+ i + ' and y = '+ i );
// require('readline').cursorTo(process.stdout, i, i);
// }
});
process.on('SIGINT', function(){
process.stdout.write('\n end \n');
process.exit();
});
这是因为对于每次执行require('readline')。cursorTo(process.stdout,i)
,光标将指向我们给出的相应x坐标
的下一行,y坐标
为零
对于第二个For循环(在上面的代码中有注释),我们同时传递x
和y
坐标。输出结果如下:
here x = 1 and y = 1console for cursorTo explanation
hhere x = 2 and y = 2
hehere x = 3 and y = 3
here x = 4 and y = 4
here x = 5 and y = 5
here x = 6 and y = 6
here x = 7 and y = 7
here x = 8 and y = 8
here x = 9 and y = 9
您可能会注意到,在第二个输出控制台中,用于游标解释的与此处的文本重叠。这是因为当给出坐标(x
和y
)时,对于(0,0)
,它会移回其起始点,即“这是用于光标解释的交互式控制台”附近,并打印位置,对于每个坐标,它都会移动并打印数据
可能有助于我们为什么光标中有三个参数??
here x = 1 and y = 1console for cursorTo explanation
hhere x = 2 and y = 2
hehere x = 3 and y = 3
here x = 4 and y = 4
here x = 5 and y = 5
here x = 6 and y = 6
here x = 7 and y = 7
here x = 8 and y = 8
here x = 9 and y = 9