Libosmium  2.15.6
Fast and flexible C++ library for working with OpenStreetMap data
progress_bar.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_UTIL_PROGRESS_BAR_HPP
2 #define OSMIUM_UTIL_PROGRESS_BAR_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2020 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <cassert>
37 #include <cstddef>
38 #include <iostream>
39 
40 namespace osmium {
41 
46  class ProgressBar {
47 
48  enum {
50  };
51 
52  static const char* bar(std::size_t len = full_length) noexcept {
53  static const char* s = "======================================================================";
54  assert(len <= full_length);
55  return s + full_length - len;
56  }
57 
58  static const char* spc(std::size_t len = full_length) noexcept {
59  static const char* s = " ";
60  assert(len >= 1 && len <= full_length);
61  return s + full_length - len;
62  }
63 
64  // The max size is the file size if there is a single file and the
65  // sum of all file sizes if there are multiple files. It corresponds
66  // to 100%.
67  std::size_t m_max_size;
68 
69  // The sum of the file sizes already done.
70  std::size_t m_done_size = 0;
71 
72  // The currently read size in the current file.
73  std::size_t m_current_size = 0;
74 
75  // The percentage calculated when it was last displayed. Used to decide
76  // whether we need to update the display. Start setting is one that
77  // will always be different from any legal setting.
78  std::size_t m_prev_percent = 100 + 1;
79 
80  // Is the progress bar enabled at all?
81  bool m_enable;
82 
83  // Used to make sure we do cleanup in the destructor if it was not
84  // already done.
85  bool m_do_cleanup = true;
86 
87  void display() {
88  const std::size_t percent = 100 * (m_done_size + m_current_size) / m_max_size;
89  if (m_prev_percent == percent) {
90  return;
91  }
92  m_prev_percent = percent;
93 
94  const auto num = static_cast<std::size_t>(percent * (full_length / 100.0));
95  std::cerr << '[';
96  if (num >= full_length) {
97  std::cerr << bar();
98  } else {
99  std::cerr << bar(num) << '>' << spc(full_length - num);
100  }
101  std::cerr << "] ";
102  if (percent < 10) {
103  std::cerr << ' ';
104  }
105  if (percent < 100) {
106  std::cerr << ' ';
107  }
108  std::cerr << percent << "% \r";
109  }
110 
111  public:
112 
120  ProgressBar(std::size_t max_size, bool enable) noexcept :
121  m_max_size(max_size),
122  m_enable(max_size > 0 && enable) {
123  }
124 
125  ProgressBar(const ProgressBar&) = delete;
126  ProgressBar& operator=(const ProgressBar&) = delete;
127 
128  ProgressBar(ProgressBar&&) noexcept = default;
129  ProgressBar& operator=(ProgressBar&&) = default;
130 
131  ~ProgressBar() {
132  if (m_do_cleanup) {
133  try {
134  done();
135  } catch (...) {
136  // Swallow any exceptions, because a destructor should
137  // not throw.
138  }
139  }
140  }
141 
150  void update(std::size_t current_size) {
151  if (!m_enable) {
152  return;
153  }
154 
155  m_current_size = current_size;
156 
157  display();
158  }
159 
166  void file_done(std::size_t file_size) {
167  if (m_enable) {
168  m_done_size += file_size;
169  m_current_size = 0;
170  display();
171  }
172  }
173 
179  void done() {
180  m_do_cleanup = false;
181  if (m_enable) {
182  m_done_size = m_max_size;
183  m_current_size = 0;
184  display();
185  std::cerr << '\n';
186  }
187  }
188 
194  void remove() {
195  if (m_enable) {
196  std::cerr << spc() << " \r";
197  m_prev_percent = 100 + 1;
198  }
199  }
200 
201  }; // class ProgressBar
202 
203 } // namespace osmium
204 
205 #endif // OSMIUM_UTIL_PROGRESS_BAR_HPP
static const char * spc(std::size_t len=full_length) noexcept
Definition: progress_bar.hpp:58
static const char * bar(std::size_t len=full_length) noexcept
Definition: progress_bar.hpp:52
std::size_t m_max_size
Definition: progress_bar.hpp:67
std::size_t m_done_size
Definition: progress_bar.hpp:70
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
bool m_enable
Definition: progress_bar.hpp:81
Definition: progress_bar.hpp:46
Definition: progress_bar.hpp:49
bool m_do_cleanup
Definition: progress_bar.hpp:85
std::size_t m_prev_percent
Definition: progress_bar.hpp:78
void display()
Definition: progress_bar.hpp:87
std::size_t m_current_size
Definition: progress_bar.hpp:73