Updated bar example

This commit is contained in:
Pranav Srinivas KumaR
2019-12-03 20:40:34 -06:00
parent 4ee9e723b6
commit 2684cacd95
2 changed files with 22 additions and 15 deletions

View File

@@ -6,16 +6,16 @@
#include <thread>
class ProgressBar {
std::string _name{""};
float _progress{0.0};
size_t _bar_width{100};
std::string _start{"[ "};
std::string _fill{""};
std::string _lead{""};
std::string _remainder{"-"};
std::string _end{" ]"};
std::string _start{"["};
std::string _fill{"="};
std::string _lead{">"};
std::string _remainder{" "};
std::string _end{"]"};
std::atomic<bool> _completed{false};
std::atomic<bool> _show_percentage{true};
std::mutex _mutex;
std::atomic<bool> _completed;
void _print_progress() {
std::unique_lock<std::mutex> lock{_mutex};
@@ -26,8 +26,11 @@ class ProgressBar {
else if (i == pos) std::cout << _lead;
else std::cout << _remainder;
}
std::cout << _end << " " << static_cast<int>(_progress) << "%";
std::cout << " " << _name << "\r";
std::cout << _end;
if (_show_percentage)
std::cout << " " << static_cast<int>(_progress) << "%\r";
else
std::cout << "\r";
std::cout.flush();
if (_completed)
std::cout << std::endl;
@@ -63,7 +66,11 @@ public:
void end_with(const std::string& end) {
std::unique_lock<std::mutex> lock{_mutex};
_end = end;
}
}
void show_percentage(bool flag) {
_show_percentage = flag;
}
void set_progress(float value) {
{