| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 12 | namespace oiseau::time { | ||
| 13 | |||
| 14 | /// Prints a bounded number of updates on one terminal line. | ||
| 15 | class ProgressBar { | ||
| 16 | public: | ||
| 17 | 9 | ProgressBar(std::ostream* output, std::size_t total) : m_output(output), m_total(total) {} | |
| 18 | |||
| 19 | 832 | void update(std::size_t completed) { | |
| 20 |
3/4✓ Branch 0 taken 11 times.
✓ Branch 1 taken 821 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
|
832 | if (m_output == nullptr || m_total == 0) return; |
| 21 | |||
| 22 | 11 | constexpr std::size_t width = 40; | |
| 23 | 11 | const auto fraction = static_cast<long double>(completed) / static_cast<long double>(m_total); | |
| 24 | 11 | const auto filled = static_cast<std::size_t>(fraction * width); | |
| 25 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
11 | if (filled == m_last_filled && completed != m_total) return; |
| 26 | 11 | m_last_filled = filled; | |
| 27 | |||
| 28 | 11 | *m_output << "\rIntegrating ["; | |
| 29 |
2/2✓ Branch 0 taken 440 times.
✓ Branch 1 taken 11 times.
|
451 | for (std::size_t column = 0; column < width; ++column) { |
| 30 |
2/2✓ Branch 0 taken 220 times.
✓ Branch 1 taken 220 times.
|
440 | *m_output << (column < filled ? '#' : ' '); |
| 31 | } | ||
| 32 | 11 | *m_output << "] " << static_cast<unsigned>(fraction * 100.0L) << "% (" << completed << '/' | |
| 33 | 11 | << m_total << ')'; | |
| 34 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
|
11 | if (completed == m_total) *m_output << '\n'; |
| 35 | 11 | *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 | ||
| 45 |