FTXUI/ftxui/src/ftxui/dom/gauge.cpp

40 lines
920 B
C++
Raw Normal View History

#include "ftxui/dom/node.hpp"
#include "ftxui/dom/elements.hpp"
2018-09-20 03:52:25 +08:00
namespace ftxui {
namespace dom {
2018-09-22 15:49:43 +08:00
static wchar_t charset[] = L" ▏▎▍▌▋▊▉█";
2018-09-20 03:52:25 +08:00
class Gauge : public Node {
public:
Gauge(float progress) : progress_(progress) {}
2018-09-22 15:49:43 +08:00
~Gauge() override {}
2018-09-20 03:52:25 +08:00
void ComputeRequirement() override {
requirement_.flex.x = 1;
requirement_.min.y = 1;
}
void Render(Screen& screen) override {
float y = box_.top;
2018-09-22 15:49:43 +08:00
float limit = box_.left + progress_ * (box_.right - box_.left + 1);
int limit_int = limit;
int x = box_.left;
while (x < limit_int)
screen.at(x++, y) = charset[9];
screen.at(x++, y) = charset[int(9*(limit-limit_int))];
while (x <= box_.right)
screen.at(x++, y) = charset[0];
2018-09-20 03:52:25 +08:00
}
private:
float progress_;
};
std::unique_ptr<Node> gauge(float progress) {
return std::make_unique<Gauge>(progress);
}
}; // namespace dom
}; // namespace ftxui