Skip to content

Commit 2bf81bd

Browse files
committed
add: more try catch to debug the error
1 parent 4cfcb92 commit 2bf81bd

File tree

1 file changed

+77
-54
lines changed

1 file changed

+77
-54
lines changed

src/ProgressBar.cpp

+77-54
Original file line numberDiff line numberDiff line change
@@ -319,79 +319,102 @@ inline std::chrono::milliseconds::rep ProgressBar::ProgressBar::calculate_elapse
319319
//--------------------------
320320
void ProgressBar::ProgressBar::display(void) {
321321
try {
322+
static std::string completed_bar,
323+
incomplete_bar,
324+
green_part,
325+
red_part,
326+
colored_bar,
327+
formatted_bar,
328+
formatted_time;
322329
//--------------------------
323-
std::string completed_bar,
324-
incomplete_bar,
325-
green_part,
326-
red_part,
327-
colored_bar,
328-
formatted_bar,
329-
formatted_time;
330+
// Reserve memory for static strings with validation
330331
//--------------------------
331-
// Reserve memory for static strings
332-
//--------------------------
333-
completed_bar.reserve(m_bar_length);
334-
incomplete_bar.reserve(m_bar_length);
335-
green_part.reserve(m_bar_length);
336-
red_part.reserve(m_bar_length);
337-
colored_bar.reserve(m_bar_length * 2UL + 1UL); // To account for the combined bar and the "]"
338-
formatted_bar.reserve(m_bar_length * 2UL + 50UL); // Adjust size based on expected length
339-
formatted_time.reserve(100UL); // Adjust size based on expected length
332+
try {
333+
completed_bar.reserve(m_bar_length);
334+
incomplete_bar.reserve(m_bar_length);
335+
green_part.reserve(m_bar_length);
336+
red_part.reserve(m_bar_length);
337+
colored_bar.reserve(m_bar_length * 2UL + 1UL); // To account for the combined bar and the "]"
338+
formatted_bar.reserve(m_bar_length * 2UL + 50UL); // Adjust size based on expected length
339+
formatted_time.reserve(100UL); // Adjust size based on expected length
340+
} catch (const std::length_error& e) {
341+
std::cerr << "Memory reservation error: " << e.what() << std::endl;
342+
throw;
343+
}
340344
//--------------------------
341345
size_t position = 0UL, percent = 0UL;
342346
//--------------------------
343-
if (m_total != 0UL && m_total != std::numeric_limits<size_t>::max()) {
344-
m_progress = std::clamp(m_progress, static_cast<size_t>(0UL), m_total);
345-
const double ratio = m_total > 0UL ? static_cast<double>(m_progress) / static_cast<double>(m_total) : 0.;
346-
percent = static_cast<size_t>(ratio * 100UL);
347-
position = static_cast<size_t>(m_bar_length * ratio);
348-
} else {
349-
position = m_progress % m_bar_length;
350-
}// end if (m_total != 0UL && m_total != std::numeric_limits<size_t>::max())
347+
try {
348+
if (m_total != 0UL && m_total != std::numeric_limits<size_t>::max()) {
349+
m_progress = std::clamp(m_progress, static_cast<size_t>(0UL), m_total);
350+
const double ratio = m_total > 0UL ? static_cast<double>(m_progress) / static_cast<double>(m_total) : 0.;
351+
percent = static_cast<size_t>(ratio * 100UL);
352+
position = static_cast<size_t>(m_bar_length * ratio);
353+
} else {
354+
position = m_progress % m_bar_length;
355+
}
356+
} catch (const std::exception& e) {
357+
std::cerr << "Calculation error: " << e.what() << std::endl;
358+
throw;
359+
}
351360
//--------------------------
352-
// Reuse static variables for string construction
361+
// Reuse static variables for string construction with validation
353362
//--------------------------
354363
try {
355364
if (position <= m_bar_length) {
356365
completed_bar.assign(position, m_progress_char[0]);
357366
incomplete_bar.assign(m_bar_length - position, m_empty_space_char[0]);
358-
} // end if (position <= m_bar_length)
367+
} else {
368+
throw std::length_error("Calculated position exceeds bar length");
369+
}
359370
} catch (const std::length_error& e) {
360371
std::cerr << "String assignment error: " << e.what() << std::endl;
361-
completed_bar.assign(m_bar_length, m_progress_char[0]);
362-
incomplete_bar.assign(0UL, m_empty_space_char[0]);
372+
throw;
363373
}
364374
//--------------------------
365-
const std::string elapsed_time = append_time(calculate_elapsed(), "Elapsed:");
366-
const std::string etc_time = (m_total != 0UL && m_total != std::numeric_limits<size_t>::max()) ? append_time(calculate_etc(), "ETC:") : "ETC: N/A ";
367-
//--------------------------
368-
// Use fmt library to format with colors
369-
green_part = fmt::format(fmt::emphasis::bold | fmt::fg(fmt::color::green), "{}", completed_bar);
370-
red_part = fmt::format(fmt::emphasis::bold | fmt::fg(fmt::color::red), "{}", incomplete_bar);
371-
//--------------------------
372-
colored_bar = green_part + red_part + "]";
373-
//--------------------------
374-
formatted_bar = fmt::format("\r{}: {:3d}% [{}{} ", m_name, percent, colored_bar, std::string(m_spaces_after_bar, ' '));
375-
//--------------------------
376-
if (m_available_width < MIN_WIDTH) {
377-
formatted_bar = fmt::format("\r{:3d}% [{}]", percent, completed_bar + incomplete_bar);
378-
}// end if (m_available_width < MIN_WIDTH)
375+
std::string elapsed_time;
376+
std::string etc_time;
377+
try {
378+
elapsed_time = append_time(calculate_elapsed(), "Elapsed:");
379+
if (m_total != 0UL && m_total != std::numeric_limits<size_t>::max()) {
380+
etc_time = append_time(calculate_etc(), "ETC:");
381+
} else {
382+
etc_time = "ETC: N/A ";
383+
}
384+
} catch (const std::exception& e) {
385+
std::cerr << "Time calculation error: " << e.what() << std::endl;
386+
throw;
387+
}
379388
//--------------------------
380-
formatted_time = fmt::format("{} {}", elapsed_time, etc_time);
389+
try {
390+
// Use fmt library to format with colors
391+
green_part = fmt::format(fmt::emphasis::bold | fmt::fg(fmt::color::green), "{}", completed_bar);
392+
red_part = fmt::format(fmt::emphasis::bold | fmt::fg(fmt::color::red), "{}", incomplete_bar);
393+
colored_bar = green_part + red_part + "]";
394+
formatted_bar = fmt::format("\r{}: {:3d}% [{}{} ", m_name, percent, colored_bar, std::string(m_spaces_after_bar, ' '));
395+
if (m_available_width < MIN_WIDTH) {
396+
formatted_bar = fmt::format("\r{:3d}% [{}]", percent, completed_bar + incomplete_bar);
397+
}
398+
formatted_time = fmt::format("{} {}", elapsed_time, etc_time);
399+
} catch (const std::exception& e) {
400+
std::cerr << "String formatting error: " << e.what() << std::endl;
401+
throw;
402+
}
381403
//--------------------------
382-
std::cout << fmt::format(fmt::emphasis::bold, "\x1b[A{} \n{}", formatted_bar, formatted_time);
383-
std::cout.flush();
404+
try {
405+
std::cout << fmt::format(fmt::emphasis::bold, "\x1b[A{} \n{}", formatted_bar, formatted_time);
406+
std::cout.flush();
407+
} catch (const std::exception& e) {
408+
std::cerr << "Output error: " << e.what() << std::endl;
409+
throw;
410+
}
384411
//--------------------------
385-
}//end try
386-
catch (const std::length_error& e) {
387-
std::cerr << "Error: " << e.what() << std::endl;
412+
} catch (const std::exception& e) {
413+
std::cerr << "Unexpected error in display: " << e.what() << std::endl;
388414
// Handle the error, possibly reset the state or provide a fallback
389-
} // end catch (const std::length_error& e)
390-
catch (const std::exception& e) {
391-
std::cerr << "Unexpected error: " << e.what() << std::endl;
392-
// Handle other exceptions
393-
} // end catch (const std::exception& e)
394-
}// end void ProgressBar::ProgressBar::display(void)
415+
}
416+
}
417+
// end void ProgressBar::ProgressBar::display(void)
395418
//--------------------------------------------------------------
396419
#else
397420
//--------------------------

0 commit comments

Comments
 (0)