如何在使用console.log时冻结NodeJS中的一行

时间:2022-03-05 14:30:43

For example, I am doing

例如,我正在做

console.log('Press A to Stop')
for(var i=0;i<10000;i++) console.log(i);

I want that the message Press a To Stop, will continue to appear all the time.

我希望消息按a停止,将一直持续出现。

Do you have any idea how to do that?

你知道怎么做吗?

如何在使用console.log时冻结NodeJS中的一行

The only solution I think about, is to calcuare number rows in the screen, and render frame after frame, (x-1 rows), and add the bottom row each time.

我考虑的唯一解决方案是,在屏幕上计算数字行,并呈现一帧又一帧的帧(x-1行),每次都添加下一行。

More Info

I want to show logs on the screen. (queries, requests, errors, and more), and I want the user to be able to change the log level, I want to show the instruction on the screen while he seeing the logs.

我想在屏幕上显示日志。(查询、请求、错误等),我希望用户能够更改日志级别,我希望在用户查看日志时在屏幕上显示指令。

1 个解决方案

#1


3  

It's actually pretty easy, but you can't use console.log (which isn't a biggie in your case).

它实际上很简单,但是你不能使用控制台。log(在你的情况下这不是什么大事)。

What you will need to do is the following:

你需要做的是:

  1. instead of console.log use process.stdout.write(x) where x is the output you want (followed by a new line character \n)
  2. 而不是控制台。log使用process.stdout.write(x)其中x是您想要的输出(后面跟着一个新的行字符\n)
  3. use process.stdout.write("Press A to Stop"); (note there is no new line character at the end)
  4. 使用process.stdout。写(“按下停止”);(注意到末尾没有新的行字符)
  5. right before you need to write the next line of information use process.stdout.clearLine(); which clears what's currently on the last line of the output
  6. 在您需要编写下一行信息使用过程之前,请使用stdout. clearline ();哪一个清除了当前输出的最后一行?
  7. repeat steps 1 to 3
  8. 重复步骤1到3

#1


3  

It's actually pretty easy, but you can't use console.log (which isn't a biggie in your case).

它实际上很简单,但是你不能使用控制台。log(在你的情况下这不是什么大事)。

What you will need to do is the following:

你需要做的是:

  1. instead of console.log use process.stdout.write(x) where x is the output you want (followed by a new line character \n)
  2. 而不是控制台。log使用process.stdout.write(x)其中x是您想要的输出(后面跟着一个新的行字符\n)
  3. use process.stdout.write("Press A to Stop"); (note there is no new line character at the end)
  4. 使用process.stdout。写(“按下停止”);(注意到末尾没有新的行字符)
  5. right before you need to write the next line of information use process.stdout.clearLine(); which clears what's currently on the last line of the output
  6. 在您需要编写下一行信息使用过程之前,请使用stdout. clearline ();哪一个清除了当前输出的最后一行?
  7. repeat steps 1 to 3
  8. 重复步骤1到3