Skip to content

Commit

Permalink
Merge pull request #121 from danielBreitlauch/master
Browse files Browse the repository at this point in the history
Bug fix: check prefix and postfix length errors
  • Loading branch information
p-ranav authored Apr 17, 2023
2 parents 5050a88 + 6549b77 commit 4ea7161
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
8 changes: 5 additions & 3 deletions include/indicators/block_progress_bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ class BlockProgressBar {
}
}

std::pair<std::string, size_t> get_prefix_text() {
std::pair<std::string, int> get_prefix_text() {
std::stringstream os;
os << get_value<details::ProgressBarOption::prefix_text>();
const auto result = os.str();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}

std::pair<std::string, size_t> get_postfix_text() {
std::pair<std::string, int> get_postfix_text() {
std::stringstream os;
const auto max_progress = get_value<details::ProgressBarOption::max_progress>();
auto now = std::chrono::high_resolution_clock::now();
Expand Down Expand Up @@ -269,7 +269,9 @@ class BlockProgressBar {
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 ...
Expand Down
8 changes: 5 additions & 3 deletions include/indicators/indeterminate_progress_bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,15 @@ class IndeterminateProgressBar {
template <typename Indicator> friend class DynamicProgress;
std::atomic<bool> multi_progress_mode_{false};

std::pair<std::string, size_t> get_prefix_text() {
std::pair<std::string, int> get_prefix_text() {
std::stringstream os;
os << get_value<details::ProgressBarOption::prefix_text>();
const auto result = os.str();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}

std::pair<std::string, size_t> get_postfix_text() {
std::pair<std::string, int> get_postfix_text() {
std::stringstream os;
os << " " << get_value<details::ProgressBarOption::postfix_text>();

Expand Down Expand Up @@ -219,7 +219,9 @@ class IndeterminateProgressBar {
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 ...
Expand Down
8 changes: 5 additions & 3 deletions include/indicators/progress_bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,15 @@ class ProgressBar {
}
}

std::pair<std::string, size_t> get_prefix_text() {
std::pair<std::string, int> get_prefix_text() {
std::stringstream os;
os << get_value<details::ProgressBarOption::prefix_text>();
const auto result = os.str();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}

std::pair<std::string, size_t> get_postfix_text() {
std::pair<std::string, int> get_postfix_text() {
std::stringstream os;
const auto max_progress =
get_value<details::ProgressBarOption::max_progress>();
Expand Down Expand Up @@ -338,7 +338,9 @@ class ProgressBar {
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 ...
Expand Down
24 changes: 15 additions & 9 deletions single_include/indicators/indicators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2128,15 +2128,15 @@ class ProgressBar {
}
}

std::pair<std::string, size_t> get_prefix_text() {
std::pair<std::string, int> get_prefix_text() {
std::stringstream os;
os << get_value<details::ProgressBarOption::prefix_text>();
const auto result = os.str();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}

std::pair<std::string, size_t> get_postfix_text() {
std::pair<std::string, int> get_postfix_text() {
std::stringstream os;
const auto max_progress =
get_value<details::ProgressBarOption::max_progress>();
Expand Down Expand Up @@ -2250,7 +2250,9 @@ class ProgressBar {
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 ...
Expand Down Expand Up @@ -2437,15 +2439,15 @@ class BlockProgressBar {
}
}

std::pair<std::string, size_t> get_prefix_text() {
std::pair<std::string, int> get_prefix_text() {
std::stringstream os;
os << get_value<details::ProgressBarOption::prefix_text>();
const auto result = os.str();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}

std::pair<std::string, size_t> get_postfix_text() {
std::pair<std::string, int> get_postfix_text() {
std::stringstream os;
const auto max_progress = get_value<details::ProgressBarOption::max_progress>();
auto now = std::chrono::high_resolution_clock::now();
Expand Down Expand Up @@ -2542,7 +2544,9 @@ class BlockProgressBar {
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 ...
Expand Down Expand Up @@ -2724,15 +2728,15 @@ class IndeterminateProgressBar {
template <typename Indicator> friend class DynamicProgress;
std::atomic<bool> multi_progress_mode_{false};

std::pair<std::string, size_t> get_prefix_text() {
std::pair<std::string, int> get_prefix_text() {
std::stringstream os;
os << get_value<details::ProgressBarOption::prefix_text>();
const auto result = os.str();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}

std::pair<std::string, size_t> get_postfix_text() {
std::pair<std::string, int> get_postfix_text() {
std::stringstream os;
os << " " << get_value<details::ProgressBarOption::postfix_text>();

Expand Down Expand Up @@ -2783,7 +2787,9 @@ class IndeterminateProgressBar {
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 ...
Expand Down

0 comments on commit 4ea7161

Please sign in to comment.