Merge branch 'p-ranav:master' into master

This commit is contained in:
赵诗涵
2025-05-08 03:34:27 +08:00
committed by GitHub
4 changed files with 26 additions and 19 deletions

View File

@@ -80,6 +80,7 @@ int main() {
option::End{"]"},
option::PostfixText{"Extracting Archive"},
option::ForegroundColor{Color::green},
option::ShowPercentage{true},
option::FontStyles{std::vector<FontStyle>{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>{FontStyle::bold}}
};
option::ShowPercentage{true},
option::FontStyles{std::vector<FontStyle>{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>{FontStyle::bold}}
@@ -291,6 +293,7 @@ int main() {
option::Start{"["},
option::End{"]"},
option::ForegroundColor{Color::white} ,
option::ShowPercentage{true},
option::FontStyles{std::vector<FontStyle>{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>{FontStyle::bold}},
option::MaxProgress{numbers.size()}

View File

@@ -102,10 +102,10 @@ public:
}
}
void set_progress(float value) {
void set_progress(size_t value) {
{
std::lock_guard<std::mutex> lock{mutex_};
progress_ = value;
tick_ = value;
}
save_start_time();
print_progress();
@@ -114,7 +114,7 @@ public:
void tick() {
{
std::lock_guard<std::mutex> lock{mutex_};
progress_ += 1;
tick_++;
}
save_start_time();
print_progress();
@@ -122,8 +122,7 @@ public:
size_t current() {
std::lock_guard<std::mutex> lock{mutex_};
return (std::min)(static_cast<size_t>(progress_),
size_t(get_value<details::ProgressBarOption::max_progress>()));
return (std::min)(tick_, size_t(get_value<details::ProgressBarOption::max_progress>()));
}
bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
@@ -147,6 +146,7 @@ private:
Settings settings_;
float progress_{0.0};
size_t tick_{0};
std::chrono::time_point<std::chrono::high_resolution_clock> start_time_point_;
std::mutex mutex_;
@@ -175,11 +175,12 @@ private:
std::pair<std::string, int> get_postfix_text() {
std::stringstream os;
const auto max_progress = get_value<details::ProgressBarOption::max_progress>();
progress_ = static_cast<float>(tick_)/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))
os << " " << (std::min)(static_cast<size_t>(progress_ * 100.0), size_t(100))
<< "%";
}
@@ -201,9 +202,8 @@ private:
if (saved_start_time) {
auto eta = std::chrono::nanoseconds(
progress_ > 0
? static_cast<long long>(std::ceil(float(elapsed.count()) *
max_progress / progress_))
tick_ > 0
? static_cast<long long>(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<details::ProgressBarOption::max_progress>();
if (multi_progress_mode_ && !from_multi_progress) {
if (progress_ > max_progress) {
if (tick_ > max_progress) {
get_value<details::ProgressBarOption::completed>() = true;
}
return;
@@ -253,7 +253,7 @@ public:
details::BlockProgressScaleWriter writer{os,
get_value<details::ProgressBarOption::bar_width>()};
writer.write(progress_ / max_progress * 100);
writer.write(progress_ * 100);
os << get_value<details::ProgressBarOption::end>();
@@ -278,7 +278,7 @@ public:
}
os.flush();
if (progress_ > max_progress) {
if (tick_ > max_progress) {
get_value<details::ProgressBarOption::completed>() = true;
}
if (get_value<details::ProgressBarOption::completed>() &&
@@ -289,4 +289,4 @@ public:
} // namespace indicators
#endif
#endif

View File

@@ -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;

View File

@@ -14,10 +14,10 @@ int main() {
std::vector<indicators::FontStyle>{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));