FTXUI  4.1.1
C++ functional terminal UI.
Loading...
Searching...
No Matches
util.cpp
Go to the documentation of this file.
1#include <algorithm> // for min
2#include <functional> // for function
3#include <memory> // for __shared_ptr_access, make_unique
4#include <type_traits> // for remove_reference, remove_reference<>::type
5#include <utility> // for move
6#include <vector> // for vector
7
8#include "ftxui/dom/elements.hpp" // for Element, Decorator, Elements, operator|, Fit, emptyElement, nothing, operator|=
9#include "ftxui/dom/node.hpp" // for Node, Node::Status
10#include "ftxui/dom/requirement.hpp" // for Requirement
11#include "ftxui/screen/box.hpp" // for Box
12#include "ftxui/screen/screen.hpp" // for Full
13#include "ftxui/screen/terminal.hpp" // for Dimensions
14
15namespace ftxui {
16
17namespace {
18Decorator compose(Decorator a, Decorator b) {
19 return [a = std::move(a), b = std::move(b)](Element element) {
20 return b(a(std::move(element)));
21 };
22}
23} // namespace
24
25/// @brief A decoration doing absolutely nothing.
26/// @ingroup dom
28 return element;
29}
30
31/// @brief Compose two decorator into one.
32/// @ingroup dom
33///
34/// ### Example
35///
36/// ```cpp
37/// auto decorator = bold | blink;
38/// ```
40 return compose(std::move(a), //
41 std::move(b));
42}
43
44/// @brief From a set of element, apply a decorator to every elements.
45/// @return the set of decorated element.
46/// @ingroup dom
47Elements operator|(Elements elements, Decorator decorator) { // NOLINT
48 Elements output;
49 for (auto& it : elements) {
50 output.push_back(std::move(it) | decorator);
51 }
52 return output;
53}
54
55/// @brief From an element, apply a decorator.
56/// @return the decorated element.
57/// @ingroup dom
58///
59/// ### Example
60///
61/// Both of these are equivalent:
62/// ```cpp
63/// bold(text("Hello"));
64/// ```
65/// ```cpp
66/// text("Hello") | bold;
67/// ```
68Element operator|(Element element, Decorator decorator) { // NOLINT
69 return decorator(std::move(element));
70}
71
72/// @brief Apply a decorator to an element.
73/// @return the decorated element.
74/// @ingroup dom
75///
76/// ### Example
77///
78/// Both of these are equivalent:
79/// ```cpp
80/// auto element = text("Hello");
81/// element |= bold;
82/// ```
84 e = e | std::move(d);
85 return e;
86}
87
88/// The minimal dimension that will fit the given element.
89/// @see Fixed
90/// @see Full
91Dimensions Dimension::Fit(Element& e) {
92 const Dimensions fullsize = Dimension::Full();
93 Box box;
94 box.x_min = 0;
95 box.y_min = 0;
96 box.x_max = fullsize.dimx;
97 box.y_max = fullsize.dimy;
98
99 Node::Status status;
100 e->Check(&status);
101 const int max_iteration = 20;
102 while (status.need_iteration && status.iteration < max_iteration) {
103 e->ComputeRequirement();
104
105 // Don't give the element more space than it needs:
106 box.x_max = std::min(box.x_max, e->requirement().min_x);
107 box.y_max = std::min(box.y_max, e->requirement().min_y);
108
109 e->SetBox(box);
110 status.need_iteration = false;
111 status.iteration++;
112 e->Check(&status);
113
114 if (!status.need_iteration) {
115 break;
116 }
117 // Increase the size of the box until it fits, but not more than the with of
118 // the terminal emulator:
119 box.x_max = std::min(e->requirement().min_x, fullsize.dimx);
120 box.y_max = std::min(e->requirement().min_y, fullsize.dimy);
121 }
122
123 return {
124 box.x_max,
125 box.y_max,
126 };
127}
128
129/// An element of size 0x0 drawing nothing.
130/// @ingroup dom
132 class Impl : public Node {
133 void ComputeRequirement() override {
134 requirement_.min_x = 0;
135 requirement_.min_y = 0;
136 }
137 };
138 return std::make_unique<Impl>();
139}
140
141} // namespace ftxui
142
143// Copyright 2020 Arthur Sonzogni. All rights reserved.
144// Use of this source code is governed by the MIT license that can be found in
145// the LICENSE file.
Dimensions Fit(Element &)
Dimensions Full()
std::function< Element(Element)> Decorator
Definition elements.hpp:21
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition util.cpp:27
std::shared_ptr< Node > Element
Definition elements.hpp:19
Element emptyElement()
Definition util.cpp:131
std::vector< Element > Elements
Definition elements.hpp:20
Component operator|(Component component, ComponentDecorator decorator)
Definition util.cpp:9
Component & operator|=(Component &component, ComponentDecorator decorator)
Definition util.cpp:19