Progress bar on standard output
#include <iostream>
void showProgressBar(const float prog = 0) {
const int width = 50; // characters
std::cout << "[";
int pos = (int) (width * prog);
for (int i = 0; i < width; ++i) {
if(i < pos) {
std::cout << "=";
} else if(i == pos) {
std::cout << ">";
} else {
std::cout << " ";
}
}
std::cout << "] " << (int) (100*prog) << "%\r";
std::cout.flush();
}
}
std::cout << "] " << (int) (100*prog) << "%\r";
std::cout.flush();
}
void sleepMs(unsigned int ms) {
int microsecs;
struct timeval tv;
microsecs = ms * 1000;
tv.tv_sec = microsecs / 1000000;
tv.tv_usec = microsecs % 1000000;
select(0, NULL, NULL, NULL, &tv);
}
int main() {
for (float i = 0; i <= 100; ++i) {
sleepMs(20); // to show progress animation
showProgress(i/100);
}
std::cout << std::endl;
showProgress(0.09);
std::cout << std::endl;
}