FTXUI/examples/dom/graph.cpp

74 lines
1.8 KiB
C++
Raw Normal View History

2019-01-27 04:52:55 +08:00
#include <chrono>
#include <cmath>
#include <iostream>
#include <thread>
2020-03-23 05:32:44 +08:00
2019-01-27 09:33:06 +08:00
#include "ftxui/dom/elements.hpp"
#include "ftxui/screen/screen.hpp"
#include "ftxui/screen/string.hpp"
2019-01-27 04:52:55 +08:00
2019-01-27 09:33:06 +08:00
class Graph {
2019-01-27 04:52:55 +08:00
public:
std::vector<int> operator()(int width, int height) {
std::vector<int> output(width);
for (int i = 0; i < width; ++i) {
float v = 0;
2020-03-23 00:33:38 +08:00
v += 0.1f * sin((i + shift) * 0.1f);
v += 0.2f * sin((i + shift + 10) * 0.15f);
v += 0.1f * sin((i + shift) * 0.03f);
2019-01-27 04:52:55 +08:00
v *= height;
2020-03-23 00:33:38 +08:00
v += 0.5f * height;
output[i] = static_cast<int>(v);
2019-01-27 04:52:55 +08:00
}
return output;
}
int shift = 0;
};
2019-01-27 09:33:06 +08:00
std::vector<int> triangle(int width, int height) {
std::vector<int> output(width);
for (int i = 0; i < width; ++i) {
output[i] = i % (height - 4) + 2;
}
return output;
}
2019-01-27 04:52:55 +08:00
int main(int argc, const char* argv[]) {
using namespace ftxui;
using namespace std::chrono_literals;
Graph my_graph;
std::string reset_position;
for (int i = 0;; ++i) {
2020-03-23 05:32:44 +08:00
// clang-format off
2019-01-27 04:52:55 +08:00
auto document =
2019-01-27 09:33:06 +08:00
hbox(
vbox(
graph(std::ref(my_graph)), separator(),
graph(triangle) | inverted
) | flex,
separator(),
vbox(
graph(std::ref(my_graph)) | color(Color::BlueLight), separator(),
graph(std::ref(my_graph)) | color(Color::RedLight), separator(),
graph(std::ref(my_graph)) | color(Color::YellowLight)
) | flex
)
| border
| size(HEIGHT, GREATER_THAN, 40);
2020-03-23 05:32:44 +08:00
// clang-format on
2019-01-27 04:52:55 +08:00
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
Render(screen, document.get());
std::cout << reset_position << screen.ToString() << std::flush;
reset_position = screen.ResetPosition();
std::this_thread::sleep_for(0.03s);
my_graph.shift++;
}
return 0;
}