Libosmium  2.14.2
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-2018 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  static constexpr const std::size_t full_length = 70;
49 
50  static const char* bar(std::size_t len = full_length) noexcept {
51  assert(len <= full_length);
52  return "======================================================================"
53  + full_length - len;
54  }
55 
56  static const char* spc(std::size_t len = full_length) noexcept {
57  assert(len >= 1 && len <= full_length);
58  return " "
59  + full_length - len;
60  }
61 
62  // The max size is the file size if there is a single file and the
63  // sum of all file sizes if there are multiple files. It corresponds
64  // to 100%.
65  std::size_t m_max_size;
66 
67  // The sum of the file sizes already done.
68  std::size_t m_done_size = 0;
69 
70  // The currently read size in the current file.
71  std::size_t m_current_size = 0;
72 
73  // The percentage calculated when it was last displayed. Used to decide
74  // whether we need to update the display. Start setting is one that
75  // will always be different from any legal setting.
76  std::size_t m_prev_percent = 100 + 1;
77 
78  // Is the progress bar enabled at all?
79  bool m_enable;
80 
81  // Used to make sure we do cleanup in the destructor if it was not
82  // already done.
83  bool m_do_cleanup = true;
84 
85  void display() {
86  const std::size_t percent = 100 * (m_done_size + m_current_size) / m_max_size;
87  if (m_prev_percent == percent) {
88  return;
89  }
90  m_prev_percent = percent;
91 
92  const auto num = static_cast<std::size_t>(percent * (full_length / 100.0));
93  std::cerr << '[';
94  if (num >= full_length) {
95  std::cerr << bar();
96  } else {
97  std::cerr << bar(num) << '>' << spc(full_length - num);
98  }
99  std::cerr << "] ";
100  if (percent < 10) {
101  std::cerr << ' ';
102  }
103  if (percent < 100) {
104  std::cerr << ' ';
105  }
106  std::cerr << percent << "% \r";
107  }
108 
109  public:
110 
118  ProgressBar(std::size_t max_size, bool enable) noexcept :
119  m_max_size(max_size),
120  m_enable(max_size > 0 && enable) {
121  }
122 
123  ProgressBar(const ProgressBar&) = delete;
124  ProgressBar& operator=(const ProgressBar&) = delete;
125 
126  ProgressBar(ProgressBar&&) noexcept = default;
127  ProgressBar& operator=(ProgressBar&&) = default;
128 
129  ~ProgressBar() {
130  if (m_do_cleanup) {
131  try {
132  done();
133  } catch (...) {
134  // Swallow any exceptions, because a destructor should
135  // not throw.
136  }
137  }
138  }
139 
148  void update(std::size_t current_size) {
149  if (!m_enable) {
150  return;
151  }
152 
153  m_current_size = current_size;
154 
155  display();
156  }
157 
164  void file_done(std::size_t file_size) {
165  if (m_enable) {
166  m_done_size += file_size;
167  m_current_size = 0;
168  display();
169  }
170  }
171 
177  void done() {
178  m_do_cleanup = false;
179  if (m_enable) {
180  m_done_size = m_max_size;
181  m_current_size = 0;
182  display();
183  std::cerr << '\n';
184  }
185  }
186 
192  void remove() {
193  if (m_enable) {
194  std::cerr << spc() << " \r";
195  m_prev_percent = 100 + 1;
196  }
197  }
198 
199  }; // class ProgressBar
200 
201 } // namespace osmium
202 
203 #endif // OSMIUM_UTIL_PROGRESS_BAR_HPP
static const char * spc(std::size_t len=full_length) noexcept
Definition: progress_bar.hpp:56
static const char * bar(std::size_t len=full_length) noexcept
Definition: progress_bar.hpp:50
std::size_t m_max_size
Definition: progress_bar.hpp:65
std::size_t m_done_size
Definition: progress_bar.hpp:68
static constexpr const std::size_t full_length
Definition: progress_bar.hpp:48
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
bool m_enable
Definition: progress_bar.hpp:79
Definition: progress_bar.hpp:46
bool m_do_cleanup
Definition: progress_bar.hpp:83
std::size_t m_prev_percent
Definition: progress_bar.hpp:76
void display()
Definition: progress_bar.hpp:85
std::size_t m_current_size
Definition: progress_bar.hpp:71