2018-10-29 18:02:00 -05:00
|
|
|
|
|
|
|
|
#include "openmc/progress_bar.h"
|
|
|
|
|
|
|
|
|
|
#include <iomanip>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
2019-02-22 23:26:32 -06:00
|
|
|
#if defined(__unix__) || defined(__unix) || \
|
|
|
|
|
(defined(__APPLE__) && defined(__MACH__))
|
2018-10-29 23:00:25 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-10-29 18:02:00 -05:00
|
|
|
#define BAR_WIDTH 72
|
|
|
|
|
|
2018-10-29 23:00:25 -05:00
|
|
|
bool is_terminal()
|
|
|
|
|
{
|
2019-02-22 23:26:32 -06:00
|
|
|
#ifdef _POSIX_VERSION
|
2018-10-30 20:13:36 -05:00
|
|
|
return isatty(STDOUT_FILENO) != 0;
|
2018-10-29 23:00:25 -05:00
|
|
|
#else
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 18:02:00 -05:00
|
|
|
ProgressBar::ProgressBar()
|
|
|
|
|
{
|
2018-10-30 12:45:06 -05:00
|
|
|
// initialize bar
|
2018-10-29 18:02:00 -05:00
|
|
|
set_value(0.0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProgressBar::set_value(double val)
|
|
|
|
|
{
|
2018-10-29 23:00:25 -05:00
|
|
|
|
|
|
|
|
if (!is_terminal())
|
|
|
|
|
return;
|
2018-11-02 23:08:51 -05:00
|
|
|
|
2018-10-29 18:02:00 -05:00
|
|
|
// set the bar percentage
|
|
|
|
|
if (val >= 100.0) {
|
|
|
|
|
bar.append("100");
|
|
|
|
|
} else if (val <= 0.0) {
|
|
|
|
|
bar.append(" 0");
|
|
|
|
|
} else {
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
ss << std::setfill(' ') << std::setw(3) << (int)val;
|
|
|
|
|
bar.append(ss.str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bar.append("% |");
|
|
|
|
|
// remaining width of the bar
|
2018-10-30 19:41:30 -05:00
|
|
|
int remaining_width = BAR_WIDTH - bar.size() - 2;
|
2018-11-02 23:08:51 -05:00
|
|
|
|
2018-10-29 18:02:00 -05:00
|
|
|
// set the bar width
|
|
|
|
|
if (val >= 100.0) {
|
2018-10-30 19:41:30 -05:00
|
|
|
bar.append(remaining_width, '=');
|
|
|
|
|
} else if (val < 0.0) {
|
2018-11-02 23:08:51 -05:00
|
|
|
bar.append(remaining_width, ' ');
|
2018-10-29 18:02:00 -05:00
|
|
|
} else {
|
2018-10-30 19:41:30 -05:00
|
|
|
int width = (int)((double)remaining_width * val / 100);
|
2018-10-29 18:02:00 -05:00
|
|
|
bar.append(width, '=');
|
|
|
|
|
bar.append(1, '>');
|
2018-10-30 19:41:30 -05:00
|
|
|
bar.append(remaining_width - width - 1, ' ');
|
2018-10-29 18:02:00 -05:00
|
|
|
}
|
|
|
|
|
|
2018-10-29 23:00:25 -05:00
|
|
|
bar.append("|+");
|
2018-11-02 23:08:51 -05:00
|
|
|
|
2018-10-29 18:02:00 -05:00
|
|
|
// write the bar
|
|
|
|
|
std::cout << '\r' << bar << std::flush;
|
2018-11-02 22:39:22 -05:00
|
|
|
if (val >= 100.0) {
|
|
|
|
|
std::cout << "\n";
|
|
|
|
|
}
|
2018-11-02 23:08:51 -05:00
|
|
|
|
2018-10-29 18:02:00 -05:00
|
|
|
// reset the bar value
|
2018-10-29 22:09:13 -05:00
|
|
|
bar = "";
|
2018-10-29 18:02:00 -05:00
|
|
|
}
|