FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
examples/dom/graph.cpp
浏览该文件的文档.
1// 版权所有 2020 Arthur Sonzogni。保留所有权利。
2// 本源代码的使用受 MIT 许可证的约束,该许可证可在 LICENSE 文件中找到。
3#include <chrono> // for operator""s, chrono_literals
4#include <cmath> // for sin
5#include <ftxui/dom/elements.hpp> // for graph, operator|, separator, color, Element, vbox, flex, inverted, operator|=, Fit, hbox, size, border, GREATER_THAN, HEIGHT
6#include <ftxui/screen/screen.hpp> // for Full, Screen
7#include <functional> // for ref, reference_wrapper
8#include <iostream> // for cout, ostream
9#include <memory> // for shared_ptr
10#include <string> // for operator<<, string
11#include <thread> // for sleep_for
12#include <utility> // for ignore
13#include <vector> // for vector
14
15#include "ftxui/dom/node.hpp" // for Render
16#include "ftxui/screen/color.hpp" // for Color, Color::BlueLight, Color::RedLight, Color::YellowLight, ftxui
17
18class Graph {
19 public:
20 std::vector<int> operator()(int width, int height) const {
21 std::vector<int> output(width);
22 for (int i = 0; i < width; ++i) {
23 float v = 0;
24 v += 0.1f * sin((i + shift) * 0.1f); // NOLINT
25 v += 0.2f * sin((i + shift + 10) * 0.15f); // NOLINT
26 v += 0.1f * sin((i + shift) * 0.03f); // NOLINT
27 v *= height; // NOLINT
28 v += 0.5f * height; // NOLINT
29 output[i] = static_cast<int>(v);
30 }
31 return output;
32 }
33 int shift = 0;
34};
35
36std::vector<int> triangle(int width, int height) {
37 std::vector<int> output(width);
38 for (int i = 0; i < width; ++i) {
39 output[i] = i % (height - 4) + 2;
40 }
41 return output;
42}
43
44int main() {
45 using namespace ftxui;
46 using namespace std::chrono_literals;
47
48 Graph my_graph;
49
50 std::string reset_position;
51 for (int i = 0;; ++i) {
52 std::ignore = i;
53 auto document = hbox({
54 vbox({
55 graph(std::ref(my_graph)),
56 separator(),
57 graph(triangle) | inverted,
58 }) | flex,
59 separator(),
60 vbox({
61 graph(std::ref(my_graph)) | color(Color::BlueLight),
62 separator(),
63 graph(std::ref(my_graph)) | color(Color::RedLight),
64 separator(),
65 graph(std::ref(my_graph)) | color(Color::YellowLight),
66 }) | flex,
67 });
68
69 document |= border;
70
71 const int min_width = 40;
72 document |= size(HEIGHT, GREATER_THAN, min_width);
73
74 auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
75 Render(screen, document);
76 std::cout << reset_position;
77 screen.Print();
78 reset_position = screen.ResetPosition();
79
80 const auto sleep_time = 0.03s;
81 std::this_thread::sleep_for(sleep_time);
82 my_graph.shift++;
83 }
84
85 return 0;
86}
std::vector< int > triangle(int width, int height)
Element color(const LinearGradient &gradient, Element child)
使用线性渐变效果设置元素的前景色。
Element border(Element child)
在元素周围绘制边框。
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase