mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-16 04:18:51 +08:00
Small improvement: using lock_guard instead of unique_lock
This commit is contained in:
@@ -43,32 +43,32 @@ namespace indicators {
|
|||||||
class BlockProgressBar {
|
class BlockProgressBar {
|
||||||
public:
|
public:
|
||||||
void set_foreground_color(Color color) {
|
void set_foreground_color(Color color) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_foreground_color = color;
|
_foreground_color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_bar_width(size_t bar_width) {
|
void set_bar_width(size_t bar_width) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_bar_width = bar_width;
|
_bar_width = bar_width;
|
||||||
}
|
}
|
||||||
|
|
||||||
void start_bar_with(const std::string &start) {
|
void start_bar_with(const std::string &start) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_start = start;
|
_start = start;
|
||||||
}
|
}
|
||||||
|
|
||||||
void end_bar_with(const std::string &end) {
|
void end_bar_with(const std::string &end) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_end = end;
|
_end = end;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_prefix_text(const std::string &text) {
|
void set_prefix_text(const std::string &text) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_prefix_text = text;
|
_prefix_text = text;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_postfix_text(const std::string &text) {
|
void set_postfix_text(const std::string &text) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_postfix_text = text;
|
_postfix_text = text;
|
||||||
if (_postfix_text.length() > _max_postfix_text_length)
|
if (_postfix_text.length() > _max_postfix_text_length)
|
||||||
_max_postfix_text_length = _postfix_text.length();
|
_max_postfix_text_length = _postfix_text.length();
|
||||||
@@ -88,7 +88,7 @@ public:
|
|||||||
|
|
||||||
void set_progress(float value) {
|
void set_progress(float value) {
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_progress = value;
|
_progress = value;
|
||||||
}
|
}
|
||||||
_save_start_time();
|
_save_start_time();
|
||||||
@@ -97,7 +97,7 @@ public:
|
|||||||
|
|
||||||
void tick() {
|
void tick() {
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_progress += 1;
|
_progress += 1;
|
||||||
}
|
}
|
||||||
_save_start_time();
|
_save_start_time();
|
||||||
@@ -105,7 +105,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t current() {
|
size_t current() {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
return std::min(static_cast<size_t>(_progress), size_t(100));
|
return std::min(static_cast<size_t>(_progress), size_t(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,7 +175,7 @@ private:
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
auto now = std::chrono::high_resolution_clock::now();
|
auto now = std::chrono::high_resolution_clock::now();
|
||||||
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
|
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
|
||||||
|
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _print_progress() {
|
void _print_progress() {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
if (_started)
|
if (_started)
|
||||||
for (size_t i = 0; i < count; ++i)
|
for (size_t i = 0; i < count; ++i)
|
||||||
std::cout << "\x1b[A";
|
std::cout << "\x1b[A";
|
||||||
|
|||||||
@@ -42,47 +42,47 @@ namespace indicators {
|
|||||||
class ProgressBar {
|
class ProgressBar {
|
||||||
public:
|
public:
|
||||||
void set_foreground_color(Color color) {
|
void set_foreground_color(Color color) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_foreground_color = color;
|
_foreground_color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_bar_width(size_t bar_width) {
|
void set_bar_width(size_t bar_width) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_bar_width = bar_width;
|
_bar_width = bar_width;
|
||||||
}
|
}
|
||||||
|
|
||||||
void start_bar_with(const std::string &start) {
|
void start_bar_with(const std::string &start) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_start = start;
|
_start = start;
|
||||||
}
|
}
|
||||||
|
|
||||||
void fill_bar_progress_with(const std::string &fill) {
|
void fill_bar_progress_with(const std::string &fill) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_fill = fill;
|
_fill = fill;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lead_bar_progress_with(const std::string &lead) {
|
void lead_bar_progress_with(const std::string &lead) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_lead = lead;
|
_lead = lead;
|
||||||
}
|
}
|
||||||
|
|
||||||
void fill_bar_remainder_with(const std::string &remainder) {
|
void fill_bar_remainder_with(const std::string &remainder) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_remainder = remainder;
|
_remainder = remainder;
|
||||||
}
|
}
|
||||||
|
|
||||||
void end_bar_with(const std::string &end) {
|
void end_bar_with(const std::string &end) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_end = end;
|
_end = end;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_prefix_text(const std::string &text) {
|
void set_prefix_text(const std::string &text) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_prefix_text = text;
|
_prefix_text = text;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_postfix_text(const std::string &text) {
|
void set_postfix_text(const std::string &text) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_postfix_text = text;
|
_postfix_text = text;
|
||||||
if (_postfix_text.length() > _max_postfix_text_length)
|
if (_postfix_text.length() > _max_postfix_text_length)
|
||||||
_max_postfix_text_length = _postfix_text.length();
|
_max_postfix_text_length = _postfix_text.length();
|
||||||
@@ -102,7 +102,7 @@ public:
|
|||||||
|
|
||||||
void set_progress(float value) {
|
void set_progress(float value) {
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_progress = value;
|
_progress = value;
|
||||||
}
|
}
|
||||||
_save_start_time();
|
_save_start_time();
|
||||||
@@ -111,7 +111,7 @@ public:
|
|||||||
|
|
||||||
void tick() {
|
void tick() {
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_progress += 1;
|
_progress += 1;
|
||||||
}
|
}
|
||||||
_save_start_time();
|
_save_start_time();
|
||||||
@@ -119,7 +119,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t current() {
|
size_t current() {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
return std::min(static_cast<size_t>(_progress), size_t(100));
|
return std::min(static_cast<size_t>(_progress), size_t(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,7 +189,7 @@ private:
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
auto now = std::chrono::high_resolution_clock::now();
|
auto now = std::chrono::high_resolution_clock::now();
|
||||||
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
|
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
|
||||||
|
|
||||||
|
|||||||
@@ -43,17 +43,17 @@ namespace indicators {
|
|||||||
class ProgressSpinner {
|
class ProgressSpinner {
|
||||||
public:
|
public:
|
||||||
void set_foreground_color(Color color) {
|
void set_foreground_color(Color color) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_foreground_color = color;
|
_foreground_color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_prefix_text(const std::string &text) {
|
void set_prefix_text(const std::string &text) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_prefix_text = text;
|
_prefix_text = text;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_postfix_text(const std::string &text) {
|
void set_postfix_text(const std::string &text) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_postfix_text = text;
|
_postfix_text = text;
|
||||||
if (_postfix_text.length() > _max_postfix_text_length)
|
if (_postfix_text.length() > _max_postfix_text_length)
|
||||||
_max_postfix_text_length = _postfix_text.length();
|
_max_postfix_text_length = _postfix_text.length();
|
||||||
@@ -77,7 +77,7 @@ public:
|
|||||||
|
|
||||||
void set_progress(float value) {
|
void set_progress(float value) {
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_progress = value;
|
_progress = value;
|
||||||
}
|
}
|
||||||
_save_start_time();
|
_save_start_time();
|
||||||
@@ -86,7 +86,7 @@ public:
|
|||||||
|
|
||||||
void tick() {
|
void tick() {
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_progress += 1;
|
_progress += 1;
|
||||||
}
|
}
|
||||||
_save_start_time();
|
_save_start_time();
|
||||||
@@ -94,7 +94,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t current() {
|
size_t current() {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
return std::min(static_cast<size_t>(_progress), size_t(100));
|
return std::min(static_cast<size_t>(_progress), size_t(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void set_spinner_states(const std::vector<std::string> &states) {
|
void set_spinner_states(const std::vector<std::string> &states) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_states = states;
|
_states = states;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +157,7 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _print_progress() {
|
void _print_progress() {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
auto now = std::chrono::high_resolution_clock::now();
|
auto now = std::chrono::high_resolution_clock::now();
|
||||||
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
|
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user