FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
src/ftxui/dom/graph.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// 本ソースコードの使用は、LICENSEファイルに記載されているMITライセンスに準拠します。
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 // Microsoftのターミナルは、グラフ全体を表す8つのUnicode文字を処理できないフォントを
22 // 使用していることがよくあります。少ない文字でフォールバックします。
23 {" ", " ", "█", " ", "█", "█", "█", "█", "█"};
24#else
25 {" ", "▗", "▐", "▖", "▄", "▟", "▌", "▙", "█"};
26#endif
27
28class Graph : public Node {
29 public:
30 explicit Graph(GraphFunction graph_function)
31 : graph_function_(std::move(graph_function)) {}
32
33 void ComputeRequirement() override {
34 requirement_.flex_grow_x = 1;
35 requirement_.flex_grow_y = 1;
36 requirement_.flex_shrink_x = 1;
37 requirement_.flex_shrink_y = 1;
38 requirement_.min_x = 3;
39 requirement_.min_y = 3;
40 }
41
42 void Render(Screen& screen) override {
43 const int width = (box_.x_max - box_.x_min + 1) * 2;
44 const int height = (box_.y_max - box_.y_min + 1) * 2;
45 if (width <= 0 || height <= 0) {
46 return;
47 }
48 auto data = graph_function_(width, height);
49 int i = 0;
50 for (int x = box_.x_min; x <= box_.x_max; ++x) {
51 const int height_1 = 2 * box_.y_max - data[i++];
52 const int height_2 = 2 * box_.y_max - data[i++];
53 for (int y = box_.y_min; y <= box_.y_max; ++y) {
54 const int yy = 2 * y;
55 int i_1 = yy < height_1 ? 0 : yy == height_1 ? 3 : 6; // NOLINT
56 int i_2 = yy < height_2 ? 0 : yy == height_2 ? 1 : 2; // NOLINT
57 screen.at(x, y) = charset[i_1 + i_2]; // NOLINT
58 }
59 }
60 }
61
62 private:
63 GraphFunction graph_function_;
64};
65
66} // namespace
67
68/// @brief GraphFunctionを使用してグラフを描画します。
69/// @param graph_function データを取得するために呼び出される関数。
70Element graph(GraphFunction graph_function) {
71 return std::make_shared<Graph>(std::move(graph_function));
72}
73
74} // namespace ftxui
void Render(Screen &screen, const Element &element)
要素をftxui::Screenに表示します。
Definition node.cpp:84
FTXUI ftxui:: 名前空間
Definition animation.hpp:9
std::shared_ptr< Node > Element
Definition elements.hpp:21
std::function< std::vector< int >(int, int)> GraphFunction
Definition elements.hpp:24
Element graph(GraphFunction)
GraphFunctionを使用してグラフを描画します。