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// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <functional> // for function
5#include <memory> // for allocator, make_shared
6#include <string> // for string
7#include <utility> // for move
8#include <vector> // for vector
9
10#include "ftxui/dom/elements.hpp" // for GraphFunction, Element, graph
11#include "ftxui/dom/node.hpp" // for Node
12#include "ftxui/dom/requirement.hpp" // for Requirement
13#include "ftxui/screen/box.hpp" // for Box
14#include "ftxui/screen/screen.hpp" // for Screen
15
16 namespace ftxui {
17
18 namespace {
19// NOLINTNEXTLINE
20 static std::string charset[] =
21#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
22 // Microsoft's terminals often use fonts not handling the 8 unicode
23 // characters for representing the whole graph. Fallback with less.
24 {" ", " ", "█", " ", "█", "█", "█", "█", "█"};
25#else
26 {" ", "▗", "▐", "▖", "▄", "▟", "▌", "▙", "█"};
27#endif
28
29 class Graph : public Node {
30 public:
31 explicit Graph(GraphFunction graph_function)
32 : graph_function_(std::move(graph_function)) {}
33
34 void ComputeRequirement() override {
35 requirement_.flex_grow_x = 1;
36 requirement_.flex_grow_y = 1;
37 requirement_.flex_shrink_x = 1;
38 requirement_.flex_shrink_y = 1;
39 requirement_.min_x = 3;
40 requirement_.min_y = 3;
41 }
42
43 void Render(Screen& screen) override {
44 const int width = (box_.x_max - box_.x_min + 1) * 2;
45 const int height = (box_.y_max - box_.y_min + 1) * 2;
46 if (width <= 0 || height <= 0) {
47 return;
48 }
49 auto data = graph_function_(width, height);
50 int i = 0;
51 for (int x = box_.x_min; x <= box_.x_max; ++x) {
52 const int height_1 = 2 * box_.y_max - data[i++];
53 const int height_2 = 2 * box_.y_max - data[i++];
54 for (int y = box_.y_min; y <= box_.y_max; ++y) {
55 const int yy = 2 * y;
56 int i_1 = yy < height_1 ? 0 : yy == height_1 ? 3 : 6; // NOLINT
57 int i_2 = yy < height_2 ? 0 : yy == height_2 ? 1 : 2; // NOLINT
58 screen.at(x, y) = charset[i_1 + i_2]; // NOLINT
59 }
60 }
61 }
62
63 private:
64 GraphFunction graph_function_;
65};
66
67} // namespace
68
69/// @brief Draw a graph using a GraphFunction.
70/// @param graph_function the function to be called to get the data.
71 Element graph(GraphFunction graph_function) {
72 return std::make_shared<Graph>(std::move(graph_function));
73}
74
75} // namespace ftxui
box.hpp
elements.hpp
ftxui::Render
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:84
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::GraphFunction
std::function< std::vector< int >(int, int)> GraphFunction
Definition elements.hpp:25
ftxui::graph
Element graph(GraphFunction)
Draw a graph using a GraphFunction.
Definition src/ftxui/dom/graph.cpp:71
node.hpp
requirement.hpp
screen.hpp