progressbar.h 1.81 KB
#ifndef RTS_PROGRESSBAR_H
#define RTS_PROGRESSBAR_H

#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

void stimPrintTime(unsigned long long s) {
	std::cout << std::fixed << std::showpoint << std::setprecision(1);
	if (s >= 60) {											//if there are more than 60 seconds
		unsigned long long m = s / 60;						//get the number of minutes
		s = s - m * 60;										//get the number of remaining seconds
		if (m > 60) {										//if more than 60 minutes
			unsigned long long h = m / 60;
			m = m - h * 60;									//get the number of remaining minutes
			if (h > 24) {									//if there are more than 24 hours
				unsigned long long d = h / 24;
				h = h - d * 24;								//get the number of remaining hours
				std::cout << " " << d << "d";
			}
			std::cout << " " << h << "h";
		}
		std::cout << " " << m << "m";
	}
	std::cout << " " << s << "s";
}

static void rtsProgressBar(unsigned int percent, double elapsed_s = 0)
{
	//std::cout<<percent<<std::endl;
	stringstream bar;
	static int x = 0;
	string slash[4];
	slash[0] = "\\";
	slash[1] = "-";
	slash[2] = "/";
	slash[3] = "|";
	bar<<"[";
	for(int i=0; i<40; i++)
	{
		if(percent > (float)i/(float)40 *100)
			bar << "*";
		else
			bar << " ";
	}
	bar<<"]";
	cout << "\r"; // carriage return back to beginning of line
	cout << bar.str() << " " << slash[x] << " " << percent << " %"; // print the bars and percentage
	if (elapsed_s > 0) {
		stimPrintTime((unsigned long long)elapsed_s);
		if (percent > 0) {
			std::cout << " / ";
			double s_per_percent = elapsed_s / percent;
			double total = s_per_percent * 100;
			stimPrintTime((unsigned long long)total);
		}
	}
	cout.flush();
	x++; // increment to make the slash appear to rotate
	if(x == 4)
	x = 0; // reset slash animation
}

#endif