2021-05-10 02:32:27 +08:00
|
|
|
#include <memory> // for make_shared
|
2021-05-02 02:40:35 +08:00
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
#include "ftxui/dom/elements.hpp" // for Element, gauge
|
|
|
|
#include "ftxui/dom/node.hpp" // for Node
|
|
|
|
#include "ftxui/dom/requirement.hpp" // for Requirement
|
|
|
|
#include "ftxui/screen/box.hpp" // for Box
|
|
|
|
#include "ftxui/screen/screen.hpp" // for Screen
|
2018-09-20 03:52:25 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2019-01-07 00:10:35 +08:00
|
|
|
|
2021-07-04 23:38:31 +08:00
|
|
|
#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
|
|
|
|
// Microsoft's terminals often use fonts not handling the 8 unicode characters
|
|
|
|
// for representing the whole gauge. Fallback with less.
|
|
|
|
static wchar_t charset[] = L" ▌▌▌███";
|
|
|
|
#else
|
2018-09-22 15:49:43 +08:00
|
|
|
static wchar_t charset[] = L" ▏▎▍▌▋▊▉█";
|
2021-07-04 23:38:31 +08:00
|
|
|
#endif
|
2018-09-22 15:49:43 +08:00
|
|
|
|
2018-09-20 03:52:25 +08:00
|
|
|
class Gauge : public Node {
|
|
|
|
public:
|
|
|
|
Gauge(float progress) : progress_(progress) {}
|
|
|
|
|
|
|
|
void ComputeRequirement() override {
|
2020-06-02 05:40:32 +08:00
|
|
|
requirement_.flex_grow_x = 1;
|
|
|
|
requirement_.flex_grow_y = 0;
|
|
|
|
requirement_.flex_shrink_x = 1;
|
|
|
|
requirement_.flex_shrink_y = 0;
|
|
|
|
requirement_.min_x = 1;
|
2020-06-01 22:13:29 +08:00
|
|
|
requirement_.min_y = 1;
|
2018-09-20 03:52:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Render(Screen& screen) override {
|
2019-01-20 05:06:05 +08:00
|
|
|
float y = box_.y_min;
|
|
|
|
float limit = box_.x_min + progress_ * (box_.x_max - box_.x_min + 1);
|
2018-09-22 15:49:43 +08:00
|
|
|
int limit_int = limit;
|
2019-01-20 05:06:05 +08:00
|
|
|
int x = box_.x_min;
|
2018-09-22 15:49:43 +08:00
|
|
|
while (x < limit_int)
|
|
|
|
screen.at(x++, y) = charset[9];
|
2020-03-23 05:32:44 +08:00
|
|
|
screen.at(x++, y) = charset[int(9 * (limit - limit_int))];
|
2019-01-20 05:06:05 +08:00
|
|
|
while (x <= box_.x_max)
|
2018-09-22 15:49:43 +08:00
|
|
|
screen.at(x++, y) = charset[0];
|
2018-09-20 03:52:25 +08:00
|
|
|
}
|
2020-03-23 05:32:44 +08:00
|
|
|
|
2018-09-20 03:52:25 +08:00
|
|
|
private:
|
|
|
|
float progress_;
|
|
|
|
};
|
|
|
|
|
2020-05-25 07:34:13 +08:00
|
|
|
/// @brief Draw a high definition progress bar.
|
|
|
|
/// @param progress The proportion of the area to be filled. Belong to [0,1].
|
|
|
|
/// @ingroup dom
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// A gauge. It can be used to represent a progress bar.
|
|
|
|
/// ~~~cpp
|
|
|
|
/// border(gauge(0.5))
|
|
|
|
/// ~~~
|
|
|
|
///
|
|
|
|
/// #### Output
|
|
|
|
///
|
|
|
|
/// ~~~bash
|
|
|
|
/// ┌──────────────────────────────────────────────────────────────────────────┐
|
|
|
|
/// │█████████████████████████████████████ │
|
|
|
|
/// └──────────────────────────────────────────────────────────────────────────┘
|
|
|
|
/// ~~~
|
2020-05-21 02:36:47 +08:00
|
|
|
Element gauge(float progress) {
|
|
|
|
return std::make_shared<Gauge>(progress);
|
2018-09-20 03:52:25 +08:00
|
|
|
}
|
|
|
|
|
2020-02-12 04:44:55 +08:00
|
|
|
} // namespace ftxui
|
2020-08-16 06:24:18 +08:00
|
|
|
|
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|