OISEAU
A modern DGTD framework
Loading...
Searching...
No Matches
progress_bar.hpp
1// Copyright (C) 2026 Tiago V. L. Amorim (@tiagovla)
2//
3// This file is part of oiseau (https://github.com/tiagovla/oiseau)
4//
5// SPDX-License-Identifier: GPL-3.0-or-later
6
7#pragma once
8
9#include <cstddef>
10#include <ostream>
11
12namespace oiseau::time {
13
15class ProgressBar {
16 public:
17 ProgressBar(std::ostream* output, std::size_t total) : m_output(output), m_total(total) {}
18
19 void update(std::size_t completed) {
20 if (m_output == nullptr || m_total == 0) return;
21
22 constexpr std::size_t width = 40;
23 const auto fraction = static_cast<long double>(completed) / static_cast<long double>(m_total);
24 const auto filled = static_cast<std::size_t>(fraction * width);
25 if (filled == m_last_filled && completed != m_total) return;
26 m_last_filled = filled;
27
28 *m_output << "\rIntegrating [";
29 for (std::size_t column = 0; column < width; ++column) {
30 *m_output << (column < filled ? '#' : ' ');
31 }
32 *m_output << "] " << static_cast<unsigned>(fraction * 100.0L) << "% (" << completed << '/'
33 << m_total << ')';
34 if (completed == m_total) *m_output << '\n';
35 *m_output << std::flush;
36 }
37
38 private:
39 std::ostream* m_output;
40 std::size_t m_total;
41 std::size_t m_last_filled = 41;
42};
43
44} // namespace oiseau::time