Skip to content

Commit

Permalink
Change: and try catch to ensure the error is catched.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjshakir committed Jun 7, 2024
1 parent 52c6625 commit 394b35f
Showing 1 changed file with 71 additions and 57 deletions.
128 changes: 71 additions & 57 deletions src/ProgressBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,65 +318,79 @@ inline std::chrono::milliseconds::rep ProgressBar::ProgressBar::calculate_elapse
#ifdef HAVE_FMT
//--------------------------
void ProgressBar::ProgressBar::display(void) {
//--------------------------
static std::string completed_bar,
incomplete_bar,
green_part,
red_part,
colored_bar,
formatted_bar,
formatted_time;
//--------------------------
// Reserve memory for static strings
//--------------------------
completed_bar.reserve(m_bar_length);
incomplete_bar.reserve(m_bar_length);
green_part.reserve(m_bar_length);
red_part.reserve(m_bar_length);
colored_bar.reserve(m_bar_length * 2UL + 1UL); // To account for the combined bar and the "]"
formatted_bar.reserve(m_bar_length * 2UL + 50UL); // Adjust size based on expected length
formatted_time.reserve(100UL); // Adjust size based on expected length
//--------------------------
size_t position = 0UL, percent = 0UL;
//--------------------------
if (m_total != 0UL && m_total != std::numeric_limits<size_t>::max()) {
m_progress = std::clamp(m_progress, static_cast<size_t>(0UL), m_total);
const double ratio = m_total > 0UL ? static_cast<double>(m_progress) / static_cast<double>(m_total) : 0.;
percent = static_cast<size_t>(ratio * 100UL);
position = static_cast<size_t>(m_bar_length * ratio);
} else {
position = m_progress % m_bar_length;
}// end if (m_total != 0UL && m_total != std::numeric_limits<size_t>::max())
//--------------------------
// Reuse static variables for string construction
//--------------------------
if(position <= m_bar_length) {
try {
//--------------------------
completed_bar.assign(position, m_progress_char[0]);
incomplete_bar.assign(m_bar_length - position, m_empty_space_char[0]);
static std::string completed_bar,
incomplete_bar,
green_part,
red_part,
colored_bar,
formatted_bar,
formatted_time;
//--------------------------
}// end if(position <= m_bar_length
//--------------------------
const std::string elapsed_time = append_time(calculate_elapsed(), "Elapsed:");
const std::string etc_time = (m_total != 0UL && m_total != std::numeric_limits<size_t>::max()) ? append_time(calculate_etc(), "ETC:") : "ETC: N/A ";
//--------------------------
// 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);
}// end if (m_available_width < MIN_WIDTH)
//--------------------------
formatted_time = fmt::format("{} {}", elapsed_time, etc_time);
//--------------------------
std::cout << fmt::format(fmt::emphasis::bold, "\x1b[A{} \n{}", formatted_bar, formatted_time);
std::cout.flush();
//--------------------------
// Reserve memory for static strings
//--------------------------
completed_bar.reserve(m_bar_length);
incomplete_bar.reserve(m_bar_length);
green_part.reserve(m_bar_length);
red_part.reserve(m_bar_length);
colored_bar.reserve(m_bar_length * 2UL + 1UL); // To account for the combined bar and the "]"
formatted_bar.reserve(m_bar_length * 2UL + 50UL); // Adjust size based on expected length
formatted_time.reserve(100UL); // Adjust size based on expected length
//--------------------------
size_t position = 0UL, percent = 0UL;
//--------------------------
if (m_total != 0UL && m_total != std::numeric_limits<size_t>::max()) {
m_progress = std::clamp(m_progress, static_cast<size_t>(0UL), m_total);
const double ratio = m_total > 0UL ? static_cast<double>(m_progress) / static_cast<double>(m_total) : 0.;
percent = static_cast<size_t>(ratio * 100UL);
position = static_cast<size_t>(m_bar_length * ratio);
} else {
position = m_progress % m_bar_length;
}// end if (m_total != 0UL && m_total != std::numeric_limits<size_t>::max())
//--------------------------
// Reuse static variables for string construction
//--------------------------
try {
if (position <= m_bar_length) {
completed_bar.assign(position, m_progress_char[0]);
incomplete_bar.assign(m_bar_length - position, m_empty_space_char[0]);
} // end if (position <= m_bar_length)
} catch (const std::length_error& e) {
std::cerr << "String assignment error: " << e.what() << std::endl;
completed_bar.assign(m_bar_length, m_progress_char[0]);
incomplete_bar.assign(0UL, m_empty_space_char[0]);
}
//--------------------------
const std::string elapsed_time = append_time(calculate_elapsed(), "Elapsed:");
const std::string etc_time = (m_total != 0UL && m_total != std::numeric_limits<size_t>::max()) ? append_time(calculate_etc(), "ETC:") : "ETC: N/A ";
//--------------------------
// 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);
}// end if (m_available_width < MIN_WIDTH)
//--------------------------
formatted_time = fmt::format("{} {}", elapsed_time, etc_time);
//--------------------------
std::cout << fmt::format(fmt::emphasis::bold, "\x1b[A{} \n{}", formatted_bar, formatted_time);
std::cout.flush();
//--------------------------
}//end try
catch (const std::length_error& e) {
std::cerr << "Error: " << e.what() << std::endl;
// Handle the error, possibly reset the state or provide a fallback
} // end catch (const std::length_error& e)
catch (const std::exception& e) {
std::cerr << "Unexpected error: " << e.what() << std::endl;
// Handle other exceptions
} // end catch (const std::exception& e)
}// end void ProgressBar::ProgressBar::display(void)
//--------------------------------------------------------------
#else
Expand Down

0 comments on commit 394b35f

Please sign in to comment.