Libosmium: include/osmium/util/progress_bar.hpp Source File

Libosmium Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
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
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2026 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <cassert>
37#include <cstddef>
38#include <iostream>
39
40namespace osmium {
41
46 class ProgressBar {
47
48 enum {
49 full_length = 70
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>(static_cast<double>(percent) * (static_cast<double>(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 (...) { // NOLINT(bugprone-empty-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
osmium::ProgressBar::m_enable
bool m_enable
Definition progress_bar.hpp:81
osmium::ProgressBar::operator=
ProgressBar & operator=(const ProgressBar &)=delete
osmium::ProgressBar::m_prev_percent
std::size_t m_prev_percent
Definition progress_bar.hpp:78
osmium::ProgressBar::ProgressBar
ProgressBar(std::size_t max_size, bool enable) noexcept
Definition progress_bar.hpp:120
osmium::ProgressBar::m_done_size
std::size_t m_done_size
Definition progress_bar.hpp:70
osmium::ProgressBar::remove
void remove()
Definition progress_bar.hpp:194
osmium::ProgressBar::m_do_cleanup
bool m_do_cleanup
Definition progress_bar.hpp:85
osmium::ProgressBar::bar
static const char * bar(std::size_t len=full_length) noexcept
Definition progress_bar.hpp:52
osmium::ProgressBar::ProgressBar
ProgressBar(const ProgressBar &)=delete
osmium::ProgressBar::spc
static const char * spc(std::size_t len=full_length) noexcept
Definition progress_bar.hpp:58
osmium::ProgressBar::full_length
@ full_length
Definition progress_bar.hpp:49
osmium::ProgressBar::display
void display()
Definition progress_bar.hpp:87
osmium::ProgressBar::ProgressBar
ProgressBar(ProgressBar &&) noexcept=default
osmium::ProgressBar::m_max_size
std::size_t m_max_size
Definition progress_bar.hpp:67
osmium::ProgressBar::file_done
void file_done(std::size_t file_size)
Definition progress_bar.hpp:166
osmium::ProgressBar::m_current_size
std::size_t m_current_size
Definition progress_bar.hpp:73
osmium::ProgressBar::done
void done()
Definition progress_bar.hpp:179
osmium::ProgressBar::update
void update(std::size_t current_size)
Definition progress_bar.hpp:150
osmium::util::file_size
std::size_t file_size(int fd)
Definition file.hpp:109
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53