Linux终端控制字符 \r 可以清空当前行的内容
"\33[3A" 终端光标向上移动三行,并清空后面的内容
#include <string>
#include <iostream>
#include <unistd.h>
#include <iomanip>
using namespace std;
// a,b,c表示进度,为0-1之间的小数
void refresh(float a, float b, float c)
{
if (a > 1)
a = 1;
if (b > 1)
b = 1;
if (c > 1)
c = 1;
// 整体放大
int pa = a * 50;
int pb = b * 50;
int pc = c * 50;
cout << "\33[3A"; // 终端光标向上移动三行
cout << "[" + string(pa, '=') + ">" + string(50 - pa, ' ') << "] " << a*100 << "%" << endl;
cout << "[" + string(pb, '=') + ">" + string(50 - pb, ' ') << "] " << b*100 << "%" << endl;
cout << "[" + string(pc, '=') + ">" + string(50 - pc, ' ') << "] " << c*100 << "%" << endl;
fflush(stdout); // 刷新缓冲区
}
int main()
{
cout << "\n\n\n";
cout << setiosflags(ios::fixed) << setprecision(2);
float a=0, b=0, c=0;
for(int i = 0; i <= 100; i++)
{
a += 0.01;
b += 0.02;
c += 0.03;
refresh(a,b,c);
usleep(1000 * 100);
}
return 0;
}
网友评论