mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-14 03:08:52 +08:00
Updated bar example
This commit is contained in:
@@ -6,16 +6,16 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
class ProgressBar {
|
class ProgressBar {
|
||||||
std::string _name{""};
|
|
||||||
float _progress{0.0};
|
float _progress{0.0};
|
||||||
size_t _bar_width{100};
|
size_t _bar_width{100};
|
||||||
std::string _start{"["};
|
std::string _start{"["};
|
||||||
std::string _fill{"■"};
|
std::string _fill{"="};
|
||||||
std::string _lead{"■"};
|
std::string _lead{">"};
|
||||||
std::string _remainder{"-"};
|
std::string _remainder{" "};
|
||||||
std::string _end{"]"};
|
std::string _end{"]"};
|
||||||
|
std::atomic<bool> _completed{false};
|
||||||
|
std::atomic<bool> _show_percentage{true};
|
||||||
std::mutex _mutex;
|
std::mutex _mutex;
|
||||||
std::atomic<bool> _completed;
|
|
||||||
|
|
||||||
void _print_progress() {
|
void _print_progress() {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::unique_lock<std::mutex> lock{_mutex};
|
||||||
@@ -26,8 +26,11 @@ class ProgressBar {
|
|||||||
else if (i == pos) std::cout << _lead;
|
else if (i == pos) std::cout << _lead;
|
||||||
else std::cout << _remainder;
|
else std::cout << _remainder;
|
||||||
}
|
}
|
||||||
std::cout << _end << " " << static_cast<int>(_progress) << "%";
|
std::cout << _end;
|
||||||
std::cout << " " << _name << "\r";
|
if (_show_percentage)
|
||||||
|
std::cout << " " << static_cast<int>(_progress) << "%\r";
|
||||||
|
else
|
||||||
|
std::cout << "\r";
|
||||||
std::cout.flush();
|
std::cout.flush();
|
||||||
if (_completed)
|
if (_completed)
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
@@ -65,6 +68,10 @@ public:
|
|||||||
_end = end;
|
_end = end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void show_percentage(bool flag) {
|
||||||
|
_show_percentage = flag;
|
||||||
|
}
|
||||||
|
|
||||||
void set_progress(float value) {
|
void set_progress(float value) {
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::unique_lock<std::mutex> lock{_mutex};
|
||||||
|
|||||||
@@ -7,14 +7,14 @@ int main() {
|
|||||||
// Configure progress bar
|
// Configure progress bar
|
||||||
bar.bar_width(50);
|
bar.bar_width(50);
|
||||||
bar.start_with("[");
|
bar.start_with("[");
|
||||||
bar.fill_progress_with("=");
|
bar.fill_progress_with("■");
|
||||||
bar.lead_progress_with(">");
|
bar.lead_progress_with("■");
|
||||||
bar.fill_remainder_with(" ");
|
bar.fill_remainder_with("-");
|
||||||
bar.end_with(" ]");
|
bar.end_with(" ]");
|
||||||
|
|
||||||
// As configured, the bar will look like this:
|
// As configured, the bar will look like this:
|
||||||
//
|
//
|
||||||
// [=================> ] 70%
|
// [■■■■■■■■■■■■■■■■■■■■ --------] 70%
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user