File indexing completed on 2023-03-17 11:12:46
0001 #ifndef PAPERINO_PROGRESS_BAR_HH
0002 #define PAPERINO_PROGRESS_BAR_HH
0003
0004 #include <cstdio>
0005 #include <ctime>
0006 #include <iostream>
0007 #include <iomanip>
0008
0009 namespace {
0010
0011
0012 double elapsed_time() {
0013 static std::clock_t start_time = std::clock();
0014 return (static_cast<double>(std::clock() - start_time) / CLOCKS_PER_SEC);
0015 }
0016
0017 void show_progress_bar(unsigned long entry, unsigned long num_entries) {
0018 static double progress;
0019 static unsigned int num_steps = 100, progress_step = 0, next_progress_step = 0;
0020 static unsigned int time_left = 0, hr_left = 0, min_left = 0;
0021
0022 num_entries = (num_entries == 0) ? 1 : num_entries;
0023
0024 if (entry == 0) {
0025
0026
0027 elapsed_time();
0028 }
0029
0030 progress = static_cast<double>(num_steps) * (entry + 1) / num_entries;
0031 progress_step = progress;
0032
0033 if (progress_step >= next_progress_step) {
0034 unsigned int i = 0;
0035 std::cout << "\r"
0036 << "[";
0037 for (; i < next_progress_step; i += 5) {
0038 std::cout << "#";
0039 }
0040 for (; i < num_steps; i += 5) {
0041 std::cout << "-";
0042 }
0043 std::cout << "] " << progress_step * 100 / num_steps << "%";
0044
0045 if (progress_step < num_steps) {
0046 time_left = elapsed_time() / (entry + 1) * (num_entries - entry);
0047 hr_left = time_left / 3600;
0048 min_left = (time_left / 60) - (hr_left * 60);
0049 time_left = time_left - (min_left * 60) - (hr_left * 3600);
0050
0051 std::cout << " approx " << std::setw(2) << hr_left << "h " << std::setw(2) << min_left << "m " << std::setw(2)
0052 << time_left << "s remaining";
0053 } else {
0054 std::cout << " ";
0055 }
0056 std::cout.flush();
0057
0058 next_progress_step += 1;
0059 }
0060
0061 if (entry == num_entries - 1) {
0062 std::cout << std::endl;
0063
0064 std::cout << "Elapsed time: " << elapsed_time() << " sec" << std::endl;
0065 }
0066 }
0067
0068 }
0069
0070 #endif