如何在Node.js CLI应用程序中使用记录和进度栏?

发布于 2025-01-21 17:47:11 字数 1085 浏览 3 评论 0原文

上下文

node.js应用程序中的

CLI应用程序时,在构建文件时将显示一个进度栏。在构建操作过程中,有时需要将信息/错误记录到控制台。这打扰了进度栏:

  • 进度栏后立即将信息/错误日志登录到控制台,而不是在新线上
  • 登录。在日志完成后,进度条再次打印出来,导致在

插图的控制台插图 中打印多个进度条。控制台:

[===========----------------------] 11 / 33 builtwarn: something wrong here.
[=============--------------------] 13 / 33 builtwarn: something wrong here.
warn: example warning that continues here.
error:  some stacktrace
[=================================] 33 / 33 built

问题

是否有办法确保进度栏不会受到干扰,并且在栏下方/下方打印到控制台的任何信息日志?因此仅显示一个进度条。

我知道有一个 intrupp node-progress中的方法,但是我不确定如何与温斯顿一起使用。

我想这在CLI应用程序中是一个相当普遍的情况,因此也赞赏有关如何通过其他依赖/方法进行的任何建议/想法!

Context

In the Node.js application, I use:

The CLI application will display a progress bar when building files. During the build operation, sometimes information/errors need to be logged to the console. This disturbs the progress bar in that:

  • the info/error logs to the console immediately after the progress bar and not on a newline
  • the progress bar gets printed again after the logs have finished, resulting in multiple progress bars printed in the console

Illustration of the console:

[===========----------------------] 11 / 33 builtwarn: something wrong here.
[=============--------------------] 13 / 33 builtwarn: something wrong here.
warn: example warning that continues here.
error:  some stacktrace
[=================================] 33 / 33 built

Question

Is there a way to ensure that the progress bar is not disturbed and any information logs to the console are printed above/below the bar? Such that only one progress bar is shown.

I understand that there's a interrupt method in node-progress, but I am not sure how to use that with winston.

I would imagine this to be a fairly common scenario in CLI applications so any suggestions/ideas of how to do it via other dependencies/approaches are appreciated too!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

居里长安 2025-01-28 17:47:11

我自己做一个进步的酒吧课。
您可以打印一个\ r以清除屏幕上的最后一行。
然后打印您的日志。
之后,打印\ n,请确保新的进度栏不会清除您的日志。

class MyProgress {
    constructor(total,str_left,str_right){
        this.str_left=".";
        this.str_right=" ";
        if(str_left)this.str_left=str_left;
        if(str_right)this.str_right=str_right;
        this.total = total;
        this.current=0;
        this.strtotal=60;//progress bar width。
    }
    update(current){
        this.current++;
        if(current)this.current=current;        
        var dots=this.str_left.repeat(parseInt( (this.current % this.total) /this.total*this.strtotal));
        var left = this.strtotal - parseInt( (this.current % this.total) /this.total*this.strtotal);
        var empty = this.str_right.repeat(left);
        process.stdout.write(`\r[${dots}${empty}] ${parseInt(this.current/this.total * 100)}% [${this.total}-${this.current}]`);
        //if(this.total <= this.current){ this.current=0; }//
    }   
}
function print(params,...other){
    process.stdout.write("\r");
    console.log(params,...other);//
    process.stdout.write("\n");
}

I make a progress bar class myself.
You can print a \r to clear last line on the screen.
Then print you log.
After that, print a \n, make sure new progress bar will not clear you log.

class MyProgress {
    constructor(total,str_left,str_right){
        this.str_left=".";
        this.str_right=" ";
        if(str_left)this.str_left=str_left;
        if(str_right)this.str_right=str_right;
        this.total = total;
        this.current=0;
        this.strtotal=60;//progress bar width。
    }
    update(current){
        this.current++;
        if(current)this.current=current;        
        var dots=this.str_left.repeat(parseInt( (this.current % this.total) /this.total*this.strtotal));
        var left = this.strtotal - parseInt( (this.current % this.total) /this.total*this.strtotal);
        var empty = this.str_right.repeat(left);
        process.stdout.write(`\r[${dots}${empty}] ${parseInt(this.current/this.total * 100)}% [${this.total}-${this.current}]`);
        //if(this.total <= this.current){ this.current=0; }//
    }   
}
function print(params,...other){
    process.stdout.write("\r");
    console.log(params,...other);//
    process.stdout.write("\n");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文