progress_bar.h
Go to the documentation of this file.
1 #include <chrono>
2 #include <iostream>
3 #include <string>
4 
6 {
7 public:
9  ProgressBar(int total, int barwidth) : initialized_(false), barwidth_(barwidth), total_(total)
10  {
11  init(total, barwidth);
12  }
13 
15  {
16  // std::cout << std::endl;
17  }
18 
19  void init(int total, int barwidth)
20  {
21  initialized_ = false;
22  barwidth_ = barwidth;
23  total_ = total;
24  last_completed_ = 0;
25  }
26 
27  void set_theme_line() { bars_ = {"─", "─", "─", "╾", "╾", "╾", "╾", "━", "═"}; }
28  void set_theme_circle() { bars_ = {" ", "◓", "◑", "◒", "◐", "◓", "◑", "◒", "#"}; }
29  void set_theme_braille() { bars_ = {" ", "⡀", "⡄", "⡆", "⡇", "⡏", "⡟", "⡿", "⣿"}; }
30  void set_theme_braille_spin() { bars_ = {" ", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠇", "⠿"}; }
31 
32  void print(int completed)
33  {
34  if (!initialized_)
35  {
36  last_print_time_ = std::chrono::system_clock::now();
37  start_time_ = std::chrono::system_clock::now();
38  initialized_ = true;
39  }
40  std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
41 
42  // limit printing to about 30 Hz
43  if (std::chrono::duration_cast<std::chrono::milliseconds>(now - last_print_time_).count() > 33
44  || completed == total_)
45  {
46  double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time_).count() / 1000.0;
47  last_print_time_ = now;
48  std::cout << " \r [";
49  double pos = barwidth_ * (completed / (double)total_);
50  for (int i = 0; i < barwidth_; ++i)
51  if (i < floor(pos))
52  std::cout << *(bars_.end() - 1);
53  else if (i == floor(pos))
54  std::cout << bars_[round((pos - floor(pos)) * (bars_.size() - 1))];
55  else
56  std::cout << " ";
57  std::cout << "] ";
58  printf("%.0f%% ", (completed / (double)total_) * 100.0);
59  double it_s = completed / elapsed;
60  std::string left_stamp = ms_to_stamp(((total_ - completed) / it_s) * 1000);
61  std::string elapsed_stamp = ms_to_stamp(elapsed * 1000.0);
62  printf("[%s<%s, %.2fit/s] ", elapsed_stamp.c_str(), left_stamp.c_str(), it_s);
63  std::cout.flush();
64  }
65  last_completed_ = completed;
66  }
67 
68  void finished() { print(total_); }
69 
70 private:
71  std::string ms_to_stamp(int ms)
72  {
73  if (ms <= 0.0)
74  {
75  return "";
76  }
77  int millis = ms % 1000;
78  int sec = ((ms - millis) % (60 * 1000)) / 1000;
79  int min = ((ms - (millis + sec * 1000)) % (60 * 60 * 1000)) / (60 * 1000);
80  int hour = ((ms - (millis + (sec + min * 60) * 1000)) % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000);
81  int day = ((ms - (millis + (sec + (min + hour * 60) * 60) * 1000)) / (24 * 60 * 60 * 1000)) / (24 * 60 * 60 * 1000);
82  char buf[25];
83  int n;
84  if (day > 0)
85  n = sprintf(buf, "%d:%d:%02d:%02d:%03d", day, hour, min, sec, millis);
86  else if (hour > 0)
87  n = sprintf(buf, "%d:%02d:%02d:%03d", hour, min, sec, millis);
88  else if (min > 0)
89  n = sprintf(buf, "%d:%02d:%03d", min, sec, millis);
90  else if (sec > 0)
91  n = sprintf(buf, "%d:%03d", sec, millis);
92  else
93  n = sprintf(buf, "%d", millis);
94  std::string out(buf);
95  return out;
96  }
97 
98  // }
99 
101  int total_;
104  std::vector<const char*> bars_ = {" ", "▏", "▎", "▍", "▋", "▋", "▊", "▉", "▉", "█"};
105 
106  std::chrono::system_clock::time_point start_time_;
107  std::chrono::system_clock::time_point last_print_time_;
108 };
std::chrono::system_clock::time_point last_print_time_
Definition: progress_bar.h:107
int last_completed_
Definition: progress_bar.h:103
uint32_t millis(void)
void set_theme_circle()
Definition: progress_bar.h:28
void finished()
Definition: progress_bar.h:68
void init(int total, int barwidth)
Definition: progress_bar.h:19
void set_theme_braille_spin()
Definition: progress_bar.h:30
void print(int completed)
Definition: progress_bar.h:32
void set_theme_line()
Definition: progress_bar.h:27
#define printf
#define sprintf
std::vector< const char * > bars_
Definition: progress_bar.h:104
ProgressBar(int total, int barwidth)
Definition: progress_bar.h:9
uint32_t out
std::chrono::system_clock::time_point start_time_
Definition: progress_bar.h:106
int i
int min(int a, int b)
std::string ms_to_stamp(int ms)
Definition: progress_bar.h:71
bool initialized_
Definition: progress_bar.h:102
void set_theme_braille()
Definition: progress_bar.h:29


rosflight_utils
Author(s):
autogenerated on Thu Apr 15 2021 05:10:06