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

@@ -6,12 +6,22 @@
#include <thread>
class ProgressBar {
std::string _name{"Running"};
size_t _bar_width{80};
std::string _start{"|"};
std::string _end{"|"};
std::mutex _mutex;
std::string _name{""};
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:
@@ -20,14 +30,17 @@ public:
void increment(float value) {
std::unique_lock<std::mutex> lock{_mutex};
_progress = value / 100.0;
std::cout << _name << " [";
hide_cursor();
std::cout << _start;
float pos = _progress * static_cast<float>(_bar_width);
for (size_t i = 0; i < _bar_width; ++i) {
if (i < pos) std::cout << '#';
else if (i == pos) std::cout << ">";
else std::cout << " ";
if (i < pos) std::cout << _step;
else if (i == pos) std::cout << _head;
else std::cout << "-";
}
std::cout << "] " << static_cast<int>(value) << "%\r";
std::cout << _end << " " << static_cast<int>(value) << "%";
std::cout << " " << _name << "\r";
std::cout.flush();
show_cursor();
}
};