From 6549b77d0b2d04f54897a2511045d306656ce10a Mon Sep 17 00:00:00 2001 From: Daniel Breitlauch Date: Tue, 4 Apr 2023 12:32:37 +0200 Subject: [PATCH] remove signed to unsigned conversion + check for errors --- include/indicators/block_progress_bar.hpp | 8 ++++--- .../indicators/indeterminate_progress_bar.hpp | 8 ++++--- include/indicators/progress_bar.hpp | 8 ++++--- single_include/indicators/indicators.hpp | 24 ++++++++++++------- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/include/indicators/block_progress_bar.hpp b/include/indicators/block_progress_bar.hpp index 1c26992..fcffdaf 100644 --- a/include/indicators/block_progress_bar.hpp +++ b/include/indicators/block_progress_bar.hpp @@ -164,7 +164,7 @@ private: } } - std::pair get_prefix_text() { + std::pair get_prefix_text() { std::stringstream os; os << get_value(); const auto result = os.str(); @@ -172,7 +172,7 @@ private: return {result, result_size}; } - std::pair get_postfix_text() { + std::pair get_postfix_text() { std::stringstream os; const auto max_progress = get_value(); auto now = std::chrono::high_resolution_clock::now(); @@ -269,7 +269,9 @@ public: 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) { + if (prefix_length == -1 || postfix_length == -1) { + os << "\r"; + } else if (remaining > 0) { os << std::string(remaining, ' ') << "\r"; } else if (remaining < 0) { // Do nothing. Maybe in the future truncate postfix with ... diff --git a/include/indicators/indeterminate_progress_bar.hpp b/include/indicators/indeterminate_progress_bar.hpp index 08d6ae4..f77dd5a 100644 --- a/include/indicators/indeterminate_progress_bar.hpp +++ b/include/indicators/indeterminate_progress_bar.hpp @@ -160,7 +160,7 @@ private: template friend class DynamicProgress; std::atomic multi_progress_mode_{false}; - std::pair get_prefix_text() { + std::pair get_prefix_text() { std::stringstream os; os << get_value(); const auto result = os.str(); @@ -168,7 +168,7 @@ private: return {result, result_size}; } - std::pair get_postfix_text() { + std::pair get_postfix_text() { std::stringstream os; os << " " << get_value(); @@ -219,7 +219,9 @@ public: 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) { + if (prefix_length == -1 || postfix_length == -1) { + os << "\r"; + } else if (remaining > 0) { os << std::string(remaining, ' ') << "\r"; } else if (remaining < 0) { // Do nothing. Maybe in the future truncate postfix with ... diff --git a/include/indicators/progress_bar.hpp b/include/indicators/progress_bar.hpp index 2d547da..ce22957 100644 --- a/include/indicators/progress_bar.hpp +++ b/include/indicators/progress_bar.hpp @@ -216,7 +216,7 @@ private: } } - std::pair get_prefix_text() { + std::pair get_prefix_text() { std::stringstream os; os << get_value(); const auto result = os.str(); @@ -224,7 +224,7 @@ private: return {result, result_size}; } - std::pair get_postfix_text() { + std::pair get_postfix_text() { std::stringstream os; const auto max_progress = get_value(); @@ -338,7 +338,9 @@ public: 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) { + if (prefix_length == -1 || postfix_length == -1) { + os << "\r"; + } else if (remaining > 0) { os << std::string(remaining, ' ') << "\r"; } else if (remaining < 0) { // Do nothing. Maybe in the future truncate postfix with ... diff --git a/single_include/indicators/indicators.hpp b/single_include/indicators/indicators.hpp index c961a93..6123158 100644 --- a/single_include/indicators/indicators.hpp +++ b/single_include/indicators/indicators.hpp @@ -2128,7 +2128,7 @@ private: } } - std::pair get_prefix_text() { + std::pair get_prefix_text() { std::stringstream os; os << get_value(); const auto result = os.str(); @@ -2136,7 +2136,7 @@ private: return {result, result_size}; } - std::pair get_postfix_text() { + std::pair get_postfix_text() { std::stringstream os; const auto max_progress = get_value(); @@ -2250,7 +2250,9 @@ public: 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) { + if (prefix_length == -1 || postfix_length == -1) { + os << "\r"; + } else if (remaining > 0) { os << std::string(remaining, ' ') << "\r"; } else if (remaining < 0) { // Do nothing. Maybe in the future truncate postfix with ... @@ -2437,7 +2439,7 @@ private: } } - std::pair get_prefix_text() { + std::pair get_prefix_text() { std::stringstream os; os << get_value(); const auto result = os.str(); @@ -2445,7 +2447,7 @@ private: return {result, result_size}; } - std::pair get_postfix_text() { + std::pair get_postfix_text() { std::stringstream os; const auto max_progress = get_value(); auto now = std::chrono::high_resolution_clock::now(); @@ -2542,7 +2544,9 @@ public: 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) { + if (prefix_length == -1 || postfix_length == -1) { + os << "\r"; + } else if (remaining > 0) { os << std::string(remaining, ' ') << "\r"; } else if (remaining < 0) { // Do nothing. Maybe in the future truncate postfix with ... @@ -2724,7 +2728,7 @@ private: template friend class DynamicProgress; std::atomic multi_progress_mode_{false}; - std::pair get_prefix_text() { + std::pair get_prefix_text() { std::stringstream os; os << get_value(); const auto result = os.str(); @@ -2732,7 +2736,7 @@ private: return {result, result_size}; } - std::pair get_postfix_text() { + std::pair get_postfix_text() { std::stringstream os; os << " " << get_value(); @@ -2783,7 +2787,9 @@ public: 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) { + if (prefix_length == -1 || postfix_length == -1) { + os << "\r"; + } else if (remaining > 0) { os << std::string(remaining, ' ') << "\r"; } else if (remaining < 0) { // Do nothing. Maybe in the future truncate postfix with ...