From 6be9c8005fde7b2a269744be6f2927497310e0e1 Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Mon, 25 May 2020 09:35:29 -0500 Subject: [PATCH] Added terminal size checks in indeterminate progress bar --- include/indicators/details/stream_helper.hpp | 3 +- .../indicators/indeterminate_progress_bar.hpp | 47 +++++++++++++++++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/include/indicators/details/stream_helper.hpp b/include/indicators/details/stream_helper.hpp index f7e02c9..baad820 100644 --- a/include/indicators/details/stream_helper.hpp +++ b/include/indicators/details/stream_helper.hpp @@ -180,8 +180,9 @@ public: : os(os), bar_width(bar_width), fill(fill), lead(lead) {} std::ostream &write(size_t progress) { - for (size_t i = 0, current_display_width = 0; i < bar_width;) { + for (size_t i = 0; i < bar_width;) { std::string next; + size_t current_display_width = 0; if (i < progress) { next = fill; diff --git a/include/indicators/indeterminate_progress_bar.hpp b/include/indicators/indeterminate_progress_bar.hpp index b549e06..253fa73 100644 --- a/include/indicators/indeterminate_progress_bar.hpp +++ b/include/indicators/indeterminate_progress_bar.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -16,6 +17,8 @@ #include #include #include +#include +#include namespace indicators { @@ -156,6 +159,23 @@ private: template friend class DynamicProgress; std::atomic multi_progress_mode_{false}; + std::pair get_prefix_text() { + std::stringstream os; + os << get_value(); + const auto result = os.str(); + const auto result_size = result.size(); + return {result, result_size}; + } + + std::pair get_postfix_text() { + std::stringstream os; + os << " " << get_value(); + + const auto result = os.str(); + const auto result_size = result.size(); + return {result, result_size}; + } + public: void print_progress(bool from_multi_progress = false) { std::lock_guard lock{mutex_}; @@ -171,7 +191,10 @@ public: for (auto &style : get_value()) details::set_font_style(os, style); - os << get_value(); + const auto prefix_pair = get_prefix_text(); + const auto prefix_text = prefix_pair.first; + const auto prefix_length = prefix_pair.second; + os << prefix_text; os << get_value(); @@ -183,11 +206,25 @@ public: os << get_value(); - if (get_value() == 0) - get_value() = 10; - os << " " << get_value() - << std::string(get_value(), ' ') << "\r"; + const auto postfix_pair = get_postfix_text(); + const auto postfix_text = postfix_pair.first; + const auto postfix_length = postfix_pair.second; + os << postfix_text; + + // Get length of prefix text and postfix text + const auto start_length = get_value().size(); + const auto bar_width = get_value(); + const auto end_length = get_value().size(); + const auto terminal_width = terminal_size().second; + // prefix + bar_width + postfix should be <= terminal_width + const int remaining = terminal_width - (prefix_length + start_length + bar_width + end_length + postfix_length); + if (remaining > 0) { + os << std::string(remaining, ' ') << "\r"; + } else if (remaining < 0) { + // Do nothing. Maybe in the future truncate postfix with ... + } os.flush(); + if (get_value() && !from_multi_progress) // Don't std::endl if calling from MultiProgress os << termcolor::reset << std::endl;