57 static int LENGTH_OF_PROGRESS_BAR;
58 static double PERCENTAGE_BIN_SIZE;
60 static std::string generateProgressBar(
unsigned int percentage,
double currentTime)
62 const int progress =
static_cast<int>(std::ceil(percentage / (
double)PERCENTAGE_BIN_SIZE));
63 std::ostringstream ss;
64 ss << std::fixed << std::setprecision(3) <<
"T:" << currentTime <<
" " << std::setw(3) << std::right << percentage <<
"% ";
65 std::string bar(
"[" + std::string(LENGTH_OF_PROGRESS_BAR - 2,
' ') +
"]");
67 unsigned int numberOfSymbols = std::min(
68 std::max(0, progress - 1),
69 LENGTH_OF_PROGRESS_BAR - 2);
71 bar.replace(1, numberOfSymbols, std::string(numberOfSymbols,
'#'));
82 ProgressBar(
double totalTime,
const std::string& initialMessage =
"")
83 : mTotalTime(totalTime)
88 mCout = std::cout.rdbuf();
89 std::cout.rdbuf(mSs.rdbuf());
90 mOutStream.rdbuf(mCout);
91 mOutStream << initialMessage <<
"\n";
92 mLengthOfLastPrintedMessage = initialMessage.size();
93 mOutStream << generateProgressBar(0, 0) <<
"\r" << std::flush;
117 mOutStream << std::string(2,
'\n');
118 std::cout.rdbuf(mCout);
131 throw std::runtime_error(
"Attempted to use progress bar after having terminated it");
136 << std::setw(LENGTH_OF_PROGRESS_BAR + 6)
138 mLengthOfLastPrintedMessage = message.size();
139 const unsigned int percentage =
static_cast<unsigned int>(std::ceil(mCurrentTime / mTotalTime * 100));
141 mOutStream << generateProgressBar(percentage, mCurrentTime) <<
"\r" << std::flush;
168 throw std::runtime_error(
169 "Attempted to use progress bar after having terminated it");
171 mCurrentTime = currentTime;
173 const unsigned int percentage =
static_cast<unsigned int>(std::ceil(mCurrentTime / mTotalTime * 100));
175 if (mSs.str().length() > 0) {
176 printNewMessage(
"Message at time " + std::to_string(currentTime) +
": " + mSs.str());
181 mOutStream << generateProgressBar(percentage, mCurrentTime) <<
"\r" << std::flush;
188 size_t mLengthOfLastPrintedMessage;
189 std::stringstream mSs;
190 std::streambuf* mCout;
191 std::ostream mOutStream;