ProgressBar.cxx
1 #include "ProgressBar.h"
2 
3 #define ANSI_COLOR_RED "\x1b[31m"
4 #define ANSI_COLOR_GREEN "\x1b[32m"
5 #define ANSI_COLOR_YELLOW "\x1b[33m"
6 #define ANSI_COLOR_BLUE "\x1b[34m"
7 #define ANSI_COLOR_MAGENTA "\x1b[35m"
8 #define ANSI_COLOR_CYAN "\x1b[36m"
9 #define ANSI_COLOR_RESET "\x1b[0m"
10 
11 
12 
13 
14 
15 //---------------------------------------------------------------------------------------------------------
20  std::cerr << "Assuming 100 events in ProgressBar" << std::endl;
21  maxEntry = 100;
22  counter = 0;
23  percentage = 0;
24  watch.Start(kTRUE);
25 }
26 
27 
28 
29 
30 
31 
32 //---------------------------------------------------------------------------------------------------------
37 ProgressBar::ProgressBar(Long64_t maxEntryInit){
38  maxEntry = maxEntryInit;
39  counter = 0;
40  percentage = 0;
41  watch.Start(kTRUE);
42 }
43 
44 
45 
46 
47 
48 
49 //---------------------------------------------------------------------------------------------------------
54 
55  if(percentage>=100) return;
56 
57  // Stops the watch
58  Int_t seconds = Int_t(watch.RealTime());
59  Int_t hours = seconds / 3600;
60  hours = hours < 0 ? 0 : hours;
61  seconds = seconds - hours * 3600;
62  Int_t mins = seconds / 60;
63  mins = mins < 0 ? 0 : mins;
64 
65  seconds = seconds - mins * 60;
66 
67  counter++;
68  double ratio = double(counter)/maxEntry;
69  // std::cout << ratio << "\t" << counter << "\t" << maxEntry << std::endl;
70 
71  if(ratio*100 > percentage){
72 
73  while(ratio*100 > percentage){
74  percentage++;
75  }
76 
77  // fprintf(stderr, "\n\033[F\033[J");
78  // std::cout << std::endl;
79  // fprintf(stderr, "\033[F\033[J");
80  fprintf(stderr, "\r");
81 
82  // Show the percentage complete.
83  fprintf(stderr, ANSI_COLOR_RED);
84  fprintf(stderr, "%3u%%", (UInt_t)(percentage) );
85  fprintf(stderr, ANSI_COLOR_RESET);
86  fprintf(stderr, " [");
87 
88  // Show the load bar.
89  fprintf(stderr, ANSI_COLOR_BLUE);
90  fprintf(stderr, "%02d:%02d:%02d", hours, mins, seconds);
91  for (UInt_t i=8; i<percentage; i++){
92  fprintf(stderr, "=");
93  }
94  fprintf(stderr, ANSI_COLOR_RESET);
95 
96  Int_t startSpace = percentage > 8 ? percentage : 8;
97  for (Int_t i=startSpace; i<100; i++){
98  fprintf(stderr, " ");
99  }
100 
101  fprintf(stderr, "]");
102  }
103 
104  if(percentage>=100) fprintf(stderr, "\n");
105  watch.Start(kFALSE);
106  return;
107 
108 }
109 
110 
111 
112 
113 
114 //---------------------------------------------------------------------------------------------------------
119  std::cout << percentage << "\t" << counter << "\t" << maxEntry << std::endl;
120 }
ProgressBar()
Default constructor - don&#39;t use this.
Definition: ProgressBar.cxx:19
void operator++(int)
Increment operator, use when you have completed one iteration of the main loop.
Definition: ProgressBar.cxx:53
void status()
For debugging, prints state of internal variables.