Diana Software
QProgressBar.hh
Go to the documentation of this file.
1 #ifndef _Q_PROGRESSBAR_HH_
2 #define _Q_PROGRESSBAR_HH_
3 
24 #include "QObject.hh"
25 #include <string>
26 #include <iostream>
27 #include <sstream>
28 #include <iomanip>
29 #include <cstdio>
30 
31 using std::string;
32 
33 
34 class QProgressBar : public Diana::QObject {
35 
36  public:
37  QProgressBar(uint64_t total_iterations,string description="Progress",bool leave=true,int step=1,bool enable=true,std::ostream& out=std::cout)
38  :foutstream(out)
39  {
40  fEnabled=enable;
41  if(fEnabled){
42  Clear();
44  fleave=leave;
45  fstep=(uint64_t)step;
46 
47  SetIterations(total_iterations);
48  SetDescription(description);
49  }
50  }
51  QProgressBar(const QProgressBar& other)
52  :foutstream(other.foutstream)
53  {
54  *this = other;
55  }
57  :foutstream(std::cout)
58  {
59  Clear();
60  }
61 
64  void Clear(){
65  if(fEnabled){
67  foutstream<<"\r";
68  for(size_t i=0;i<terminalwidth;i++){
69  foutstream<<" ";
70  }
71  foutstream<<"\r";
72 
73  fNumOfIters=0;
74  fCurrentIter=0;
75  fdesc="Progress";
76  terminalwidth=50;
77  fleave=true;
78  fstep=1;
79  }
80  }
81 
82  void SetIterations(uint64_t total_iterations){
83  fNumOfIters=total_iterations;
84  uint64_t sugg_step=total_iterations/1e4;//because we are printing number with 2 decimal places
85  if(sugg_step>fstep){
86  fstep=sugg_step;
87  }
88  }
89  void SetDescription(string description){fdesc=description;}
90 
91  void IncrementIter(bool printbar=true){
92  if(fEnabled){
93  fCurrentIter++;
94  printbar = printbar and ((fCurrentIter%fstep==0) or (fCurrentIter==fNumOfIters));
95  if(printbar){
96  PrintBar();
97  }
99  if(!fleave){
100  foutstream<<"\r";
101  for(size_t i=0;i<terminalwidth;i++){
102  foutstream<<" ";
103  }
104  foutstream<<"\r";
105  }else{
106  foutstream<<std::endl;
107  }
108  }
109  }
110  }
111 
112  void Print(string message,bool isbar=false)const{
113  if(fEnabled){
114  foutstream<<"\r";
115  foutstream<<message;
116  if(!isbar){
117  for(size_t i=message.size();i<terminalwidth;i++){
118  foutstream<<" ";
119  }
120  foutstream<<std::endl;
121  foutstream<<bar.str();
122  }
123  }else if(!isbar){
124  foutstream<<message<<std::endl;
125  }
126  foutstream.flush();
127  }
129  {
130  fNumOfIters=other.fNumOfIters;
131  fCurrentIter = other.fCurrentIter;
132  fdesc=other.fdesc;
133  bar<<other.bar.str();
134  fleave=other.fleave;
136  fEnabled=other.fEnabled;
137  return *this;
138  }
139  protected:
140  std::ostream& foutstream;
141 
142  private:
144  FILE* stream = popen("tput cols", "r");
145  if (stream) {
146  char buffer[128];
147  if (fgets(buffer, sizeof(buffer), stream) != NULL) {
148  terminalwidth = (size_t)std::stoi(buffer);
149  }
150  pclose(stream);
151  }
152  }
153 
154  void PrintBar(){
155  char donechar ='#';
156  double doneperc = (double)fCurrentIter/(double)fNumOfIters;
157  bar.str("");
158  bar <<"[";
159  bar <<std::setw(6)<<std::setfill(' ')<< std::fixed << std::setprecision(2) << doneperc*100;
160  bar <<"%]"<<fdesc<<": [";
161 
162  size_t progresssize=terminalwidth-(bar.str().size()+1);
163  size_t maxdonechar = (size_t)(doneperc * (double)progresssize);
164  for(size_t i =0;i<progresssize;i++){
165  if(i<maxdonechar){
166  bar<<donechar;
167  }else{
168  bar<<" ";
169  }
170  }
171  bar <<"]";
172  Print(bar.str(),true);
173  }
174  bool fleave;
175 
176  uint64_t fNumOfIters;
177  uint64_t fCurrentIter;
178  uint64_t fstep;
179 
180  string fdesc;
181 
182  std::ostringstream bar;
184  bool fEnabled;
185 
187 
188 };
189 
190 #endif
one-line description of your QObject
Definition: QProgressBar.hh:34
uint64_t fCurrentIter
void SetDescription(string description)
Definition: QProgressBar.hh:89
uint64_t fNumOfIters
void Print(string message, bool isbar=false) const
std::ostream & foutstream
void IncrementIter(bool printbar=true)
Definition: QProgressBar.hh:91
QProgressBar & operator=(const QProgressBar &other)
void get_terminal_size()
QObjectDef(QProgressBar, 2)
uint64_t fstep
std::ostringstream bar
QProgressBar(uint64_t total_iterations, string description="Progress", bool leave=true, int step=1, bool enable=true, std::ostream &out=std::cout)
Definition: QProgressBar.hh:37
void Clear()
clear members
Definition: QProgressBar.hh:64
QProgressBar(const QProgressBar &other)
Definition: QProgressBar.hh:51
size_t terminalwidth
void SetIterations(uint64_t total_iterations)
Definition: QProgressBar.hh:82