Closes 25 - Changed to

This commit is contained in:
Pranav Srinivas Kumar
2020-02-22 11:02:16 +05:30
parent ec1973607d
commit d4a38eb034
6 changed files with 23 additions and 16 deletions

View File

@@ -53,7 +53,7 @@ To introduce a progress bar in your application, include `indicators/progress_ba
^^^^^^^^^^^^^ Bar Width ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ 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: From application-level code, there are two ways in which you can update this progress:

View File

@@ -102,8 +102,8 @@ public:
const std::string &lead, const std::string &remainder) const std::string &lead, const std::string &remainder)
: os(os), bar_width(bar_width), fill(fill), lead(lead), remainder(remainder) {} : os(os), bar_width(bar_width), fill(fill), lead(lead), remainder(remainder) {}
std::ostream &write(float progress) { std::ostream &write(size_t progress) {
auto pos = static_cast<size_t>(progress * static_cast<float>(bar_width) / 100.0); auto pos = static_cast<size_t>(progress * bar_width / 100.0);
for (size_t i = 0; i < bar_width; ++i) { for (size_t i = 0; i < bar_width; ++i) {
if (i < pos) if (i < pos)
os << fill; os << fill;

View File

@@ -123,7 +123,7 @@ private:
for (size_t i = 0; i < total_count_; ++i) for (size_t i = 0; i < total_count_; ++i)
std::cout << "\x1b[A"; std::cout << "\x1b[A";
} }
for (auto &bar: bars_) { for (auto &bar : bars_) {
bar.get().print_progress(true); bar.get().print_progress(true);
std::cout << "\n"; std::cout << "\n";
} }

View File

@@ -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> template <size_t index>
typename std::enable_if<(index >= 0 && index < count), void>::type set_progress(float value) { typename std::enable_if<(index >= 0 && index < count), void>::type set_progress(float value) {
if (!bars_[index].get().is_completed()) if (!bars_[index].get().is_completed())

View File

@@ -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; progress_ = new_progress;
} }
@@ -147,7 +147,7 @@ public:
size_t current() { size_t current() {
std::lock_guard<std::mutex> lock{mutex_}; 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>(); } bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
@@ -169,7 +169,7 @@ private:
return details::get_value<id>(settings_).value; return details::get_value<id>(settings_).value;
} }
float progress_{0}; size_t progress_{0};
Settings settings_; Settings settings_;
std::chrono::nanoseconds elapsed_; std::chrono::nanoseconds elapsed_;
std::chrono::time_point<std::chrono::high_resolution_clock> start_time_point_; 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) { void print_progress(bool from_multi_progress = false) {
std::lock_guard<std::mutex> lock{mutex_}; std::lock_guard<std::mutex> lock{mutex_};
if (multi_progress_mode_ && !from_multi_progress) { if (multi_progress_mode_ && !from_multi_progress) {
if (progress_ > 100.0) { if (progress_ > 100) {
get_value<details::ProgressBarOption::completed>() = true; get_value<details::ProgressBarOption::completed>() = true;
} }
return; return;
@@ -217,7 +217,7 @@ private:
std::cout << get_value<details::ProgressBarOption::end>(); std::cout << get_value<details::ProgressBarOption::end>();
if (get_value<details::ProgressBarOption::show_percentage>()) { 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>()) { if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
@@ -246,7 +246,7 @@ private:
<< std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ') << std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ')
<< "\r"; << "\r";
std::cout.flush(); std::cout.flush();
if (progress_ > 100.0) { if (progress_ > 100) {
get_value<details::ProgressBarOption::completed>() = true; get_value<details::ProgressBarOption::completed>() = true;
} }
if (get_value<details::ProgressBarOption::completed>() && if (get_value<details::ProgressBarOption::completed>() &&

View File

@@ -119,7 +119,7 @@ public:
} }
} }
void set_progress(float value) { void set_progress(size_t value) {
{ {
std::lock_guard<std::mutex> lock{mutex_}; std::lock_guard<std::mutex> lock{mutex_};
progress_ = value; progress_ = value;
@@ -139,7 +139,7 @@ public:
size_t current() { size_t current() {
std::lock_guard<std::mutex> lock{mutex_}; 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>(); } bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
@@ -151,7 +151,7 @@ public:
private: private:
Settings settings_; Settings settings_;
float progress_{0.0}; size_t progress_{0};
size_t index_{0}; size_t index_{0};
std::chrono::time_point<std::chrono::high_resolution_clock> start_time_point_; std::chrono::time_point<std::chrono::high_resolution_clock> start_time_point_;
std::mutex mutex_; std::mutex mutex_;
@@ -189,7 +189,7 @@ private:
std::cout << get_value<details::ProgressBarOption::spinner_states>() std::cout << get_value<details::ProgressBarOption::spinner_states>()
[index_ % get_value<details::ProgressBarOption::spinner_states>().size()]; [index_ % get_value<details::ProgressBarOption::spinner_states>().size()];
if (get_value<details::ProgressBarOption::show_percentage>()) { 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>()) { if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
@@ -219,7 +219,7 @@ private:
<< "\r"; << "\r";
std::cout.flush(); std::cout.flush();
index_ += 1; index_ += 1;
if (progress_ > 100.0) { if (progress_ > 100) {
get_value<details::ProgressBarOption::completed>() = true; get_value<details::ProgressBarOption::completed>() = true;
} }
if (get_value<details::ProgressBarOption::completed>()) if (get_value<details::ProgressBarOption::completed>())