diff --git a/README.md b/README.md index 8b843b4..e919271 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ int main() { option::End{"]"}, option::PostfixText{"Extracting Archive"}, option::ForegroundColor{Color::green}, + option::ShowPercentage{true}, option::FontStyles{std::vector{FontStyle::bold}} }; @@ -127,8 +128,8 @@ int main() { option::End{" ]"}, option::PostfixText{"Loading dependency 1/4"}, option::ForegroundColor{Color::cyan}, - option::FontStyles{std::vector{FontStyle::bold}} - }; + option::ShowPercentage{true}, + option::FontStyles{std::vector{FontStyle::bold}} }; // Update bar state bar.set_progress(10); // 10% done @@ -190,6 +191,7 @@ int main() { option::End{"]"}, option::PrefixText{"Training Gaze Network 👀"}, option::ForegroundColor{Color::yellow}, + option::ShowPercentage{true}, option::ShowElapsedTime{true}, option::ShowRemainingTime{true}, option::FontStyles{std::vector{FontStyle::bold}} @@ -291,6 +293,7 @@ int main() { option::Start{"["}, option::End{"]"}, option::ForegroundColor{Color::white} , + option::ShowPercentage{true}, option::FontStyles{std::vector{FontStyle::bold}} }; @@ -340,6 +343,7 @@ int main() { option::Remainder{" "}, option::End{" ]"}, option::ForegroundColor{Color::yellow}, + option::ShowPercentage{true}, option::ShowElapsedTime{true}, option::ShowRemainingTime{true}, option::PrefixText{"Progress Bar #1 "}, @@ -356,6 +360,7 @@ int main() { option::Remainder{" "}, option::End{" ]"}, option::ForegroundColor{Color::cyan}, + option::ShowPercentage{true}, option::ShowElapsedTime{true}, option::ShowRemainingTime{true}, option::PrefixText{"Progress Bar #2 "}, @@ -371,6 +376,7 @@ int main() { option::Remainder{" "}, option::End{" ]"}, option::ForegroundColor{Color::red}, + option::ShowPercentage{true}, option::ShowElapsedTime{true}, option::ShowRemainingTime{true}, option::PrefixText{"Progress Bar #3 "}, @@ -723,6 +729,7 @@ int main() { BlockProgressBar bar{ option::BarWidth{80}, option::ForegroundColor{Color::white}, + option::ShowPercentage{true}, option::FontStyles{ std::vector{FontStyle::bold}}, option::MaxProgress{numbers.size()} diff --git a/include/indicators/block_progress_bar.hpp b/include/indicators/block_progress_bar.hpp index fcffdaf..aeadb6c 100644 --- a/include/indicators/block_progress_bar.hpp +++ b/include/indicators/block_progress_bar.hpp @@ -102,10 +102,10 @@ public: } } - void set_progress(float value) { + void set_progress(size_t value) { { std::lock_guard lock{mutex_}; - progress_ = value; + tick_ = value; } save_start_time(); print_progress(); @@ -114,7 +114,7 @@ public: void tick() { { std::lock_guard lock{mutex_}; - progress_ += 1; + tick_++; } save_start_time(); print_progress(); @@ -122,8 +122,7 @@ public: size_t current() { std::lock_guard lock{mutex_}; - return (std::min)(static_cast(progress_), - size_t(get_value())); + return (std::min)(tick_, size_t(get_value())); } bool is_completed() const { return get_value(); } @@ -147,6 +146,7 @@ private: Settings settings_; float progress_{0.0}; + size_t tick_{0}; std::chrono::time_point start_time_point_; std::mutex mutex_; @@ -175,11 +175,12 @@ private: std::pair get_postfix_text() { std::stringstream os; const auto max_progress = get_value(); + progress_ = static_cast(tick_)/max_progress; auto now = std::chrono::high_resolution_clock::now(); auto elapsed = std::chrono::duration_cast(now - start_time_point_); if (get_value()) { - os << " " << (std::min)(static_cast(progress_ / max_progress * 100.0), size_t(100)) + os << " " << (std::min)(static_cast(progress_ * 100.0), size_t(100)) << "%"; } @@ -201,9 +202,8 @@ private: if (saved_start_time) { auto eta = std::chrono::nanoseconds( - progress_ > 0 - ? static_cast(std::ceil(float(elapsed.count()) * - max_progress / progress_)) + tick_ > 0 + ? static_cast(std::ceil(float(elapsed.count()) * progress_)) : 0); auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta); details::write_duration(os, remaining); @@ -232,7 +232,7 @@ public: const auto max_progress = get_value(); if (multi_progress_mode_ && !from_multi_progress) { - if (progress_ > max_progress) { + if (tick_ > max_progress) { get_value() = true; } return; @@ -253,7 +253,7 @@ public: details::BlockProgressScaleWriter writer{os, get_value()}; - writer.write(progress_ / max_progress * 100); + writer.write(progress_ * 100); os << get_value(); @@ -278,7 +278,7 @@ public: } os.flush(); - if (progress_ > max_progress) { + if (tick_ > max_progress) { get_value() = true; } if (get_value() && @@ -289,4 +289,4 @@ public: } // namespace indicators -#endif \ No newline at end of file +#endif diff --git a/include/indicators/display_width.hpp b/include/indicators/display_width.hpp index c937d2b..b1283ff 100644 --- a/include/indicators/display_width.hpp +++ b/include/indicators/display_width.hpp @@ -311,7 +311,7 @@ static inline int mk_wcswidth_cjk(const wchar_t *pwcs, size_t n) { // convert UTF-8 string to wstring #ifdef _MSC_VER static inline std::wstring utf8_decode(const std::string& s) { - auto r = setlocale(LC_ALL, ""); + auto r = setlocale(LC_ALL, NULL); std::string curLocale; if (r) curLocale = r; @@ -327,7 +327,7 @@ static inline std::wstring utf8_decode(const std::string& s) { } #else static inline std::wstring utf8_decode(const std::string& s) { - auto r = setlocale(LC_ALL, ""); + auto r = setlocale(LC_ALL, NULL); std::string curLocale; if (r) curLocale = r; diff --git a/samples/block_progress_bar.cpp b/samples/block_progress_bar.cpp index b2be760..44b2525 100644 --- a/samples/block_progress_bar.cpp +++ b/samples/block_progress_bar.cpp @@ -14,10 +14,10 @@ int main() { std::vector{indicators::FontStyle::bold}}}; // Update bar state - auto progress = 0.0f; + size_t progress = 0; while (true) { bar.set_progress(progress); - progress += 0.25f; + progress++; if (bar.is_completed()) break; std::this_thread::sleep_for(std::chrono::milliseconds(50));