FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
src/ftxui/dom/graph.cpp
浏览该文件的文档.
1// Copyright 2020 Arthur Sonzogni. 保留所有权利。
2// 此源代码受 MIT 许可证的约束,该许可证可在 LICENSE 文件中找到。
3#include <functional> // for function
4#include <memory> // for allocator, make_shared
5#include <string> // for string
6#include <utility> // for move
7#include <vector> // for vector
8
9#include "ftxui/dom/elements.hpp" // for GraphFunction, Element, graph
10#include "ftxui/dom/node.hpp" // for Node
11#include "ftxui/dom/requirement.hpp" // for Requirement
12#include "ftxui/screen/box.hpp" // for Box
13#include "ftxui/screen/screen.hpp" // for Screen
14
15namespace ftxui {
16
17namespace {
18// NOLINTNEXTLINE
19static std::string charset[] =
20#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
21 // 微软的终端通常使用无法处理用于表示整个图表的8个Unicode字符的字体。回退到较少的字符。
22 {" ", " ", "█", " ", "█", "█", "█", "█", "█"};
23#else
24 {" ", "▗", "▐", "▖", "▄", "▟", "▌", "▙", "█"};
25#endif
26
27class Graph : public Node {
28 public:
29 explicit Graph(GraphFunction graph_function)
30 : graph_function_(std::move(graph_function)) {}
31
32 void ComputeRequirement() override {
33 requirement_.flex_grow_x = 1;
34 requirement_.flex_grow_y = 1;
35 requirement_.flex_shrink_x = 1;
36 requirement_.flex_shrink_y = 1;
37 requirement_.min_x = 3;
38 requirement_.min_y = 3;
39 }
40
41 void Render(Screen& screen) override {
42 const int width = (box_.x_max - box_.x_min + 1) * 2;
43 const int height = (box_.y_max - box_.y_min + 1) * 2;
44 if (width <= 0 || height <= 0) {
45 return;
46 }
47 auto data = graph_function_(width, height);
48 int i = 0;
49 for (int x = box_.x_min; x <= box_.x_max; ++x) {
50 const int height_1 = 2 * box_.y_max - data[i++];
51 const int height_2 = 2 * box_.y_max - data[i++];
52 for (int y = box_.y_min; y <= box_.y_max; ++y) {
53 const int yy = 2 * y;
54 int i_1 = yy < height_1 ? 0 : yy == height_1 ? 3 : 6; // NOLINT
55 int i_2 = yy < height_2 ? 0 : yy == height_2 ? 1 : 2; // NOLINT
56 screen.at(x, y) = charset[i_1 + i_2]; // NOLINT
57 }
58 }
59 }
60
61 private:
62 GraphFunction graph_function_;
63};
64
65} // namespace
66
67/// @brief 使用 GraphFunction 绘制图表。
68/// @param graph_function 用于获取数据的函数。
69Element graph(GraphFunction graph_function) {
70 return std::make_shared<Graph>(std::move(graph_function));
71}
72
73} // namespace ftxui
void Render(Screen &screen, const Element &element)
在 ftxui::Screen 上显示元素。
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
std::shared_ptr< Node > Element
std::function< std::vector< int >(int, int)> GraphFunction
Element graph(GraphFunction)
使用 GraphFunction 绘制图表。