94 std::atomic<int> m_progress{0};
96 std::atomic<int> m_last_fill{-1};
101 static constexpr std::size_t bar_width = (Length >= 7) ? (Length - 7) : 1;
110 template <
typename T,
typename = std::enable_if_t<std::is_
integral_v<T>>>
112 : m_max(static_cast<int>(
max)), m_print(print) {
121 const int progress = ++m_progress;
122 if (progress > m_max)
124 const bool is_final = (progress == m_max);
127 const int current = int(bar_width *
double(progress) /
double(m_max));
128 if (!is_final && current <= m_last_fill.load(std::memory_order_relaxed))
131 print_prog_bar(progress);
138 void print_prog_bar(
int progress) {
139 const bool is_tty = isatty(fileno(stdout));
141 print_prog_bar_notty(progress);
145 const bool is_final = (progress == m_max);
148 const auto current = int(bar_width *
double(progress) /
double(m_max));
149 char buf[Length + 2];
152 for (
int j = 0; j < current; ++j)
154 for (
int j = current; j < (int)bar_width; ++j)
158 p += snprintf(p,
sizeof(buf) - std::size_t(p - buf) - 1,
"%d%%",
159 int(100.0 *
double(progress) /
double(m_max)));
160 *p++ = is_final ?
'\n' :
'\r';
165 bool did_print =
false;
177 m_last_fill.store(current, std::memory_order_relaxed);
183 void print_prog_bar_notty(
int progress) {
184 const bool is_initial = (progress == 0);
185 const bool is_final = (progress == m_max);
188 is_final ? 100 : int(100.0 *
double(progress) /
double(m_max));
191 static constexpr int n_entries =
static_cast<int>(Length) / 5;
192 static constexpr int interval =
193 (n_entries > 1) ? (100 / (n_entries - 1)) : 100;
194 const int slot = pct / interval;
196 if (!is_initial && !is_final &&
197 slot <= m_last_fill.load(std::memory_order_relaxed))
202 snprintf(buf,
sizeof(buf),
"100%%\n");
204 snprintf(buf,
sizeof(buf),
"%d%%, ", pct);
206 bool did_print =
false;
217 if (!is_final && did_print)
218 m_last_fill.store(slot, std::memory_order_relaxed);
ProgressBar(T max, bool print=true)
Construct progress bar for max iterations.
Definition Widgets.hpp:111
void progbar(int i, int max, int length=50)
Basic progress bar. Prints new line if (and only if) i==(max-1)
Definition Widgets.hpp:17
T max(T first, Args... rest)
Returns the maximum of any number of parameters (variadic).
Definition Maths.hpp:28