Hiding and showing cursor with CSI sequences. Updated progress spinner states

This commit is contained in:
Pranav Srinivas Kumar
2019-12-03 13:50:00 -06:00
parent 9eaaa04956
commit cdb0ceec61
2 changed files with 37 additions and 15 deletions

View File

@@ -9,12 +9,18 @@
class ProgressSpinner {
std::string _name{"Running"};
size_t _bar_width{80};
std::string _start{"|"};
std::string _end{"|"};
std::mutex _mutex;
float _progress{0.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:
@@ -22,11 +28,14 @@ public:
void increment(float value) {
std::unique_lock<std::mutex> lock{_mutex};
std::cout << _name << " [";
_progress = value / 100.0;
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();
show_cursor();
_index += 1;
}
};