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 <array>
5#include <cstddef>
6#include <functional> // for function
7#include <memory> // for allocator, make_shared
8#include <string> // for string
9#include <utility> // for move
10#include <vector> // for vector
11
12#include "ftxui/dom/elements.hpp" // for GraphFunction, Element, graph
13#include "ftxui/dom/node.hpp" // for Node
14#include "ftxui/dom/requirement.hpp" // for Requirement
15#include "ftxui/screen/box.hpp" // for Box
16#include "ftxui/screen/screen.hpp" // for Screen
17
19
20namespace ftxui {
21
22namespace {
23// NOLINTNEXTLINE
24static const std::array<std::string, 9> charset = {" ", "▗", "▐", "▖", "▄",
25 "▟", "▌", "▙", "█"};
26
27// NOLINTNEXTLINE
28static const std::array<std::string, 9> charset_microsoft = {
29 " ", " ", "█", " ", "█", "█", "█", "█", "█"};
30
31class Graph : public Node {
32 public:
33 explicit Graph(GraphFunction graph_function)
34 : graph_function_(std::move(graph_function)) {}
35
36 void ComputeRequirement() override {
37 requirement_.flex_grow_x = 1;
38 requirement_.flex_grow_y = 1;
39 requirement_.flex_shrink_x = 1;
40 requirement_.flex_shrink_y = 1;
41 requirement_.min_x = 3;
42 requirement_.min_y = 3;
43 }
44
45 void Render(Screen& screen) override {
46 const int width = (box_.x_max - box_.x_min + 1) * 2;
47 const int height = (box_.y_max - box_.y_min + 1) * 2;
48 if (width <= 0 || height <= 0) {
49 return;
50 }
51
52 const auto& current_charset =
53 Terminal::GetQuirks().block_characters ? charset : charset_microsoft;
54
55 auto data = graph_function_(width, height);
56 int i = 0;
57 for (int x = box_.x_min; x <= box_.x_max; ++x) {
58 const int height_1 = 2 * box_.y_max - data.at(static_cast<size_t>(i++));
59 const int height_2 = 2 * box_.y_max - data.at(static_cast<size_t>(i++));
60 for (int y = box_.y_min; y <= box_.y_max; ++y) {
61 const int yy = 2 * y;
62 const int i_1 = yy < height_1 ? 0 : yy == height_1 ? 3 : 6; // NOLINT
63 const int i_2 = yy < height_2 ? 0 : yy == height_2 ? 1 : 2; // NOLINT
64 screen.at(x, y) =
65 current_charset.at(static_cast<size_t>(i_1 + i_2)); // NOLINT
66 }
67 }
68 }
69
70 private:
71 GraphFunction graph_function_;
72};
73
74} // namespace
75
76/// @brief Draw a graph using a GraphFunction.
77/// @param graph_function the function to be called to get the data.
78Element graph(GraphFunction graph_function) {
79 return std::make_shared<Graph>(std::move(graph_function));
80}
81
82} // namespace ftxui
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:84
bool block_characters
Whether the terminal font supports the 8 Unicode block characters.
Definition terminal.hpp:37
Quirks GetQuirks()
Get the terminal quirks.
Definition terminal.cpp:183
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::shared_ptr< Node > Element
Definition elements.hpp:23
std::function< std::vector< int >(int, int)> GraphFunction
Definition elements.hpp:26
Element graph(GraphFunction)
Draw a graph using a GraphFunction.