mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-16 04:18:51 +08:00
Removed code duplication in prefix/postfix length calculation
This commit is contained in:
@@ -168,6 +168,53 @@ private:
|
|||||||
return os.str().size();
|
return os.str().size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t get_postfix_length() {
|
||||||
|
std::stringstream os;
|
||||||
|
const auto max_progress = get_value<details::ProgressBarOption::max_progress>();
|
||||||
|
auto now = std::chrono::high_resolution_clock::now();
|
||||||
|
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - start_time_point_);
|
||||||
|
|
||||||
|
if (get_value<details::ProgressBarOption::show_percentage>()) {
|
||||||
|
os << " " << std::min(static_cast<size_t>(progress_ / max_progress * 100.0), size_t(100))
|
||||||
|
<< "%";
|
||||||
|
}
|
||||||
|
|
||||||
|
auto &saved_start_time = get_value<details::ProgressBarOption::saved_start_time>();
|
||||||
|
|
||||||
|
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
|
||||||
|
os << " [";
|
||||||
|
if (saved_start_time)
|
||||||
|
details::write_duration(os, elapsed);
|
||||||
|
else
|
||||||
|
os << "00:00s";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (get_value<details::ProgressBarOption::show_remaining_time>()) {
|
||||||
|
if (get_value<details::ProgressBarOption::show_elapsed_time>())
|
||||||
|
os << "<";
|
||||||
|
else
|
||||||
|
os << " [";
|
||||||
|
|
||||||
|
if (saved_start_time) {
|
||||||
|
auto eta = std::chrono::nanoseconds(
|
||||||
|
progress_ > 0 ? static_cast<long long>(elapsed.count() * max_progress / progress_) : 0);
|
||||||
|
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
|
||||||
|
details::write_duration(os, remaining);
|
||||||
|
} else {
|
||||||
|
os << "00:00s";
|
||||||
|
}
|
||||||
|
|
||||||
|
os << "]";
|
||||||
|
} else {
|
||||||
|
if (get_value<details::ProgressBarOption::show_elapsed_time>())
|
||||||
|
os << "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
os << " " << get_value<details::ProgressBarOption::postfix_text>();
|
||||||
|
|
||||||
|
return os.str().size();
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void print_progress(bool from_multi_progress = false) {
|
void print_progress(bool from_multi_progress = false) {
|
||||||
std::lock_guard<std::mutex> lock{mutex_};
|
std::lock_guard<std::mutex> lock{mutex_};
|
||||||
@@ -240,6 +287,7 @@ public:
|
|||||||
os << " " << get_value<details::ProgressBarOption::postfix_text>()
|
os << " " << get_value<details::ProgressBarOption::postfix_text>()
|
||||||
<< std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ') << "\r";
|
<< std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ') << "\r";
|
||||||
os.flush();
|
os.flush();
|
||||||
|
|
||||||
if (progress_ > max_progress) {
|
if (progress_ > max_progress) {
|
||||||
get_value<details::ProgressBarOption::completed>() = true;
|
get_value<details::ProgressBarOption::completed>() = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -214,16 +214,14 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t get_prefix_length() {
|
std::pair<std::string, size_t> get_prefix_text() {
|
||||||
std::stringstream os;
|
std::stringstream os;
|
||||||
os << get_value<details::ProgressBarOption::prefix_text>();
|
os << get_value<details::ProgressBarOption::prefix_text>();
|
||||||
return os.str().size();
|
return {os.str(), os.str().size()};
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t get_postfix_length() {
|
std::pair<std::string, size_t> get_postfix_text() {
|
||||||
std::stringstream os;
|
std::stringstream os;
|
||||||
const auto min_progress =
|
|
||||||
get_value<details::ProgressBarOption::min_progress>();
|
|
||||||
const auto max_progress =
|
const auto max_progress =
|
||||||
get_value<details::ProgressBarOption::max_progress>();
|
get_value<details::ProgressBarOption::max_progress>();
|
||||||
|
|
||||||
@@ -271,7 +269,7 @@ private:
|
|||||||
|
|
||||||
os << " " << get_value<details::ProgressBarOption::postfix_text>();
|
os << " " << get_value<details::ProgressBarOption::postfix_text>();
|
||||||
|
|
||||||
return os.str().size();
|
return {os.str(), os.str().size()};
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -305,7 +303,10 @@ public:
|
|||||||
for (auto &style : get_value<details::ProgressBarOption::font_styles>())
|
for (auto &style : get_value<details::ProgressBarOption::font_styles>())
|
||||||
details::set_font_style(os, style);
|
details::set_font_style(os, style);
|
||||||
|
|
||||||
os << get_value<details::ProgressBarOption::prefix_text>();
|
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<details::ProgressBarOption::start>();
|
os << get_value<details::ProgressBarOption::start>();
|
||||||
|
|
||||||
@@ -318,56 +319,15 @@ public:
|
|||||||
|
|
||||||
os << get_value<details::ProgressBarOption::end>();
|
os << get_value<details::ProgressBarOption::end>();
|
||||||
|
|
||||||
if (get_value<details::ProgressBarOption::show_percentage>()) {
|
const auto postfix_pair = get_postfix_text();
|
||||||
os << " "
|
const auto postfix_text = postfix_pair.first;
|
||||||
<< std::min(static_cast<size_t>(static_cast<float>(progress_) /
|
const auto postfix_length = postfix_pair.second;
|
||||||
max_progress * 100),
|
os << postfix_text;
|
||||||
size_t(100))
|
|
||||||
<< "%";
|
|
||||||
}
|
|
||||||
|
|
||||||
auto &saved_start_time =
|
|
||||||
get_value<details::ProgressBarOption::saved_start_time>();
|
|
||||||
|
|
||||||
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
|
|
||||||
os << " [";
|
|
||||||
if (saved_start_time)
|
|
||||||
details::write_duration(os, elapsed_);
|
|
||||||
else
|
|
||||||
os << "00:00s";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (get_value<details::ProgressBarOption::show_remaining_time>()) {
|
|
||||||
if (get_value<details::ProgressBarOption::show_elapsed_time>())
|
|
||||||
os << "<";
|
|
||||||
else
|
|
||||||
os << " [";
|
|
||||||
|
|
||||||
if (saved_start_time) {
|
|
||||||
auto eta = std::chrono::nanoseconds(
|
|
||||||
progress_ > 0 ? static_cast<long long>(elapsed_.count() *
|
|
||||||
max_progress / progress_)
|
|
||||||
: 0);
|
|
||||||
auto remaining = eta > elapsed_ ? (eta - elapsed_) : (elapsed_ - eta);
|
|
||||||
details::write_duration(os, remaining);
|
|
||||||
} else {
|
|
||||||
os << "00:00s";
|
|
||||||
}
|
|
||||||
|
|
||||||
os << "]";
|
|
||||||
} else {
|
|
||||||
if (get_value<details::ProgressBarOption::show_elapsed_time>())
|
|
||||||
os << "]";
|
|
||||||
}
|
|
||||||
|
|
||||||
os << " " << get_value<details::ProgressBarOption::postfix_text>();
|
|
||||||
|
|
||||||
// Get length of prefix text and postfix text
|
// Get length of prefix text and postfix text
|
||||||
const auto prefix_length = get_prefix_length();
|
|
||||||
const auto start_length = get_value<details::ProgressBarOption::start>().size();
|
const auto start_length = get_value<details::ProgressBarOption::start>().size();
|
||||||
const auto bar_width = get_value<details::ProgressBarOption::bar_width>();
|
const auto bar_width = get_value<details::ProgressBarOption::bar_width>();
|
||||||
const auto end_length = get_value<details::ProgressBarOption::end>().size();
|
const auto end_length = get_value<details::ProgressBarOption::end>().size();
|
||||||
const auto postfix_length = get_postfix_length();
|
|
||||||
const auto terminal_width = terminal_size().second;
|
const auto terminal_width = terminal_size().second;
|
||||||
// prefix + bar_width + postfix should be <= terminal_width
|
// prefix + bar_width + postfix should be <= terminal_width
|
||||||
const int remaining = terminal_width - (prefix_length + start_length + bar_width + end_length + postfix_length);
|
const int remaining = terminal_width - (prefix_length + start_length + bar_width + end_length + postfix_length);
|
||||||
|
|||||||
Reference in New Issue
Block a user