Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
progress_bar.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
9
10#include <stdexcept>
11#include <string>
12
13namespace momentum {
14
15ProgressBar::ProgressBar(const std::string& prefix, const size_t numOperations) {
16 using namespace indicators;
17
18 // Reserve 9 columns for the brackets, leading space, and percentage suffix
19 // (e.g., " 100%") so the total rendered line stays within kMaxWidth.
20 constexpr size_t kReservedColumns = 9;
21 // Throw `std::invalid_argument` directly rather than via `MT_THROW_IF` and avoid pulling in
22 // any momentum or third-party headers — `:common` already depends on `:progress_bar`, so
23 // reaching back through `momentum/common/exception.h` would create a cyclic dependency, and
24 // `<fmt>` is not in the `:progress_bar` BUCK target's deps either.
25 if (prefix.size() + kReservedColumns >= kMaxWidth) {
26 throw std::invalid_argument(
27 "ProgressBar prefix is too long: prefix.size()=" + std::to_string(prefix.size()) +
28 " leaves no room within kMaxWidth=" + std::to_string(kMaxWidth));
29 }
30 bar_.set_option(option::BarWidth(kMaxWidth - prefix.size() - kReservedColumns));
31 bar_.set_option(option::MaxProgress(numOperations));
32 bar_.set_option(option::Start{"["});
33 bar_.set_option(option::Fill{"="});
34 bar_.set_option(option::Lead{">"});
35 bar_.set_option(option::Remainder{" "});
36 bar_.set_option(option::End{"]"});
37 bar_.set_option(option::PrefixText{prefix});
38 bar_.set_option(option::ShowPercentage{true});
39 bar_.set_option(option::FontStyles{std::vector<FontStyle>{FontStyle::bold}});
40}
41
42void ProgressBar::increment(size_t count) {
43 bar_.set_progress(bar_.current() + count);
44}
45
46void ProgressBar::set(size_t count) {
47 bar_.set_progress(count);
48}
49
51 return bar_.current();
52}
53
54} // namespace momentum
void set(size_t count)
Sets progress to the absolute value count and redraws the bar.
void increment(size_t count=1)
Advances progress by count (default 1) and redraws the bar.
size_t getCurrentProgress()
Returns the current absolute progress count (not a percentage).
ProgressBar(const std::string &prefix, size_t numOperations)
Constructs the progress bar and renders its initial (zero-progress) state.