mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-13 10:38:51 +08:00
Hiding and showing cursor with CSI sequences. Updated progress spinner states
This commit is contained in:
@@ -6,12 +6,22 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
class ProgressBar {
|
class ProgressBar {
|
||||||
std::string _name{"Running"};
|
std::string _name{""};
|
||||||
size_t _bar_width{80};
|
|
||||||
std::string _start{"|"};
|
|
||||||
std::string _end{"|"};
|
|
||||||
std::mutex _mutex;
|
|
||||||
float _progress{0.0};
|
float _progress{0.0};
|
||||||
|
size_t _bar_width{80};
|
||||||
|
std::string _start{"["};
|
||||||
|
std::string _step{"■"};
|
||||||
|
std::string _head{"■"};
|
||||||
|
std::string _end{"]"};
|
||||||
|
std::mutex _mutex;
|
||||||
|
|
||||||
|
void hide_cursor() {
|
||||||
|
std::cout << "\e[?25l";
|
||||||
|
}
|
||||||
|
|
||||||
|
void show_cursor() {
|
||||||
|
std::cout << "\e[?25h";
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@@ -20,14 +30,17 @@ public:
|
|||||||
void increment(float value) {
|
void increment(float value) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::unique_lock<std::mutex> lock{_mutex};
|
||||||
_progress = value / 100.0;
|
_progress = value / 100.0;
|
||||||
std::cout << _name << " [";
|
hide_cursor();
|
||||||
|
std::cout << _start;
|
||||||
float pos = _progress * static_cast<float>(_bar_width);
|
float pos = _progress * static_cast<float>(_bar_width);
|
||||||
for (size_t i = 0; i < _bar_width; ++i) {
|
for (size_t i = 0; i < _bar_width; ++i) {
|
||||||
if (i < pos) std::cout << '#';
|
if (i < pos) std::cout << _step;
|
||||||
else if (i == pos) std::cout << ">";
|
else if (i == pos) std::cout << _head;
|
||||||
else std::cout << " ";
|
else std::cout << "-";
|
||||||
}
|
}
|
||||||
std::cout << "] " << static_cast<int>(value) << "%\r";
|
std::cout << _end << " " << static_cast<int>(value) << "%";
|
||||||
|
std::cout << " " << _name << "\r";
|
||||||
std::cout.flush();
|
std::cout.flush();
|
||||||
|
show_cursor();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,12 +9,18 @@
|
|||||||
class ProgressSpinner {
|
class ProgressSpinner {
|
||||||
std::string _name{"Running"};
|
std::string _name{"Running"};
|
||||||
size_t _bar_width{80};
|
size_t _bar_width{80};
|
||||||
std::string _start{"|"};
|
|
||||||
std::string _end{"|"};
|
|
||||||
std::mutex _mutex;
|
std::mutex _mutex;
|
||||||
float _progress{0.0};
|
float _progress{0.0};
|
||||||
size_t _index{0};
|
size_t _index{0};
|
||||||
std::vector<std::string> _states{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}; //{"-", "\\", "|", "/"};
|
std::vector<std::string> _states{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"};
|
||||||
|
|
||||||
|
void hide_cursor() {
|
||||||
|
std::cout << "\e[?25l";
|
||||||
|
}
|
||||||
|
|
||||||
|
void show_cursor() {
|
||||||
|
std::cout << "\e[?25h";
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@@ -22,11 +28,14 @@ public:
|
|||||||
|
|
||||||
void increment(float value) {
|
void increment(float value) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::unique_lock<std::mutex> lock{_mutex};
|
||||||
std::cout << _name << " [";
|
|
||||||
_progress = value / 100.0;
|
_progress = value / 100.0;
|
||||||
float pos = _progress * static_cast<float>(_bar_width);
|
float pos = _progress * static_cast<float>(_bar_width);
|
||||||
std::cout << _states[_index % _states.size()] << "] " << static_cast<int>(value) << "%\r";
|
hide_cursor();
|
||||||
|
std::cout << _states[_index % _states.size()];
|
||||||
|
std::cout << " " << static_cast<int>(value) << "%";
|
||||||
|
std::cout << " " << _name << "\r";
|
||||||
std::cout.flush();
|
std::cout.flush();
|
||||||
|
show_cursor();
|
||||||
_index += 1;
|
_index += 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user