progress_bar.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
31 
32 #include <ctime>
33 #include <sstream>
34 
35 namespace swri_console_util
36 {
38  paused_(false),
39  percent_complete_(0),
40  start_time_(ros::WallTime::now()),
41  paused_time_(0)
42  {
43  SetupTerminal();
44  }
45 
47  {
49  }
50 
51  void ProgressBar::SetStartTime(const ros::WallTime& start_time)
52  {
53  start_time_ = start_time;
54  }
55 
56  void ProgressBar::SetProgress(double percent_complete)
57  {
58  percent_complete_ = percent_complete;
59  }
60 
62  {
63  ros::WallTime current_time = ros::WallTime::now();
64  ros::WallDuration elapsed = (current_time - start_time_) - paused_time_;
65 
66  if (percent_complete_ > 0)
67  {
68  ros::WallDuration time_left =
69  (elapsed * (1.0 / percent_complete_)) - elapsed;
70 
71  if (paused_)
72  {
73  printf("\r [PAUSED] %.2f%% Complete, Elapsed: %s Estimated Remaining: %s \r",
74  percent_complete_ * 100.0,
75  GetTimeString(elapsed.toSec()).c_str(),
76  GetTimeString(time_left.toSec()).c_str());
77  }
78  else
79  {
80  printf("\r [RUNNING] %.2f%% Complete, Elapsed: %s Estimated Remaining: %s \r",
81  percent_complete_ * 100.0,
82  GetTimeString(elapsed.toSec()).c_str(),
83  GetTimeString(time_left.toSec()).c_str());
84  }
85  }
86  else
87  {
88  if (paused_)
89  {
90  printf("\r [PAUSED] %.2f%% Complete, Elapsed: %s \r",
91  percent_complete_ * 100.0,
92  GetTimeString(elapsed.toSec()).c_str());
93  }
94  else
95  {
96  printf("\r [RUNNING] %.2f%% Complete, Elapsed: %s \r",
97  percent_complete_ * 100.0,
98  GetTimeString(elapsed.toSec()).c_str());
99  }
100  }
101 
102  fflush(stdout);
103  }
104 
106  {
107  ros::WallTime start_pause = ros::WallTime::now();
108  do
109  {
110  bool charsleftorpaused = true;
111  while (charsleftorpaused && ros::ok())
112  {
113  switch (ReadCharFromStdin())
114  {
115  case ' ':
116  paused_ = !paused_;
117 
118  if (paused_)
119  {
120  PrintTime();
121  }
122  case EOF:
123  charsleftorpaused = paused_;
124  }
125  }
126  }
127  while (paused_ && ros::ok());
128 
129  paused_time_ += (ros::WallTime::now() - start_pause);
130 
131  PrintTime();
132  }
133 
135  {
136  fd_set testfd = stdin_fdset_;
137 
138  timeval tv;
139  tv.tv_sec = 0;
140  tv.tv_usec = 0;
141 
142  if (select(maxfd_, &testfd, NULL, NULL, &tv) <= 0)
143  {
144  return EOF;
145  }
146 
147  return getc(stdin);
148  }
149 
151  {
152  const int fd = fileno(stdin);
153  termios flags;
154  tcgetattr(fd, &orig_flags_);
155  flags = orig_flags_;
156  flags.c_lflag &= ~ICANON; // set raw (unset canonical modes)
157  flags.c_cc[VMIN] = 0; // i.e. min 1 char for blocking, 0 chars for
158  // non-blocking
159  flags.c_cc[VTIME] = 0; // block if waiting for char
160  tcsetattr(fd, TCSANOW, &flags);
161 
162  FD_ZERO(&stdin_fdset_);
163  FD_SET(fd, &stdin_fdset_);
164  maxfd_ = fd + 1;
165  }
166 
168  {
169  const int fd = fileno(stdin);
170  tcsetattr(fd, TCSANOW, &orig_flags_);
171  }
172 
173  std::string ProgressBar::GetTimeString(double seconds)
174  {
175  int days = static_cast<int>(seconds / 86400.0);
176  seconds -= days * 86400.0;
177 
178  int hours = static_cast<int>(seconds / 3600.0);
179  seconds -= hours * 3600.0;
180 
181  int minutes = static_cast<int>(seconds / 60.0);
182  seconds -= minutes * 60.0;
183 
184  std::string time;
185  std::string unit;
186  if (days > 0)
187  {
188  time += IntToString(days, 2) + ":";
189  unit = "d";
190  }
191 
192  if (hours > 0 || !time.empty())
193  {
194  if (time.empty())
195  {
196  unit = "h";
197  }
198 
199  time += IntToString(hours, 2) + ":";
200  }
201 
202  if (minutes > 0 || !time.empty())
203  {
204  if (time.empty())
205  {
206  unit = "m";
207  }
208 
209  time += IntToString(minutes, 2) + ":";
210  }
211 
212  if (time.empty())
213  {
214  unit = "s";
215  }
216 
217  time += IntToString(seconds, 2) + unit;
218 
219  return time;
220  }
221 
222  std::string ProgressBar::IntToString(int64_t i, int width)
223  {
224  std::stringstream ss;
225  std::string s;
226  ss << std::setfill('0');
227  ss << std::setw(width);
228  ss << i;
229  s = ss.str();
230  return s;
231  }
232 }
void SetProgress(double percent_complete)
ros::WallDuration paused_time_
Definition: progress_bar.h:64
static std::string IntToString(int64_t i, int width=0)
XmlRpcServer s
static std::string GetTimeString(double seconds)
void SetStartTime(const ros::WallTime &start_time)
ROSCPP_DECL bool ok()
static WallTime now()
#define NULL


swri_console_util
Author(s): Marc Alban
autogenerated on Sat Jan 21 2023 03:13:13