向c ++控制台应用程序添加状态栏 [英] Adding a status bar to c++ console applications

查看:207
本文介绍了向c ++控制台应用程序添加状态栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C ++做一个linux应用程序,它将打印信息输出到控制台。程序的一部分将需要一段时间来计算,我想在控制台中添加一个状态栏,类似于wget中使用的状态栏(我把自己的描述在下面)。

 %complete [===========> ] eta 

实现这个目标的最好方法是什么?是否有任何有用的库可以方便添加此功能?

解决方案

如果你的程序像wget,基本上是一个批处理程序,而不需要一个全屏的UI(我建议 ncurses ),您可以使用在你的行后打印回车符(但不是换行符)的诡计;

 <$> 

这是一个演示。 c $ c> #include< iostream>
#include< unistd.h>

int main(void)
{

for(int i = 0; i <10; i ++){
std :: cout ;& 状态:< i<< \r<< std :: flush;
sleep(1);
}
std :: cout<< Completed.\\\
;
}


I am making a linux application using C++ and it will print info out to the console. Parts of the program will take a while to compute and I would like to add a status bar in the console similar to the one used in wget (I put my own depiction below).

%complete[===========>               ] eta

What would be the best way to accomplish this goal? Are there any useful libraries that make it easy to add this functionality?

解决方案

If your program is like wget, that is, it's basically a batch program without the need for a full-screen UI (for which I would recommend ncurses), you can use the trick to print a carriage return (but not line feed) after your line; the next thing you write will overwrite the same line.

Here's a demonstration.

#include <iostream>
#include <unistd.h>

int main(void)
{

        for (int i = 0; i < 10; i++) {
                std::cout << "Status: " << i << "\r" << std::flush;
                sleep(1);
        }
        std::cout << "Completed.\n";
}

这篇关于向c ++控制台应用程序添加状态栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆