mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-16 04:18:51 +08:00
Closes 25 - Changed to
This commit is contained in:
@@ -53,7 +53,7 @@ To introduce a progress bar in your application, include `indicators/progress_ba
|
||||
^^^^^^^^^^^^^ Bar Width ^^^^^^^^^^^^^^^
|
||||
```
|
||||
|
||||
The amount of progress in ProgressBar is maintained as a float in range `[0, 100]`. When progress reaches 100, the progression is complete.
|
||||
The amount of progress in ProgressBar is maintained as a size_t in range `[0, 100]`. When progress reaches 100, the progression is complete.
|
||||
|
||||
From application-level code, there are two ways in which you can update this progress:
|
||||
|
||||
|
||||
@@ -102,8 +102,8 @@ public:
|
||||
const std::string &lead, const std::string &remainder)
|
||||
: os(os), bar_width(bar_width), fill(fill), lead(lead), remainder(remainder) {}
|
||||
|
||||
std::ostream &write(float progress) {
|
||||
auto pos = static_cast<size_t>(progress * static_cast<float>(bar_width) / 100.0);
|
||||
std::ostream &write(size_t progress) {
|
||||
auto pos = static_cast<size_t>(progress * bar_width / 100.0);
|
||||
for (size_t i = 0; i < bar_width; ++i) {
|
||||
if (i < pos)
|
||||
os << fill;
|
||||
|
||||
@@ -45,6 +45,13 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
template <size_t index>
|
||||
typename std::enable_if<(index >= 0 && index < count), void>::type set_progress(size_t value) {
|
||||
if (!bars_[index].get().is_completed())
|
||||
bars_[index].get().set_progress(value);
|
||||
print_progress();
|
||||
}
|
||||
|
||||
template <size_t index>
|
||||
typename std::enable_if<(index >= 0 && index < count), void>::type set_progress(float value) {
|
||||
if (!bars_[index].get().is_completed())
|
||||
|
||||
@@ -126,9 +126,9 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void set_progress(float new_progress) {
|
||||
void set_progress(size_t new_progress) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lck(mutex_);
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
progress_ = new_progress;
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ public:
|
||||
|
||||
size_t current() {
|
||||
std::lock_guard<std::mutex> lock{mutex_};
|
||||
return std::min(static_cast<size_t>(progress_), size_t(100));
|
||||
return std::min(progress_, size_t(100));
|
||||
}
|
||||
|
||||
bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
|
||||
@@ -169,7 +169,7 @@ private:
|
||||
return details::get_value<id>(settings_).value;
|
||||
}
|
||||
|
||||
float progress_{0};
|
||||
size_t progress_{0};
|
||||
Settings settings_;
|
||||
std::chrono::nanoseconds elapsed_;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> start_time_point_;
|
||||
@@ -192,7 +192,7 @@ private:
|
||||
void print_progress(bool from_multi_progress = false) {
|
||||
std::lock_guard<std::mutex> lock{mutex_};
|
||||
if (multi_progress_mode_ && !from_multi_progress) {
|
||||
if (progress_ > 100.0) {
|
||||
if (progress_ > 100) {
|
||||
get_value<details::ProgressBarOption::completed>() = true;
|
||||
}
|
||||
return;
|
||||
@@ -217,7 +217,7 @@ private:
|
||||
std::cout << get_value<details::ProgressBarOption::end>();
|
||||
|
||||
if (get_value<details::ProgressBarOption::show_percentage>()) {
|
||||
std::cout << " " << std::min(static_cast<size_t>(progress_), size_t(100)) << "%";
|
||||
std::cout << " " << std::min(progress_, size_t(100)) << "%";
|
||||
}
|
||||
|
||||
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
|
||||
@@ -246,7 +246,7 @@ private:
|
||||
<< std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ')
|
||||
<< "\r";
|
||||
std::cout.flush();
|
||||
if (progress_ > 100.0) {
|
||||
if (progress_ > 100) {
|
||||
get_value<details::ProgressBarOption::completed>() = true;
|
||||
}
|
||||
if (get_value<details::ProgressBarOption::completed>() &&
|
||||
|
||||
@@ -119,7 +119,7 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void set_progress(float value) {
|
||||
void set_progress(size_t value) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock{mutex_};
|
||||
progress_ = value;
|
||||
@@ -139,7 +139,7 @@ public:
|
||||
|
||||
size_t current() {
|
||||
std::lock_guard<std::mutex> lock{mutex_};
|
||||
return std::min(static_cast<size_t>(progress_), size_t(100));
|
||||
return std::min(progress_, size_t(100));
|
||||
}
|
||||
|
||||
bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
|
||||
@@ -151,7 +151,7 @@ public:
|
||||
|
||||
private:
|
||||
Settings settings_;
|
||||
float progress_{0.0};
|
||||
size_t progress_{0};
|
||||
size_t index_{0};
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> start_time_point_;
|
||||
std::mutex mutex_;
|
||||
@@ -189,7 +189,7 @@ private:
|
||||
std::cout << get_value<details::ProgressBarOption::spinner_states>()
|
||||
[index_ % get_value<details::ProgressBarOption::spinner_states>().size()];
|
||||
if (get_value<details::ProgressBarOption::show_percentage>()) {
|
||||
std::cout << " " << std::min(static_cast<size_t>(progress_), size_t(100)) << "%";
|
||||
std::cout << " " << std::min(progress_, size_t(100)) << "%";
|
||||
}
|
||||
|
||||
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
|
||||
@@ -219,7 +219,7 @@ private:
|
||||
<< "\r";
|
||||
std::cout.flush();
|
||||
index_ += 1;
|
||||
if (progress_ > 100.0) {
|
||||
if (progress_ > 100) {
|
||||
get_value<details::ProgressBarOption::completed>() = true;
|
||||
}
|
||||
if (get_value<details::ProgressBarOption::completed>())
|
||||
|
||||
Reference in New Issue
Block a user