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
55{
56 public:
57 static int LENGTH_OF_PROGRESS_BAR;
58 static double PERCENTAGE_BIN_SIZE;
59
60 static std::string generateProgressBar(unsigned int percentage, double currentTime)
61 {
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, ' ') + "]");
66
67 unsigned int numberOfSymbols = std::min(
68 std::max(0, progress - 1),
69 LENGTH_OF_PROGRESS_BAR - 2);
70
71 bar.replace(1, numberOfSymbols, std::string(numberOfSymbols, '#'));
72 ss << bar;
73 return ss.str();
74 }
75
82 ProgressBar(double totalTime, const std::string& initialMessage = "")
83 : mTotalTime(totalTime)
84 , mCurrentTime(0)
85 , mEnded(false)
86 , mOutStream(nullptr)
87 {
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;
94 }
95
100 {
102 }
103
104 // Make the object non-copyable
105 ProgressBar(const ProgressBar& o) = delete;
106 ProgressBar& operator=(const ProgressBar& o) = delete;
107
115 {
116 if (!mEnded) {
117 mOutStream << std::string(2, '\n');
118 std::cout.rdbuf(mCout);
119 }
120 mEnded = true;
121 }
122
128 void printNewMessage(const std::string& message)
129 {
130 if (mEnded) {
131 throw std::runtime_error("Attempted to use progress bar after having terminated it");
132 }
133
134 mOutStream << "\r"
135 << std::left
136 << std::setw(LENGTH_OF_PROGRESS_BAR + 6)
137 << message << "\n";
138 mLengthOfLastPrintedMessage = message.size();
139 const unsigned int percentage = static_cast<unsigned int>(std::ceil(mCurrentTime / mTotalTime * 100));
140
141 mOutStream << generateProgressBar(percentage, mCurrentTime) << "\r" << std::flush;
142 }
143
144
151 /*void updateLastPrintedMessage(const std::string& message)
152 {
153 if (mEnded)
154 {
155 throw std::runtime_error("Attempted to use progress bar after having terminated it");
156 }
157
158 mOutStream << "\r\x1b[1A"
159 << std::left
160 << std::setw(mLengthOfLastPrintedMessage)
161 << message << "\n";
162 mLengthOfLastPrintedMessage = message.size();
163 }*/
164
165 void setTime(double currentTime)
166 {
167 if (mEnded) {
168 throw std::runtime_error(
169 "Attempted to use progress bar after having terminated it");
170 }
171 mCurrentTime = currentTime;
172
173 const unsigned int percentage = static_cast<unsigned int>(std::ceil(mCurrentTime / mTotalTime * 100));
174
175 if (mSs.str().length() > 0) {
176 printNewMessage("Message at time " + std::to_string(currentTime) + ": " + mSs.str());
177 mSs.str("");
178 mSs.clear();
179 }
180
181 mOutStream << generateProgressBar(percentage, mCurrentTime) << "\r" << std::flush;
182 }
183
184 private:
185 double mTotalTime;
186 double mCurrentTime;
187 bool mEnded;
188 size_t mLengthOfLastPrintedMessage;
189 std::stringstream mSs;
190 std::streambuf* mCout;
191 std::ostream mOutStream;
192};
193
194
195class CProgressBar : public SimObject
196{
197 public:
198 CProgressBar(const std::string& name, ISimObjectCreator* const creator);
199 void OdeFcn(const double t, const double* const x, double* const xdot) const { }
200 void PreOdeFcn(const double T, const double* const X);
201 void FinalSetup(const double T, const double* const X, ISimObjectCreator* const creator);
202 protected:
205 std::unique_ptr<ProgressBar> m_actualProgress;
206 PrintDuringExec* mPrint;
207};
208
209
210#endif /* PROGRESS_BAR_H */
Definition CProgressBar.h:196
double m_end_time
Definition CProgressBar.h:204
Definition CProgressBar.h:55
ProgressBar(double totalTime, const std::string &initialMessage="")
Definition CProgressBar.h:82
void printNewMessage(const std::string &message)
Definition CProgressBar.h:128
~ProgressBar()
Definition CProgressBar.h:99
void setTime(double currentTime)
Definition CProgressBar.h:165
void endProgressBar()
Definition CProgressBar.h:114