From d4a38eb034fad6fdfb707df5d1b8e3c482323cf3 Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Sat, 22 Feb 2020 11:02:16 +0530 Subject: [PATCH] Closes 25 - Changed to --- README.md | 2 +- include/indicators/details/stream_helper.hpp | 4 ++-- include/indicators/dynamic_progress.hpp | 2 +- include/indicators/multi_progress.hpp | 7 +++++++ include/indicators/progress_bar.hpp | 14 +++++++------- include/indicators/progress_spinner.hpp | 10 +++++----- 6 files changed, 23 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 5c4d580..528ce9f 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/include/indicators/details/stream_helper.hpp b/include/indicators/details/stream_helper.hpp index d85c2e9..eececed 100644 --- a/include/indicators/details/stream_helper.hpp +++ b/include/indicators/details/stream_helper.hpp @@ -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(progress * static_cast(bar_width) / 100.0); + std::ostream &write(size_t progress) { + auto pos = static_cast(progress * bar_width / 100.0); for (size_t i = 0; i < bar_width; ++i) { if (i < pos) os << fill; diff --git a/include/indicators/dynamic_progress.hpp b/include/indicators/dynamic_progress.hpp index bc8f8a7..f97a7aa 100644 --- a/include/indicators/dynamic_progress.hpp +++ b/include/indicators/dynamic_progress.hpp @@ -123,7 +123,7 @@ private: for (size_t i = 0; i < total_count_; ++i) std::cout << "\x1b[A"; } - for (auto &bar: bars_) { + for (auto &bar : bars_) { bar.get().print_progress(true); std::cout << "\n"; } diff --git a/include/indicators/multi_progress.hpp b/include/indicators/multi_progress.hpp index ae8065f..41c4e86 100644 --- a/include/indicators/multi_progress.hpp +++ b/include/indicators/multi_progress.hpp @@ -45,6 +45,13 @@ public: } } + template + 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 typename std::enable_if<(index >= 0 && index < count), void>::type set_progress(float value) { if (!bars_[index].get().is_completed()) diff --git a/include/indicators/progress_bar.hpp b/include/indicators/progress_bar.hpp index 200eac5..4959755 100644 --- a/include/indicators/progress_bar.hpp +++ b/include/indicators/progress_bar.hpp @@ -126,9 +126,9 @@ public: } } - void set_progress(float new_progress) { + void set_progress(size_t new_progress) { { - std::lock_guard lck(mutex_); + std::lock_guard lock(mutex_); progress_ = new_progress; } @@ -147,7 +147,7 @@ public: size_t current() { std::lock_guard lock{mutex_}; - return std::min(static_cast(progress_), size_t(100)); + return std::min(progress_, size_t(100)); } bool is_completed() const { return get_value(); } @@ -169,7 +169,7 @@ private: return details::get_value(settings_).value; } - float progress_{0}; + size_t progress_{0}; Settings settings_; std::chrono::nanoseconds elapsed_; std::chrono::time_point start_time_point_; @@ -192,7 +192,7 @@ private: void print_progress(bool from_multi_progress = false) { std::lock_guard lock{mutex_}; if (multi_progress_mode_ && !from_multi_progress) { - if (progress_ > 100.0) { + if (progress_ > 100) { get_value() = true; } return; @@ -217,7 +217,7 @@ private: std::cout << get_value(); if (get_value()) { - std::cout << " " << std::min(static_cast(progress_), size_t(100)) << "%"; + std::cout << " " << std::min(progress_, size_t(100)) << "%"; } if (get_value()) { @@ -246,7 +246,7 @@ private: << std::string(get_value(), ' ') << "\r"; std::cout.flush(); - if (progress_ > 100.0) { + if (progress_ > 100) { get_value() = true; } if (get_value() && diff --git a/include/indicators/progress_spinner.hpp b/include/indicators/progress_spinner.hpp index 9a4d7c2..a69c316 100644 --- a/include/indicators/progress_spinner.hpp +++ b/include/indicators/progress_spinner.hpp @@ -119,7 +119,7 @@ public: } } - void set_progress(float value) { + void set_progress(size_t value) { { std::lock_guard lock{mutex_}; progress_ = value; @@ -139,7 +139,7 @@ public: size_t current() { std::lock_guard lock{mutex_}; - return std::min(static_cast(progress_), size_t(100)); + return std::min(progress_, size_t(100)); } bool is_completed() const { return get_value(); } @@ -151,7 +151,7 @@ public: private: Settings settings_; - float progress_{0.0}; + size_t progress_{0}; size_t index_{0}; std::chrono::time_point start_time_point_; std::mutex mutex_; @@ -189,7 +189,7 @@ private: std::cout << get_value() [index_ % get_value().size()]; if (get_value()) { - std::cout << " " << std::min(static_cast(progress_), size_t(100)) << "%"; + std::cout << " " << std::min(progress_, size_t(100)) << "%"; } if (get_value()) { @@ -219,7 +219,7 @@ private: << "\r"; std::cout.flush(); index_ += 1; - if (progress_ > 100.0) { + if (progress_ > 100) { get_value() = true; } if (get_value())