Documentation of CSL
progressBar.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
23 #ifndef CSL_PROGRESS_BAR_H_INCLUDED
24 #define CSL_PROGRESS_BAR_H_INCLUDED
25 
26 #include <iostream>
27 #include <string>
28 #include "timeMonitor.h"
29 
30 namespace csl {
31 
32 class ProgressBar {
33 
34  public:
35 
36  static inline char onCharacter = '=';
37  static inline char lastOnCharacter = '>';
38  static inline char offCharacter = '.';
39  static inline char leftSide = '[';
40  static inline char rightSide = ']';
41  static inline const size_t defaultSizeBar = 30;
42  static inline const std::string defaultMessage = "Progress : ";
43 
44  ProgressBar(size_t t_max,
45  std::string const& t_message = defaultMessage,
46  size_t t_sizeBar = defaultSizeBar,
47  std::ostream & t_out = std::cout)
48  :message(t_message),
49  out(t_out),
50  max(t_max),
51  sizeBar(t_sizeBar),
52  timeMonitoring(true)
53  {
54 
55  }
56 
57  void setTimeMonitoring(bool value)
58  {
59  timeMonitoring = value;
60  monitor.reset();
61  }
62 
63  void reset()
64  {
65  monitor.reset();
66  }
67 
68  void reset(size_t t_max)
69  {
70  max = t_max;
71  monitor.reset();
72  }
73 
74  void reset(size_t t_max, std::string const &t_message)
75  {
76  max = t_max;
77  message = t_message;
78  monitor.reset();
79  }
80 
81  void progress(size_t pos) const
82  {
83  enableCursor(false);
84  size_t n = nChar(pos);
85  out << message << " ";
86  out << leftSide;
87  for (size_t i = 0; i != sizeBar; ++i)
88  if (i < n)
89  out << ((i+1 == n) ? lastOnCharacter : onCharacter);
90  else
91  out << offCharacter;
92  out << rightSide;
93  float percent = percentage(pos);
94  out << " " << int(percent) << '%';
95  if (timeMonitoring) {
96  out << " , Elapsed time : ";
97  monitor.print(out);
98  if (percent > 0 and percent < 100) {
99  out << " , Time left : ";
100  double ratio = (100 - percent) * 1. / percent;
101  TimeMonitor::print(ratio * monitor.getTotalSec(), out);
102  }
103  }
104  out << ((pos + 1 == max) ? '\n' : '\r');
105  enableCursor(true);
106  }
107 
108  protected:
109 
110  size_t nChar(size_t pos) const
111  {
112  return (pos+1) * sizeBar / max;
113  }
114 
115  size_t percentage(size_t pos) const
116  {
117  return (pos+1) * 100 / max;
118  }
119 
120  void enableCursor(bool enable) const
121  {
122  out << ((enable) ? "\033[?25l" : "\033[?25h");
123  out.flush();
124  }
125 
126  private:
127 
128  std::string message;
129  std::ostream &out;
130  size_t max;
131  size_t sizeBar;
132  bool timeMonitoring;
133  mutable TimeMonitor monitor;
134 };
135 
136 } // End of namespace csl
137 
138 #endif
Definition: progressBar.h:32
Namespace for csl library.
Definition: abreviation.h:34
Definition: timeMonitor.h:31