FTXUI/examples/dom/graph.cpp

86 lines
2.7 KiB
C++
Raw Normal View History

2023-08-19 19:56:36 +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.
#include <chrono> // for operator""s, chrono_literals
#include <cmath> // for sin
2022-03-31 08:17:43 +08:00
#include <ftxui/dom/elements.hpp> // for graph, operator|, separator, color, Element, vbox, flex, inverted, operator|=, Fit, hbox, size, border, GREATER_THAN, HEIGHT
#include <ftxui/screen/screen.hpp> // for Full, Screen
#include <functional> // for ref, reference_wrapper
#include <iostream> // for cout, ostream
2022-03-31 08:17:43 +08:00
#include <memory> // for shared_ptr
#include <string> // for operator<<, string
#include <thread> // for sleep_for
#include <vector> // for vector
2021-05-02 02:40:35 +08:00
2022-01-07 18:03:54 +08:00
#include "ftxui/dom/node.hpp" // for Render
#include "ftxui/screen/color.hpp" // for Color, Color::BlueLight, Color::RedLight, Color::YellowLight, ftxui
2020-03-23 05:32:44 +08:00
2019-01-27 09:33:06 +08:00
class Graph {
2019-01-27 04:52:55 +08:00
public:
2022-03-31 08:17:43 +08:00
std::vector<int> operator()(int width, int height) const {
2019-01-27 04:52:55 +08:00
std::vector<int> output(width);
for (int i = 0; i < width; ++i) {
float v = 0;
2022-03-31 08:17:43 +08:00
v += 0.1f * sin((i + shift) * 0.1f); // NOLINT
v += 0.2f * sin((i + shift + 10) * 0.15f); // NOLINT
v += 0.1f * sin((i + shift) * 0.03f); // NOLINT
v *= height; // NOLINT
v += 0.5f * height; // NOLINT
2020-03-23 00:33:38 +08:00
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;
}
2022-03-31 08:17:43 +08:00
int main() {
2019-01-27 04:52:55 +08:00
using namespace ftxui;
using namespace std::chrono_literals;
Graph my_graph;
std::string reset_position;
for (int i = 0;; ++i) {
2022-03-31 08:17:43 +08:00
auto document = hbox({
vbox({
graph(std::ref(my_graph)),
separator(),
2022-03-31 08:17:43 +08:00
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,
});
document |= border;
const int min_width = 40;
document |= size(HEIGHT, GREATER_THAN, min_width);
2019-01-27 04:52:55 +08:00
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
Render(screen, document);
2021-03-22 05:54:39 +08:00
std::cout << reset_position;
screen.Print();
2019-01-27 04:52:55 +08:00
reset_position = screen.ResetPosition();
2022-03-31 08:17:43 +08:00
const auto sleep_time = 0.03s;
std::this_thread::sleep_for(sleep_time);
2019-01-27 04:52:55 +08:00
my_graph.shift++;
}
return 0;
}