FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
CProgressBar.h
1#ifndef PROGRESS_BAR_H
2#define PROGRESS_BAR_H
3
41#include <cmath>
42#include <fhsim/PrintDuringExec.h>
43#include <fhsim/simobject/SimObject.h>
44#include <iomanip>
45#include <iostream>
46#include <memory>
47#include <sstream>
48#include <string>
49#include <algorithm>
50#include <stdexcept>
51
57{
58 public:
59 static int LENGTH_OF_PROGRESS_BAR;
60 static double PERCENTAGE_BIN_SIZE;
61
62 static std::string generateProgressBar(unsigned int percentage, double currentTime)
63 {
64 const int progress = static_cast<int>(std::ceil(percentage / (double)PERCENTAGE_BIN_SIZE));
65 std::ostringstream ss;
66 ss << std::fixed << std::setprecision(3) << "T:" << currentTime << " " << std::setw(3) << std::right << percentage << "% ";
67 std::string bar("[" + std::string(LENGTH_OF_PROGRESS_BAR - 2, ' ') + "]");
68
69 unsigned int numberOfSymbols = std::min(
70 std::max(0, progress - 1),
71 LENGTH_OF_PROGRESS_BAR - 2);
72
73 bar.replace(1, numberOfSymbols, std::string(numberOfSymbols, '#'));
74 ss << bar;
75 return ss.str();
76 }
77
84 ProgressBar(double totalTime, const std::string& initialMessage = "")
85 : mTotalTime(totalTime)
86 , mCurrentTime(0)
87 , mEnded(false)
88 , mOutStream(nullptr)
89 {
90 mCout = std::cout.rdbuf();
91 std::cout.rdbuf(mSs.rdbuf());
92 mOutStream.rdbuf(mCout);
93 mOutStream << initialMessage << "\n";
94 mLengthOfLastPrintedMessage = initialMessage.size();
95 mOutStream << generateProgressBar(0, 0) << "\r" << std::flush;
96 }
97
102 {
104 }
105
106 // Make the object non-copyable
107 ProgressBar(const ProgressBar& o) = delete;
108 ProgressBar& operator=(const ProgressBar& o) = delete;
109
117 {
118 if (!mEnded) {
119 mOutStream << std::string(2, '\n');
120 std::cout.rdbuf(mCout);
121 }
122 mEnded = true;
123 }
124
130 void printNewMessage(const std::string& message)
131 {
132 if (mEnded) {
133 throw std::runtime_error("Attempted to use progress bar after having terminated it");
134 }
135
136 mOutStream << "\r"
137 << std::left
138 << std::setw(LENGTH_OF_PROGRESS_BAR + 6)
139 << message << "\n";
140 mLengthOfLastPrintedMessage = message.size();
141 const unsigned int percentage = static_cast<unsigned int>(std::ceil(mCurrentTime / mTotalTime * 100));
142
143 mOutStream << generateProgressBar(percentage, mCurrentTime) << "\r" << std::flush;
144 }
145
146
153 /*void updateLastPrintedMessage(const std::string& message)
154 {
155 if (mEnded)
156 {
157 throw std::runtime_error("Attempted to use progress bar after having terminated it");
158 }
159
160 mOutStream << "\r\x1b[1A"
161 << std::left
162 << std::setw(mLengthOfLastPrintedMessage)
163 << message << "\n";
164 mLengthOfLastPrintedMessage = message.size();
165 }*/
166
167 void setTime(double currentTime)
168 {
169 if (mEnded) {
170 throw std::runtime_error(
171 "Attempted to use progress bar after having terminated it");
172 }
173 mCurrentTime = currentTime;
174
175 const unsigned int percentage = static_cast<unsigned int>(std::ceil(mCurrentTime / mTotalTime * 100));
176
177 if (mSs.str().length() > 0) {
178 printNewMessage("Message at time " + std::to_string(currentTime) + ": " + mSs.str());
179 mSs.str("");
180 mSs.clear();
181 }
182
183 mOutStream << generateProgressBar(percentage, mCurrentTime) << "\r" << std::flush;
184 }
185
186 private:
187 double mTotalTime;
188 double mCurrentTime;
189 bool mEnded;
190 size_t mLengthOfLastPrintedMessage;
191 std::stringstream mSs;
192 std::streambuf* mCout;
193 std::ostream mOutStream;
194};
195
196
197class CProgressBar : public SimObject
198{
199 public:
200 CProgressBar(const std::string& name, ISimObjectCreator* const creator);
201 void OdeFcn(const double t, const double* const x, double* const xdot) const { }
202 void PreOdeFcn(const double T, const double* const X);
203 void FinalSetup(const double T, const double* const X, ISimObjectCreator* const creator);
204 protected:
207 std::unique_ptr<ProgressBar> m_actualProgress;
208 PrintDuringExec* mPrint;
209};
210
211
212#endif /* PROGRESS_BAR_H */
Definition CProgressBar.h:198
double m_end_time
Definition CProgressBar.h:206
Definition CProgressBar.h:57
ProgressBar(double totalTime, const std::string &initialMessage="")
Definition CProgressBar.h:84
void printNewMessage(const std::string &message)
Definition CProgressBar.h:130
~ProgressBar()
Definition CProgressBar.h:101
void setTime(double currentTime)
Definition CProgressBar.h:167
void endProgressBar()
Definition CProgressBar.h:116