Skip to content

Commit

Permalink
Fix: the issue that causes basic_string::_M_create error
Browse files Browse the repository at this point in the history
  • Loading branch information
mjshakir committed Jun 7, 2024
1 parent 2bf81bd commit dbad942
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/ProgressBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,18 +387,27 @@ inline std::chrono::milliseconds::rep ProgressBar::ProgressBar::calculate_elapse
}
//--------------------------
try {
// Use fmt library to format with colors
green_part = fmt::format(fmt::emphasis::bold | fmt::fg(fmt::color::green), "{}", completed_bar);
red_part = fmt::format(fmt::emphasis::bold | fmt::fg(fmt::color::red), "{}", incomplete_bar);
colored_bar = green_part + red_part + "]";
formatted_bar = fmt::format("\r{}: {:3d}% [{}{} ", m_name, percent, colored_bar, std::string(m_spaces_after_bar, ' '));
if (m_available_width < MIN_WIDTH) {
formatted_bar = fmt::format("\r{:3d}% [{}]", percent, completed_bar + incomplete_bar);
// Ensure lengths are within reasonable bounds before formatting
if (completed_bar.length() <= m_bar_length && incomplete_bar.length() <= m_bar_length) {
green_part = fmt::format(fmt::emphasis::bold | fmt::fg(fmt::color::green), "{}", completed_bar);
red_part = fmt::format(fmt::emphasis::bold | fmt::fg(fmt::color::red), "{}", incomplete_bar);
colored_bar = green_part + red_part + "]";
formatted_bar = fmt::format("\r{}: {:3d}% [{}{} ", m_name, percent, colored_bar, std::string(m_spaces_after_bar, ' '));

if (m_available_width < MIN_WIDTH) {
formatted_bar = fmt::format("\r{:3d}% [{}]", percent, completed_bar + incomplete_bar);
}

formatted_time = fmt::format("{} {}", elapsed_time, etc_time);
} else {
throw std::length_error("Bar components exceed safe lengths");
}
formatted_time = fmt::format("{} {}", elapsed_time, etc_time);
} catch (const std::exception& e) {
} catch (const std::length_error& e) {
std::cerr << "String formatting error: " << e.what() << std::endl;
throw;
} catch (const std::exception& e) {
std::cerr << "Unexpected string formatting error: " << e.what() << std::endl;
throw;
}
//--------------------------
try {
Expand All @@ -413,8 +422,7 @@ inline std::chrono::milliseconds::rep ProgressBar::ProgressBar::calculate_elapse
std::cerr << "Unexpected error in display: " << e.what() << std::endl;
// Handle the error, possibly reset the state or provide a fallback
}
}
// end void ProgressBar::ProgressBar::display(void)
}// end void ProgressBar::ProgressBar::display(void)
//--------------------------------------------------------------
#else
//--------------------------
Expand Down

0 comments on commit dbad942

Please sign in to comment.