ComPWA
Common Partial-Wave-Analysis Framework
ProgressBar.cpp
Go to the documentation of this file.
1 // Copyright (c) 2013 The ComPWA Team.
2 // This file is part of the ComPWA framework, check
3 // https://github.com/ComPWA/ComPWA/license.txt for details.
4 
5 #include "Core/ProgressBar.hpp"
6 
7 #include <chrono>
8 #include <iostream>
9 
10 namespace ComPWA {
11 
13 
14 ProgressBar::ProgressBar(std::size_t size, int update)
15  : numEvents(size), updateInterval(update), hasStarted(0), startTime(0),
17  if (update == 0)
18  updateInterval = 1;
19 }
20 
21 ProgressBar::~ProgressBar() { std::cout << std::endl; }
22 
23 void ProgressBar::next(size_t increment) {
24  if (!hasStarted) {
25  lastUpdate = 0;
26  currentEvent = 0;
27  time(&startTime);
28  hasStarted = 1;
29  update();
30  fflush(stdout);
31  }
32  currentEvent += increment;
33  if (currentEvent > numEvents)
35  if ((int)((timePassed() - lastUpdate)) > updateInterval)
36  update();
37  if (currentEvent == numEvents) {
38  update();
39  std::cout << std::endl;
40  }
41 }
43  // std::cout<<"update()"<<std::endl;
44  // std::cout<<timePassed()<<" "<<lastUpdate<<"
45  //"<<(int)((timePassed()-lastUpdate))<<std::endl;
46  currentPercent = (double)currentEvent / numEvents * 100;
47  int nStars = ((int)(currentPercent / 10 + 0.5));
48  char buf[10];
49  int i = 0;
50  for (; i < nStars; i++)
51  buf[i] = '*';
52  for (; i < 10; i++)
53  buf[i] = '-';
54  // for( ; i<15; i++) buf[i]=' ';
55  time_t estEndTime = endTime();
56  char timebuf[10];
57  strftime(timebuf, 10, "%I:%M%p", localtime(&estEndTime));
58  // the escape sequence \e[?25l switches off the courser and \e[?25h switch it
59  // on again
60  printf("\e[?25l \r %4.2fmin [ %.*s %4.2f%% ] %4.2fmin , end time: %7s "
61  " \e[?25h",
62  timePassed() / 60, 10, buf, currentPercent, timeRemaining() / 60,
63  timebuf);
64  fflush(stdout);
65 
67  return;
68 }
69 
71  if (currentPercent == 0)
72  return 0.0;
73  return timePassed() * (1 / currentPercent * 100 - 1);
74 }
76  time_t currentTime;
77  time(&currentTime);
78  return difftime(currentTime, startTime);
79 }
80 
82  time_t currentTime;
83  time(&currentTime);
84  time_t estEndTime = currentTime + timeRemaining();
85  return estEndTime;
86 }
87 
88 } // namespace ComPWA
std::size_t numEvents
Definition: ProgressBar.hpp:29
void next(size_t increment=1)
indicate the next step in process
Definition: ProgressBar.cpp:23
unsigned int currentEvent
Definition: ProgressBar.hpp:35